added dicom update

This commit is contained in:
Alexander-D-Karpov 2022-11-06 04:15:03 +03:00
parent 9019d31d48
commit 8c25cacc76
3 changed files with 46 additions and 32 deletions

View File

@ -66,7 +66,7 @@ urlpatterns = [
name="create_ruler", name="create_ruler",
), ),
path( path(
"<str:slug>/<int:layer>", "shapes/<str:dicom_slug>",
ListUpdateDicomImageNumberApi.as_view(), ListUpdateDicomImageNumberApi.as_view(),
name="update_dicom_layer", name="update_dicom_layer",
), ),

View File

@ -116,6 +116,19 @@ class BaseShapeLayerSerializer(serializers.Serializer):
radius = serializers.FloatField(required=False) radius = serializers.FloatField(required=False)
coordinates = CoordinateSerializer(many=True) 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 LayerChildSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -159,7 +172,7 @@ class LayerSerializer(serializers.ModelSerializer):
class DicomSerializer(serializers.ModelSerializer): class DicomSerializer(serializers.ModelSerializer):
file = serializers.FileField() file = serializers.FileField()
shapes = serializers.SerializerMethodField("get_dicom_shapes") 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)) @extend_schema_field(field=BaseShapeSerializer(many=True))
def get_dicom_shapes(self, obj): def get_dicom_shapes(self, obj):

View File

@ -5,7 +5,7 @@ from rest_framework.generics import GenericAPIView, get_object_or_404
from rest_framework.parsers import FormParser, MultiPartParser from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.response import Response 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 ..services import process_files
from .serializers import ( from .serializers import (
BaseShapeLayerSerializer, BaseShapeLayerSerializer,
@ -155,54 +155,53 @@ class ListUpdateDicomImageNumberApi(GenericAPIView):
@extend_schema( @extend_schema(
request=None, request=None,
responses={200: BaseShapeSerializer(many=True)}, responses={200: DicomSerializer},
operation_id="get_dicom_layer", operation_id="get_dicom_layer",
) )
def get(self, request, slug, layer): def get(self, request, dicom_slug):
shapes = [ return Response(
x.serialize_self_without_layer() DicomSerializer(
for x in get_object_or_404(Dicom, slug=slug).shapes.filter( get_object_or_404(Dicom, slug=dicom_slug), context={"request": request}
image_number=layer ).data,
) status=status.HTTP_200_OK,
] )
return Response(shapes, status=status.HTTP_200_OK)
@extend_schema( @extend_schema(
request=BaseShapeLayerSerializer(many=True), request=BaseShapeLayerSerializer(many=True),
responses={201: DicomSerializer}, responses={201: DicomSerializer},
operation_id="update_dicom_layer", operation_id="update_dicom_layer",
) )
def put(self, request, slug, layer): def put(self, request, dicom_slug):
dicom = get_object_or_404(Dicom, slug=slug) dicom = get_object_or_404(Dicom, slug=dicom_slug)
dicom.shapes.filter(image_number=layer).delete() dicom.shapes.delete()
serializer = BaseShapeLayerSerializer(data=request.data, many=True) serializer = BaseShapeLayerSerializer(
data=request.data, many=True, context={"request": request}
)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
for shape in serializer.data: for shape in serializer.data:
layer = Layer.objects.get(slug=shape["layer"])
if shape["type"] == "circle": if shape["type"] == "circle":
obj = Circle.objects.create( obj = Circle.objects.create(layer_fk=layer, radius=shape["radius"])
dicom=dicom, image_number=layer, radius=shape["radius"]
)
if len(shape["coordinates"]) > 1:
raise ValidationError
elif shape["type"] == "roi": 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) create_coordinate(shape["coordinates"], obj)
shapes = [ return Response(
x.serialize_self_without_layer() DicomSerializer(dicom, context={"request": request}).data,
for x in get_object_or_404(Dicom, slug=slug).shapes.filter( status=status.HTTP_200_OK,
image_number=layer )
)
]
return Response(shapes, status=status.HTTP_200_OK)
@extend_schema( @extend_schema(
request=None, request=None,
responses={204: None}, responses={204: None},
operation_id="delete_dicom_layer", operation_id="delete_dicom_layer",
) )
def delete(self, request, slug, layer): def delete(self, request, dicom_slug):
dicom = get_object_or_404(Dicom, slug=slug) dicom = get_object_or_404(Dicom, slug=dicom_slug)
dicom.shapes.filter(image_number=layer).delete() dicom.shapes.delete()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
@ -210,6 +209,8 @@ class ListCreateProjectApi(generics.ListCreateAPIView):
serializer_class = ListProjectSerializer serializer_class = ListProjectSerializer
def get_queryset(self): def get_queryset(self):
if self.request.user.is_staff:
return Project.objects.all()
return Project.objects.filter(user=self.request.user) return Project.objects.filter(user=self.request.user)
@extend_schema( @extend_schema(