comparison forums/signals.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/forums/signals.py@82b97697312e
children 5902dc5e58a3
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Signal handlers & signals 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 import django.dispatch
8
9 from forums.models import Forum, Topic, Post
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.signals')
58 post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals')
59
60 post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals')
61 post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals')
62
63
64 # Signals for the forums application.
65 #
66 # This signal is sent when a topic has had its textual content (title) changed.
67 # The provided arguments are:
68 # sender - the topic model instance
69 # created - True if the topic is new, False if updated
70
71 topic_content_update = django.dispatch.Signal(providing_args=['created'])
72
73 # This signal is sent when a post has had its textual content (body) changed.
74 # The provided arguments are:
75 # sender - the post model instance
76 # created - True if the post is new, False if updated
77
78 post_content_update = django.dispatch.Signal(providing_args=['created'])
79
80
81 def notify_new_topic(topic):
82 """
83 Sends the topic_content_update signal for a new topic instance.
84
85 """
86 topic_content_update.send_robust(topic, created=True)
87
88
89 def notify_updated_topic(topic):
90 """
91 Sends the topic_content_update signal for an updated topic instance.
92
93 """
94 topic_content_update.send_robust(topic, created=False)
95
96
97 def notify_new_post(post):
98 """
99 Sends the post_content_update signal for a new post instance.
100
101 """
102 post_content_update.send_robust(post, created=True)
103
104
105 def notify_updated_post(post):
106 """
107 Sends the post_content_update signal for an updated post instance.
108
109 """
110 post_content_update.send_robust(post, created=False)
111
112
113 # Avoid circular imports
114 import forums.latest