# HG changeset patch # User neko259 # Date 2015-01-21 16:34:34 # Node ID e663371ba1cd980d90439bd601fcfc5694455932 # Parent 99fbb992f61aff396820e5e27adc13b26817678f Delete replies properly when deleting a thread. Delete thread directly, not by deleting its OP diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -355,8 +355,7 @@ class Post(models.Model, Viewable): def delete(self, using=None): """ - Deletes all post images and the post itself. If the post is opening, - thread with all posts is deleted. + Deletes all post images and the post itself. """ for image in self.images.all(): @@ -364,12 +363,9 @@ class Post(models.Model, Viewable): if image_refs_count == 1: image.delete() - if self.is_opening(): - self.get_thread().delete() - else: - thread = self.get_thread() - thread.last_edit_time = timezone.now() - thread.save() + thread = self.get_thread() + thread.last_edit_time = timezone.now() + thread.save() super(Post, self).delete(using) diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -127,6 +127,10 @@ class Thread(models.Model): return query.all() def get_replies_with_images(self, view_fields_only=False): + """ + Gets replies that have at least one image attached + """ + return self.get_replies(view_fields_only).annotate(images_count=Count( 'images')).filter(images_count__gt=0) @@ -173,10 +177,14 @@ class Thread(models.Model): return self.get_opening_post().pub_time def delete(self, using=None): - if self.replies.exists(): - self.replies.all().delete() + """ + Deletes thread with all replies. + """ + + for reply in self.replies.all(): + reply.delete() super(Thread, self).delete(using) def __str__(self): - return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) \ No newline at end of file + return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) diff --git a/boards/tests/test_post.py b/boards/tests/test_post.py --- a/boards/tests/test_post.py +++ b/boards/tests/test_post.py @@ -37,7 +37,7 @@ class PostTests(TestCase): thread = opening_post.get_thread() reply = Post.objects.create_post("", "", thread=thread) - opening_post.delete() + thread.delete() self.assertFalse(Post.objects.filter(id=reply.id).exists(), 'Reply was not deleted with the thread.')