comparison 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
comparison
equal deleted inserted replaced
106:cb72577785df 107:e94398f5e027
106 if self.post_count > 0: 106 if self.post_count > 0:
107 self.last_post = my_posts[self.post_count - 1] 107 self.last_post = my_posts[self.post_count - 1]
108 else: 108 else:
109 self.last_post = None 109 self.last_post = None
110 110
111 def last_post_pre_delete(self):
112 """
113 Call this function prior to deleting the last post in the forum.
114 A new last post will be found, if one exists.
115 This is to avoid the Django cascading delete issue.
116 """
117 try:
118 self.last_post = \
119 Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest()
120 except Post.DoesNotExist:
121 self.last_post = None
122
111 123
112 class Topic(models.Model): 124 class Topic(models.Model):
113 """ 125 """
114 A topic is a thread of discussion, consisting of a series of posts. 126 A topic is a thread of discussion, consisting of a series of posts.
115 """ 127 """
166 self.creation_date = now 178 self.creation_date = now
167 self.update_date = now 179 self.update_date = now
168 180
169 super(Topic, self).save(*args, **kwargs) 181 super(Topic, self).save(*args, **kwargs)
170 182
183 def last_post_pre_delete(self):
184 """
185 Call this function prior to deleting the last post in the topic.
186 A new last post will be found, if one exists.
187 This is to avoid the Django cascading delete issue.
188 """
189 try:
190 self.last_post = \
191 Post.objects.filter(topic=self).exclude(pk=self.last_post.pk).latest()
192 except Post.DoesNotExist:
193 self.last_post = None
194
171 195
172 class Post(models.Model): 196 class Post(models.Model):
173 """ 197 """
174 A post is an instance of a user's single contribution to a topic. 198 A post is an instance of a user's single contribution to a topic.
175 """ 199 """
181 html = models.TextField() 205 html = models.TextField()
182 user_ip = models.IPAddressField(blank=True, default='', null=True) 206 user_ip = models.IPAddressField(blank=True, default='', null=True)
183 207
184 class Meta: 208 class Meta:
185 ordering = ('creation_date', ) 209 ordering = ('creation_date', )
210 get_latest_by = 'creation_date'
186 211
187 @models.permalink 212 @models.permalink
188 def get_absolute_url(self): 213 def get_absolute_url(self):
189 return ('forums-goto_post', [self.pk]) 214 return ('forums-goto_post', [self.pk])
190 215