From e8669f4a1d3afcfecd92439dc7738ee84e346ca8 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sun, 6 Nov 2022 20:58:50 +0300 Subject: [PATCH] fixed shape delete error --- image_markuper/dicom/api/views.py | 2 +- image_markuper/dicom/models/base.py | 10 +++++++++- image_markuper/dicom/models/shapes.py | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/image_markuper/dicom/api/views.py b/image_markuper/dicom/api/views.py index 432dd07..5ef6b5c 100644 --- a/image_markuper/dicom/api/views.py +++ b/image_markuper/dicom/api/views.py @@ -173,7 +173,7 @@ class ListUpdateDicomImageNumberApi(GenericAPIView): ) def put(self, request, dicom_slug): dicom = get_object_or_404(Dicom, slug=dicom_slug) - [x.delete() for x in dicom.shapes] + dicom.delete_shapes() serializer = BaseShapeLayerSerializer( data=request.data, many=True, context={"request": request} ) diff --git a/image_markuper/dicom/models/base.py b/image_markuper/dicom/models/base.py index fcea371..dff6839 100644 --- a/image_markuper/dicom/models/base.py +++ b/image_markuper/dicom/models/base.py @@ -82,7 +82,15 @@ class Dicom(models.Model): @property def shapes(self): - return BaseShape.objects.filter(layer_fk__dicom=self) + qs = BaseShape.objects.filter(layer_fk__dicom=self) + if qs.exists(): + return qs + return None + + def delete_shapes(self): + shapes = self.shapes + if shapes: + [x.destroy() for x in shapes] def get_layers(self): return self.layers.filter(parent__isnull=True).first().serialize_self() diff --git a/image_markuper/dicom/models/shapes.py b/image_markuper/dicom/models/shapes.py index 5a07fb8..754b4dd 100644 --- a/image_markuper/dicom/models/shapes.py +++ b/image_markuper/dicom/models/shapes.py @@ -33,6 +33,12 @@ class BaseShape(PolymorphicModel): def coordinates(self) -> [(int, int)]: return self.shape_coordinates.all().values("x", "y") + def destroy(self): + try: + self.delete() + except BaseShape.DoesNotExist: + pass + def __str__(self): return f"{self.TYPE} on {self.layer}" @@ -51,6 +57,7 @@ class Coordinate(models.Model): class Circle(BaseShape): + TYPE = "circle" radius = models.FloatField() max_coordinates = 1