Mercurial > public > sg101
annotate gpp/forums/signals.py @ 235:d302c498560e
Fix problem when deleting multiple topics from a forum in bulk. We getting a list of topics from the database, then deleting each topic. But after you delete a topic, the forum.last_post on the remaining non-deleted topics can be stale. This was causing a weird DoesNotExist. Now just get the topics one at a time from the database.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 26 Aug 2010 04:01:58 +0000 |
parents | a46788862737 |
children | 3a4bbf9c2cce |
rev | line source |
---|---|
bgneal@75 | 1 """ |
bgneal@75 | 2 Signal handlers for the forums application. |
bgneal@75 | 3 """ |
bgneal@75 | 4 from django.db.models.signals import post_save |
bgneal@75 | 5 from django.db.models.signals import post_delete |
bgneal@181 | 6 |
bgneal@181 | 7 from forums.models import Topic, Post |
bgneal@232 | 8 from forums.views.subscriptions import notify_topic_subscribers |
bgneal@75 | 9 |
bgneal@75 | 10 |
bgneal@75 | 11 def on_topic_save(sender, **kwargs): |
bgneal@75 | 12 if kwargs['created']: |
bgneal@75 | 13 topic = kwargs['instance'] |
bgneal@75 | 14 topic.forum.topic_count_update() |
bgneal@75 | 15 topic.forum.save() |
bgneal@75 | 16 |
bgneal@75 | 17 |
bgneal@75 | 18 def on_topic_delete(sender, **kwargs): |
bgneal@75 | 19 topic = kwargs['instance'] |
bgneal@75 | 20 topic.forum.topic_count_update() |
bgneal@75 | 21 topic.forum.save() |
bgneal@75 | 22 |
bgneal@75 | 23 |
bgneal@75 | 24 def on_post_save(sender, **kwargs): |
bgneal@75 | 25 if kwargs['created']: |
bgneal@75 | 26 post = kwargs['instance'] |
bgneal@75 | 27 |
bgneal@75 | 28 # update the topic |
bgneal@75 | 29 post.topic.post_count_update() |
bgneal@75 | 30 post.topic.save() |
bgneal@75 | 31 |
bgneal@75 | 32 # update the forum |
bgneal@75 | 33 post.topic.forum.post_count_update() |
bgneal@75 | 34 post.topic.forum.save() |
bgneal@75 | 35 |
bgneal@181 | 36 # send out any email notifications |
bgneal@181 | 37 notify_topic_subscribers(post) |
bgneal@181 | 38 |
bgneal@75 | 39 |
bgneal@75 | 40 def on_post_delete(sender, **kwargs): |
bgneal@75 | 41 post = kwargs['instance'] |
bgneal@75 | 42 |
bgneal@75 | 43 # update the topic |
bgneal@75 | 44 post.topic.post_count_update() |
bgneal@75 | 45 post.topic.save() |
bgneal@75 | 46 |
bgneal@75 | 47 # update the forum |
bgneal@75 | 48 post.topic.forum.post_count_update() |
bgneal@75 | 49 post.topic.forum.save() |
bgneal@75 | 50 |
bgneal@75 | 51 |
bgneal@75 | 52 post_save.connect(on_topic_save, sender=Topic) |
bgneal@75 | 53 post_delete.connect(on_topic_delete, sender=Topic) |
bgneal@75 | 54 |
bgneal@75 | 55 post_save.connect(on_post_save, sender=Post) |
bgneal@75 | 56 post_delete.connect(on_post_delete, sender=Post) |