Repository: liqd/a4-opin

Issue:
delete comments of paragraphs and document if document was deleted 



Discussion:

Resolving patch:
diff --git a/euth/documents/apps.py b/euth/documents/apps.py
--- a/euth/documents/apps.py
+++ b/euth/documents/apps.py
@@ -4,3 +4,6 @@
 class DocumentConfig(AppConfig):
     name = 'euth.documents'
     label = 'euth_documents'
+
+    def ready(self):
+        import euth.documents.signals  # noqa:F401
diff --git a/euth/documents/models.py b/euth/documents/models.py
--- a/euth/documents/models.py
+++ b/euth/documents/models.py
@@ -31,6 +31,14 @@ def clean(self, *args, **kwargs):
     def paragraphs_sorted(self):
         return self.paragraphs.all().order_by('weight')
 
+    @cached_property
+    def comments(self):
+        contenttype = ContentType.objects.get_for_model(self)
+        pk = self.id
+        comments = comment_models.Comment.objects.all().filter(
+            content_type=contenttype, object_pk=pk)
+        return comments
+
 
 class Paragraph(base_models.TimeStampedModel):
     name = models.CharField(max_length=120, blank=True)
diff --git a/euth/documents/signals.py b/euth/documents/signals.py
new file mode 100644
--- /dev/null
+++ b/euth/documents/signals.py
@@ -0,0 +1,21 @@
+from django.contrib.contenttypes.models import ContentType
+from django.db.models.signals import post_delete
+from django.dispatch import receiver
+
+from euth.contrib import services
+
+from .models import Document, Paragraph
+
+
+@receiver(post_delete, sender=Document)
+def delete_comments_for_Document(sender, instance, **kwargs):
+    contenttype = ContentType.objects.get_for_model(instance)
+    pk = instance.pk
+    services.delete_comments(contenttype, pk)
+
+
+@receiver(post_delete, sender=Paragraph)
+def delete_comments_for_Paragraph(sender, instance, **kwargs):
+    contenttype = ContentType.objects.get_for_model(instance)
+    pk = instance.pk
+    services.delete_comments(contenttype, pk)


Test patch:
diff --git a/tests/documents/test_document_models.py b/tests/documents/test_document_models.py
--- a/tests/documents/test_document_models.py
+++ b/tests/documents/test_document_models.py
@@ -44,3 +44,31 @@ def test_paragraphs_comments(paragraph, comment_factory):
         comment_factory(content_object=paragraph)
     comment_count = comments_models.Comment.objects.all().count()
     assert comment_count == paragraph.comments.count()
+
+
+@pytest.mark.django_db
+def test_delete_document(document, comment_factory):
+    for i in range(5):
+        comment_factory(content_object=document)
+    comment_count = comments_models.Comment.objects.all().count()
+    assert comment_count == len(document.comments.all())
+
+    assert comment_count == 5
+
+    document.delete()
+    comment_count = comments_models.Comment.objects.all().count()
+    assert comment_count == 0
+
+
+@pytest.mark.django_db
+def test_delete_paragraph(paragraph, comment_factory):
+    for i in range(5):
+        comment_factory(content_object=paragraph)
+    comment_count = comments_models.Comment.objects.all().count()
+    assert comment_count == len(paragraph.comments.all())
+
+    assert comment_count == 5
+
+    paragraph.delete()
+    comment_count = comments_models.Comment.objects.all().count()
+    assert comment_count == 0