# HG changeset patch # User Brian Neal # Date 1293598613 0 # Node ID c92fb89dbc7d49ea4c399bbdaf1eacd5df1e213d # Parent 2367c4795c925d1ef43a6394bebd49a526f8777d 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. diff -r 2367c4795c92 -r c92fb89dbc7d gpp/forums/models.py --- 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() diff -r 2367c4795c92 -r c92fb89dbc7d gpp/forums/views/main.py --- 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