comparison forums/signals.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 ee87ea74d46b
children
comparison
equal deleted inserted replaced
919:0b6bf9c5a982 920:5902dc5e58a3
1 """ 1 """
2 Signal handlers & signals for the forums application. 2 Signals for the forums application.
3 3
4 """ 4 """
5 from django.db.models.signals import post_save
6 from django.db.models.signals import post_delete
7 import django.dispatch 5 import django.dispatch
8 6
9 from forums.models import Forum, Topic, Post
10 7
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. 8 # This signal is sent when a topic has had its textual content (title) changed.
67 # The provided arguments are: 9 # The provided arguments are:
68 # sender - the topic model instance 10 # sender - the topic model instance
69 # created - True if the topic is new, False if updated 11 # created - True if the topic is new, False if updated
70 12
106 """ 48 """
107 Sends the post_content_update signal for an updated post instance. 49 Sends the post_content_update signal for an updated post instance.
108 50
109 """ 51 """
110 post_content_update.send_robust(post, created=False) 52 post_content_update.send_robust(post, created=False)
111
112
113 # Avoid circular imports
114 import forums.latest