comparison 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
comparison
equal deleted inserted replaced
292:2367c4795c92 293:c92fb89dbc7d
130 denormalized fields. 130 denormalized fields.
131 """ 131 """
132 self.topic_count_update() 132 self.topic_count_update()
133 self.post_count_update() 133 self.post_count_update()
134 134
135 def last_post_pre_delete(self): 135 def last_post_pre_delete(self, deleting_topic=False):
136 """ 136 """
137 Call this function prior to deleting the last post in the forum. 137 Call this function prior to deleting the last post in the forum.
138 A new last post will be found, if one exists. 138 A new last post will be found, if one exists.
139 This is to avoid the Django cascading delete issue. 139 This is to avoid the Django cascading delete issue.
140 If deleting_topic is True, then the whole topic the last post is
141 part of is being deleted, so we can't pick a new last post from that
142 topic.
140 """ 143 """
141 try: 144 try:
142 self.last_post = \ 145 qs = Post.objects.filter(topic__forum=self)
143 Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest() 146 if deleting_topic:
147 qs = qs.exclude(topic=self.last_post.topic)
148 else:
149 qs = qs.exclude(pk=self.last_post.pk)
150
151 self.last_post = qs.latest()
152
144 except Post.DoesNotExist: 153 except Post.DoesNotExist:
145 self.last_post = None 154 self.last_post = None
146 155
147 def catchup(self, user, flv=None): 156 def catchup(self, user, flv=None):
148 """ 157 """
361 370
362 def save(self, *args, **kwargs): 371 def save(self, *args, **kwargs):
363 if self.id is None: 372 if self.id is None:
364 self.touch() 373 self.touch()
365 super(TopicLastVisit, self).save(*args, **kwargs) 374 super(TopicLastVisit, self).save(*args, **kwargs)
366 375
367 def touch(self): 376 def touch(self):
368 self.last_visit = datetime.datetime.now() 377 self.last_visit = datetime.datetime.now()
369 378
370 379
371 class Attachment(models.Model): 380 class Attachment(models.Model):