diff gpp/forums/models.py @ 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 8fd4984d5c3b
children ee451ad46af1
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()