From 8c25cacc766c872aff94e9129c30cb4841d09457 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sun, 6 Nov 2022 04:15:03 +0300 Subject: [PATCH] added dicom update --- image_markuper/config/api_router.py | 2 +- image_markuper/dicom/api/serializers.py | 15 +++++- image_markuper/dicom/api/views.py | 61 +++++++++++++------------ 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/image_markuper/config/api_router.py b/image_markuper/config/api_router.py index fb46324..2d4225a 100644 --- a/image_markuper/config/api_router.py +++ b/image_markuper/config/api_router.py @@ -66,7 +66,7 @@ urlpatterns = [ name="create_ruler", ), path( - "/", + "shapes/", ListUpdateDicomImageNumberApi.as_view(), name="update_dicom_layer", ), diff --git a/image_markuper/dicom/api/serializers.py b/image_markuper/dicom/api/serializers.py index b3cf881..4fb606c 100644 --- a/image_markuper/dicom/api/serializers.py +++ b/image_markuper/dicom/api/serializers.py @@ -116,6 +116,19 @@ class BaseShapeLayerSerializer(serializers.Serializer): radius = serializers.FloatField(required=False) coordinates = CoordinateSerializer(many=True) + def validate_layer(self, val): + if val: + return get_object_or_404(Layer, slug=val).slug + return ( + get_object_or_404( + Dicom, + slug=self.context["request"].parser_context["kwargs"]["dicom_slug"], + ) + .layers.filter(parent__isnull=True) + .first() + .slug + ) + class LayerChildSerializer(serializers.ModelSerializer): class Meta: @@ -159,7 +172,7 @@ class LayerSerializer(serializers.ModelSerializer): class DicomSerializer(serializers.ModelSerializer): file = serializers.FileField() shapes = serializers.SerializerMethodField("get_dicom_shapes") - layers = serializers.SerializerMethodField("get_dicom_layers") + layers = serializers.SerializerMethodField("get_dicom_layers", read_only=True) @extend_schema_field(field=BaseShapeSerializer(many=True)) def get_dicom_shapes(self, obj): diff --git a/image_markuper/dicom/api/views.py b/image_markuper/dicom/api/views.py index ebdff47..c3f2a79 100644 --- a/image_markuper/dicom/api/views.py +++ b/image_markuper/dicom/api/views.py @@ -5,7 +5,7 @@ from rest_framework.generics import GenericAPIView, get_object_or_404 from rest_framework.parsers import FormParser, MultiPartParser from rest_framework.response import Response -from ..models import Circle, Dicom, Layer, Project, Roi +from ..models import Circle, Dicom, FreeHand, Layer, Project, Roi, Ruler from ..services import process_files from .serializers import ( BaseShapeLayerSerializer, @@ -155,54 +155,53 @@ class ListUpdateDicomImageNumberApi(GenericAPIView): @extend_schema( request=None, - responses={200: BaseShapeSerializer(many=True)}, + responses={200: DicomSerializer}, operation_id="get_dicom_layer", ) - def get(self, request, slug, layer): - shapes = [ - x.serialize_self_without_layer() - for x in get_object_or_404(Dicom, slug=slug).shapes.filter( - image_number=layer - ) - ] - return Response(shapes, status=status.HTTP_200_OK) + def get(self, request, dicom_slug): + return Response( + DicomSerializer( + get_object_or_404(Dicom, slug=dicom_slug), context={"request": request} + ).data, + status=status.HTTP_200_OK, + ) @extend_schema( request=BaseShapeLayerSerializer(many=True), responses={201: DicomSerializer}, operation_id="update_dicom_layer", ) - def put(self, request, slug, layer): - dicom = get_object_or_404(Dicom, slug=slug) - dicom.shapes.filter(image_number=layer).delete() - serializer = BaseShapeLayerSerializer(data=request.data, many=True) + def put(self, request, dicom_slug): + dicom = get_object_or_404(Dicom, slug=dicom_slug) + dicom.shapes.delete() + serializer = BaseShapeLayerSerializer( + data=request.data, many=True, context={"request": request} + ) serializer.is_valid(raise_exception=True) for shape in serializer.data: + layer = Layer.objects.get(slug=shape["layer"]) if shape["type"] == "circle": - obj = Circle.objects.create( - dicom=dicom, image_number=layer, radius=shape["radius"] - ) - if len(shape["coordinates"]) > 1: - raise ValidationError + obj = Circle.objects.create(layer_fk=layer, radius=shape["radius"]) elif shape["type"] == "roi": - obj = Roi.objects.create(dicom=dicom, image_number=layer) + obj = Roi.objects.create(layer_fk=layer) + elif shape["type"] == "free_hand": + obj = FreeHand.objects.create(layer_fk=layer) + else: + obj = Ruler.objects.create(layer_fk=layer) create_coordinate(shape["coordinates"], obj) - shapes = [ - x.serialize_self_without_layer() - for x in get_object_or_404(Dicom, slug=slug).shapes.filter( - image_number=layer - ) - ] - return Response(shapes, status=status.HTTP_200_OK) + return Response( + DicomSerializer(dicom, context={"request": request}).data, + status=status.HTTP_200_OK, + ) @extend_schema( request=None, responses={204: None}, operation_id="delete_dicom_layer", ) - def delete(self, request, slug, layer): - dicom = get_object_or_404(Dicom, slug=slug) - dicom.shapes.filter(image_number=layer).delete() + def delete(self, request, dicom_slug): + dicom = get_object_or_404(Dicom, slug=dicom_slug) + dicom.shapes.delete() return Response(status=status.HTTP_204_NO_CONTENT) @@ -210,6 +209,8 @@ class ListCreateProjectApi(generics.ListCreateAPIView): serializer_class = ListProjectSerializer def get_queryset(self): + if self.request.user.is_staff: + return Project.objects.all() return Project.objects.filter(user=self.request.user) @extend_schema(