bgneal@75
|
1 """
|
bgneal@469
|
2 Signal handlers & signals for the forums application.
|
bgneal@469
|
3
|
bgneal@75
|
4 """
|
bgneal@75
|
5 from django.db.models.signals import post_save
|
bgneal@75
|
6 from django.db.models.signals import post_delete
|
bgneal@469
|
7 import django.dispatch
|
bgneal@181
|
8
|
bgneal@348
|
9 from forums.models import Forum, Topic, Post
|
bgneal@232
|
10 from forums.views.subscriptions import notify_topic_subscribers
|
bgneal@390
|
11 from forums.tools import auto_favorite, auto_subscribe
|
bgneal@75
|
12
|
bgneal@75
|
13
|
bgneal@75
|
14 def on_topic_save(sender, **kwargs):
|
bgneal@75
|
15 if kwargs['created']:
|
bgneal@75
|
16 topic = kwargs['instance']
|
bgneal@75
|
17 topic.forum.topic_count_update()
|
bgneal@75
|
18 topic.forum.save()
|
bgneal@75
|
19
|
bgneal@75
|
20
|
bgneal@75
|
21 def on_topic_delete(sender, **kwargs):
|
bgneal@75
|
22 topic = kwargs['instance']
|
bgneal@75
|
23 topic.forum.topic_count_update()
|
bgneal@75
|
24 topic.forum.save()
|
bgneal@75
|
25
|
bgneal@75
|
26
|
bgneal@75
|
27 def on_post_save(sender, **kwargs):
|
bgneal@75
|
28 if kwargs['created']:
|
bgneal@75
|
29 post = kwargs['instance']
|
bgneal@75
|
30
|
bgneal@75
|
31 # update the topic
|
bgneal@75
|
32 post.topic.post_count_update()
|
bgneal@75
|
33 post.topic.save()
|
bgneal@75
|
34
|
bgneal@75
|
35 # update the forum
|
bgneal@75
|
36 post.topic.forum.post_count_update()
|
bgneal@75
|
37 post.topic.forum.save()
|
bgneal@75
|
38
|
bgneal@181
|
39 # send out any email notifications
|
bgneal@181
|
40 notify_topic_subscribers(post)
|
bgneal@181
|
41
|
bgneal@390
|
42 # perform any auto-favorite and auto-subscribe actions for the new post
|
bgneal@390
|
43 auto_favorite(post)
|
bgneal@390
|
44 auto_subscribe(post)
|
bgneal@390
|
45
|
bgneal@75
|
46
|
bgneal@75
|
47 def on_post_delete(sender, **kwargs):
|
bgneal@75
|
48 post = kwargs['instance']
|
bgneal@75
|
49
|
bgneal@75
|
50 # update the topic
|
bgneal@348
|
51 try:
|
bgneal@348
|
52 post.topic.post_count_update()
|
bgneal@348
|
53 post.topic.save()
|
bgneal@348
|
54 except Topic.DoesNotExist:
|
bgneal@348
|
55 pass
|
bgneal@348
|
56 else:
|
bgneal@348
|
57 # update the forum
|
bgneal@348
|
58 try:
|
bgneal@348
|
59 post.topic.forum.post_count_update()
|
bgneal@348
|
60 post.topic.forum.save()
|
bgneal@348
|
61 except Forum.DoesNotExist:
|
bgneal@348
|
62 pass
|
bgneal@75
|
63
|
bgneal@75
|
64
|
bgneal@260
|
65 post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.signals')
|
bgneal@260
|
66 post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals')
|
bgneal@75
|
67
|
bgneal@260
|
68 post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals')
|
bgneal@260
|
69 post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals')
|
bgneal@469
|
70
|
bgneal@469
|
71
|
bgneal@469
|
72 # Signals for the forums application.
|
bgneal@469
|
73 #
|
bgneal@469
|
74 # This signal is sent when a topic has had its textual content (title) changed.
|
bgneal@469
|
75 # The provided arguments are:
|
bgneal@469
|
76 # sender - the topic model instance
|
bgneal@469
|
77 # instance - the topic model instance
|
bgneal@469
|
78 # created - True if the topic is new, False if updated
|
bgneal@469
|
79
|
bgneal@469
|
80 topic_content_update = django.dispatch.Signal(providing_args=['action'])
|
bgneal@469
|
81
|
bgneal@469
|
82 # This signal is sent when a post has had its textual content (body) changed.
|
bgneal@469
|
83 # The provided arguments are:
|
bgneal@469
|
84 # sender - the post model instance
|
bgneal@469
|
85 # instance - the topic model instance
|
bgneal@469
|
86 # created - True if the post is new, False if updated
|
bgneal@469
|
87
|
bgneal@469
|
88 post_content_update = django.dispatch.Signal(providing_args=['action'])
|
bgneal@469
|
89
|
bgneal@469
|
90
|
bgneal@469
|
91 def notify_new_topic(topic):
|
bgneal@469
|
92 """
|
bgneal@469
|
93 Sends the topic_content_update signal for a new topic instance.
|
bgneal@469
|
94
|
bgneal@469
|
95 """
|
bgneal@469
|
96 topic_content_update.send_robust(topic, instance=topic, created=True)
|
bgneal@469
|
97
|
bgneal@469
|
98
|
bgneal@469
|
99 def notify_updated_topic(topic):
|
bgneal@469
|
100 """
|
bgneal@469
|
101 Sends the topic_content_update signal for an updated topic instance.
|
bgneal@469
|
102
|
bgneal@469
|
103 """
|
bgneal@469
|
104 topic_content_update.send_robust(topic, instance=topic, created=False)
|
bgneal@469
|
105
|
bgneal@469
|
106
|
bgneal@469
|
107 def notify_new_post(post):
|
bgneal@469
|
108 """
|
bgneal@469
|
109 Sends the post_content_update signal for a new post instance.
|
bgneal@469
|
110
|
bgneal@469
|
111 """
|
bgneal@469
|
112 post_content_update.send_robust(post, instance=post, created=True)
|
bgneal@469
|
113
|
bgneal@469
|
114
|
bgneal@469
|
115 def notify_updated_post(post):
|
bgneal@469
|
116 """
|
bgneal@469
|
117 Sends the post_content_update signal for an updated post instance.
|
bgneal@469
|
118
|
bgneal@469
|
119 """
|
bgneal@469
|
120 post_content_update.send_robust(post, instance=post, created=False)
|