Repository: genialis/resolwe

Issue:
404 returned when deleting entity with `delete_content` set to `true`
Probably because sample gets deleted when the last data object is deleted.


Discussion:

Resolving patch:
diff --git a/resolwe/flow/views/entity.py b/resolwe/flow/views/entity.py
--- a/resolwe/flow/views/entity.py
+++ b/resolwe/flow/views/entity.py
@@ -68,6 +68,11 @@ def destroy(self, request, *args, **kwargs):
                 if user.has_perm('edit_data', data):
                     data.delete()
 
+            # If all data objects in an entity are removed, the entity may
+            # have already been removed, so there is no need to call destroy.
+            if not Entity.objects.filter(pk=obj.pk).exists():
+                return Response(status=status.HTTP_204_NO_CONTENT)
+
         # NOTE: Collection's ``destroy`` method should be skiped, so we
         # intentionaly call it's parent.
         return super(CollectionViewSet, self).destroy(  # pylint: disable=no-member,bad-super-call


Test patch:
diff --git a/resolwe/flow/tests/test_api.py b/resolwe/flow/tests/test_api.py
--- a/resolwe/flow/tests/test_api.py
+++ b/resolwe/flow/tests/test_api.py
@@ -419,11 +419,11 @@ def test_delete(self):
 
         entity.data.add(data_1, data_2)
 
-        assign_perm("view_entity", self.user, entity)
-        assign_perm("edit_entity", self.user, entity)
-        assign_perm("view_data", self.user, data_1)
-        assign_perm("view_data", self.user, data_2)
-        assign_perm("edit_data", self.user, data_1)
+        assign_perm('view_entity', self.user, entity)
+        assign_perm('edit_entity', self.user, entity)
+        assign_perm('view_data', self.user, data_1)
+        assign_perm('view_data', self.user, data_2)
+        assign_perm('edit_data', self.user, data_1)
 
         request = factory.delete(self.detail_url(entity.pk))
         force_authenticate(request, self.user)
@@ -440,8 +440,8 @@ def test_delete(self):
 
         entity.data.add(data_1, data_2)
 
-        assign_perm("view_entity", self.user, entity)
-        assign_perm("edit_entity", self.user, entity)
+        assign_perm('view_entity', self.user, entity)
+        assign_perm('edit_entity', self.user, entity)
 
         request = factory.delete('{}?delete_content=1'.format(self.detail_url(entity.pk)))
         force_authenticate(request, self.user)
@@ -450,3 +450,24 @@ def test_delete(self):
         # Only objects with `edit` permission can be deleted.
         self.assertFalse(Data.objects.filter(pk=data_1.pk).exists())
         self.assertTrue(Data.objects.filter(pk=data_2.pk).exists())
+
+        # Ensure that deletion works correctly when all data objects of an entity
+        # are deleted.
+        entity = Entity.objects.create(
+            name="Test entity",
+            contributor=self.contributor,
+        )
+
+        assign_perm('view_entity', self.user, entity)
+        assign_perm('edit_entity', self.user, entity)
+        assign_perm('edit_data', self.user, data_2)
+
+        entity.data.add(data_2)
+
+        request = factory.delete('{}?delete_content=1'.format(self.detail_url(entity.pk)))
+        force_authenticate(request, self.user)
+        response = self.entity_detail_viewset(request, pk=entity.pk)
+
+        self.assertEqual(response.status_code, 204)
+        self.assertFalse(Entity.objects.filter(pk=entity.pk).exists())
+        self.assertFalse(Data.objects.filter(pk=data_2.pk).exists())