backend/image_markuper/dicom/api/serializers.py

115 lines
3.6 KiB
Python
Raw Normal View History

2022-10-28 21:52:02 +03:00
from dicom.models import Circle, Coordinate, Dicom, Polygon
from drf_spectacular.utils import extend_schema_field
2022-10-26 23:22:04 +03:00
from rest_framework import serializers
2022-10-28 21:52:02 +03:00
from rest_framework.generics import get_object_or_404
def create_coordinate(coordinates, obj):
for coordinate in coordinates:
Coordinate.objects.create(
x=coordinate["x"],
y=coordinate["y"],
shape=obj,
)
class CoordinateSerializer(serializers.ModelSerializer):
class Meta:
model = Coordinate
fields = ["x", "y"]
2022-10-26 23:22:04 +03:00
class ListDicomSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="get_update_delete_dicom", lookup_field="slug"
)
file = serializers.FileField()
class Meta:
model = Dicom
2022-10-28 22:17:29 +03:00
fields = ["file", "uploaded", "pathology_type", "url"]
2022-10-26 23:22:04 +03:00
def create(self, validated_data):
return Dicom.objects.create(**validated_data, user=self.context["request"].user)
2022-10-28 21:52:02 +03:00
class BaseShapeSerializer(serializers.Serializer):
type = serializers.ChoiceField(choices=["circle", "polygon"])
image_number = serializers.IntegerField()
coordinates = CoordinateSerializer(many=True)
2022-10-26 23:22:04 +03:00
class DicomSerializer(serializers.ModelSerializer):
file = serializers.FileField()
2022-10-28 21:52:02 +03:00
shapes = serializers.SerializerMethodField("get_dicom_shapes")
@extend_schema_field(field=BaseShapeSerializer)
def get_dicom_shapes(self, obj):
return [x.serialize_self() for x in obj.shapes.all()]
2022-10-26 23:22:04 +03:00
class Meta:
model = Dicom
2022-10-28 22:17:29 +03:00
fields = ["file", "uploaded", "pathology_type", "shapes"]
2022-10-28 21:52:02 +03:00
class PolygonSerializer(serializers.ModelSerializer):
coordinates = CoordinateSerializer(many=True)
class Meta:
model = Polygon
fields = ["id", "image_number", "coordinates"]
extra_kwargs = {"id": {"read_only": True}}
def create(self, validated_data):
if "coordinates" not in validated_data:
raise serializers.ValidationError
dicom = get_object_or_404(
Dicom, slug=self.context["request"].parser_context["kwargs"]["slug"]
)
polygon = Polygon.objects.create(
dicom=dicom, image_number=validated_data["image_number"]
)
create_coordinate(validated_data["coordinates"], polygon)
return polygon
def update(self, obj: Circle, validated_data):
Coordinate.objects.filter(shape=obj).delete()
create_coordinate(validated_data["coordinates"], obj)
return obj
class CircleSerializer(serializers.ModelSerializer):
coordinates = CoordinateSerializer(many=True)
class Meta:
model = Circle
fields = ["id", "image_number", "radius", "coordinates"]
extra_kwargs = {"id": {"read_only": True}}
def create(self, validated_data):
if (
"coordinates" not in validated_data
and len(validated_data["coordinates"]) > 1
):
raise serializers.ValidationError
dicom = get_object_or_404(
Dicom, slug=self.context["request"].parser_context["kwargs"]["slug"]
)
circle = Circle.objects.create(
dicom=dicom,
image_number=validated_data["image_number"],
radius=validated_data["radius"],
)
create_coordinate(validated_data["coordinates"], circle)
return circle
def update(self, obj: Circle, validated_data):
Coordinate.objects.filter(shape=obj).delete()
create_coordinate(validated_data["coordinates"], obj)
if "radius" in validated_data:
obj.radius = validated_data["radius"]
obj.save()
return obj