changeset 293:c92fb89dbc7d

Fixed a bug where if you tried to delete a topic the new last post might be in that topic, so a cascading delete would occur and the forum would get deleted.
author Brian Neal <bgneal@gmail.com>
date Wed, 29 Dec 2010 04:56:53 +0000
parents 2367c4795c92
children 254db4cb6a86
files gpp/forums/models.py gpp/forums/views/main.py
diffstat 2 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/models.py	Fri Dec 24 22:20:30 2010 +0000
+++ b/gpp/forums/models.py	Wed Dec 29 04:56:53 2010 +0000
@@ -132,15 +132,24 @@
         self.topic_count_update()
         self.post_count_update()
 
-    def last_post_pre_delete(self):
+    def last_post_pre_delete(self, deleting_topic=False):
         """
         Call this function prior to deleting the last post in the forum.
         A new last post will be found, if one exists.
         This is to avoid the Django cascading delete issue.
+        If deleting_topic is True, then the whole topic the last post is
+        part of is being deleted, so we can't pick a new last post from that
+        topic.
         """
         try:
-            self.last_post = \
-                Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest()
+            qs = Post.objects.filter(topic__forum=self)
+            if deleting_topic:
+                qs = qs.exclude(topic=self.last_post.topic)
+            else:
+                qs = qs.exclude(pk=self.last_post.pk)
+
+            self.last_post = qs.latest()
+
         except Post.DoesNotExist:
             self.last_post = None
 
@@ -363,7 +372,7 @@
         if self.id is None:
             self.touch()
         super(TopicLastVisit, self).save(*args, **kwargs)
-        
+
     def touch(self):
         self.last_visit = datetime.datetime.now()
 
--- a/gpp/forums/views/main.py	Fri Dec 24 22:20:30 2010 +0000
+++ b/gpp/forums/views/main.py	Wed Dec 29 04:56:53 2010 +0000
@@ -472,7 +472,7 @@
     post counts as that doesn't seem to be worth the effort.
     """
     if topic.forum.last_post and topic.forum.last_post.topic == topic:
-        topic.forum.last_post_pre_delete()
+        topic.forum.last_post_pre_delete(deleting_topic=True)
         topic.forum.save()
 
     # delete subscriptions to this topic