comparison forums/receivers.py @ 920:5902dc5e58a3

Forums app refactor. For Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Tue, 07 Apr 2015 20:11:32 -0500
parents forums/signals.py@ee87ea74d46b
children
comparison
equal deleted inserted replaced
919:0b6bf9c5a982 920:5902dc5e58a3
1 """
2 Signal handlers for the forums application.
3
4 """
5 from django.db.models.signals import post_save
6 from django.db.models.signals import post_delete
7
8 from forums.models import Forum, Topic, Post
9 import forums.latest
10
11
12 def on_topic_save(sender, **kwargs):
13 if kwargs['created']:
14 topic = kwargs['instance']
15 topic.forum.topic_count_update()
16 topic.forum.save()
17
18
19 def on_topic_delete(sender, **kwargs):
20 topic = kwargs['instance']
21 topic.forum.topic_count_update()
22 topic.forum.save()
23 forums.latest.notify_topic_delete(topic)
24
25
26 def on_post_save(sender, **kwargs):
27 if kwargs['created']:
28 post = kwargs['instance']
29
30 # update the topic
31 post.topic.post_count_update()
32 post.topic.save()
33
34 # update the forum
35 post.topic.forum.post_count_update()
36 post.topic.forum.save()
37
38
39 def on_post_delete(sender, **kwargs):
40 post = kwargs['instance']
41
42 # update the topic
43 try:
44 post.topic.post_count_update()
45 post.topic.save()
46 except Topic.DoesNotExist:
47 pass
48 else:
49 # update the forum
50 try:
51 post.topic.forum.post_count_update()
52 post.topic.forum.save()
53 except Forum.DoesNotExist:
54 pass
55
56
57 post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.receivers')
58 post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.receivers')
59 post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.receivers')
60 post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.receivers')