Fix #5415 - Deleted comments get removed properly when tombstones
authorJessica Tallon <tsyesika@tsyesika.se>
Mon, 29 Feb 2016 14:35:30 +0000 (14:35 +0000)
committerJessica Tallon <tsyesika@tsyesika.se>
Mon, 29 Feb 2016 15:40:34 +0000 (15:40 +0000)
The original wrapper existed and should be been removed, this fix
now ensures the TextComment removes the Comment wrapper to prevent
the deleted (comments which are tombstones) existing.

mediagoblin/db/models.py
mediagoblin/tests/test_misc.py

index 38684e6f481a277d535c4aec60227b113d6d1095..97dc91dc96ee862918b67eceb17307e6d97ed385 100644 (file)
@@ -1008,6 +1008,17 @@ class TextComment(Base, TextCommentMixin, CommentingMixin):
                                               cascade="all, delete-orphan"))
     deletion_mode = Base.SOFT_DELETE
 
+    def soft_delete(self, *args, **kwargs):
+        # Find the GMR for this model.
+        gmr = GenericModelReference.query.filter_by(
+            obj_pk=self.id,
+            model_type=self.__tablename__
+        ).first()
+
+        # Delete the Comment object for this comment
+        Comment.query.filter_by(comment_id=gmr.id).delete()
+        return super(TextComment, self).soft_delete(*args, **kwargs)
+
     def serialize(self, request):
         """ Unserialize to python dictionary for API """
         target = self.get_reply_to()
index 558a9bd76f75f949a6c03bc0f6aa90a69a6b4c36..3423520986da3056a6ac0bbc03a973aa2659bb1b 100644 (file)
@@ -138,3 +138,34 @@ def test_garbage_collection_task(test_app):
 
     # Now validate the image has been deleted
     assert MediaEntry.query.filter_by(id=entry_id).first() is None
+
+def test_comments_removed_when_graveyarded(test_app):
+    """ Checks comments which are tombstones are removed from collection """
+    user = fixture_add_user()
+    media = fixture_media_entry(
+        uploader=user.id,
+        expunge=False,
+        fake_upload=False
+    )
+    
+    # Add the TextComment
+    comment = TextComment()
+    comment.actor = user.id
+    comment.content = u"This is a comment that will be deleted."
+    comment.save()
+
+    # Add a link for the comment
+    link = Comment()
+    link.target = media
+    link.comment = comment
+    link.save()
+
+    # First double check it's there and all is well...
+    assert Comment.query.filter_by(target_id=link.target_id).first() is not None
+
+    # Now delete the comment.
+    comment.delete()
+
+    # Verify this also deleted the Comment link, ergo there is no comment left.
+    assert Comment.query.filter_by(target_id=link.target_id).first() is None