diff gpp/forums/models.py @ 107:e94398f5e027

Forums: implemented post delete feature.
author Brian Neal <bgneal@gmail.com>
date Tue, 22 Sep 2009 03:36:39 +0000
parents e67c4dd98db5
children d1b0b86441c0
line wrap: on
line diff
--- a/gpp/forums/models.py	Sat Sep 19 21:49:56 2009 +0000
+++ b/gpp/forums/models.py	Tue Sep 22 03:36:39 2009 +0000
@@ -108,6 +108,18 @@
         else:
             self.last_post = None
 
+    def last_post_pre_delete(self):
+        """
+        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.
+        """
+        try:
+            self.last_post = \
+                Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest()
+        except Post.DoesNotExist:
+            self.last_post = None
+
 
 class Topic(models.Model):
     """
@@ -168,6 +180,18 @@
 
         super(Topic, self).save(*args, **kwargs)
 
+    def last_post_pre_delete(self):
+        """
+        Call this function prior to deleting the last post in the topic.
+        A new last post will be found, if one exists.
+        This is to avoid the Django cascading delete issue.
+        """
+        try:
+            self.last_post = \
+                Post.objects.filter(topic=self).exclude(pk=self.last_post.pk).latest()
+        except Post.DoesNotExist:
+            self.last_post = None
+
 
 class Post(models.Model):
     """
@@ -183,6 +207,7 @@
 
     class Meta:
         ordering = ('creation_date', )
+        get_latest_by = 'creation_date'
 
     @models.permalink
     def get_absolute_url(self):