diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/forums/receivers.py	Tue Apr 07 20:11:32 2015 -0500
@@ -0,0 +1,60 @@
+"""
+Signal handlers for the forums application.
+
+"""
+from django.db.models.signals import post_save
+from django.db.models.signals import post_delete
+
+from forums.models import Forum, Topic, Post
+import forums.latest
+
+
+def on_topic_save(sender, **kwargs):
+    if kwargs['created']:
+        topic = kwargs['instance']
+        topic.forum.topic_count_update()
+        topic.forum.save()
+
+
+def on_topic_delete(sender, **kwargs):
+    topic = kwargs['instance']
+    topic.forum.topic_count_update()
+    topic.forum.save()
+    forums.latest.notify_topic_delete(topic)
+
+
+def on_post_save(sender, **kwargs):
+    if kwargs['created']:
+        post = kwargs['instance']
+
+        # update the topic
+        post.topic.post_count_update()
+        post.topic.save()
+
+        # update the forum
+        post.topic.forum.post_count_update()
+        post.topic.forum.save()
+
+
+def on_post_delete(sender, **kwargs):
+    post = kwargs['instance']
+
+    # update the topic
+    try:
+        post.topic.post_count_update()
+        post.topic.save()
+    except Topic.DoesNotExist:
+        pass
+    else:
+        # update the forum
+        try:
+            post.topic.forum.post_count_update()
+            post.topic.forum.save()
+        except Forum.DoesNotExist:
+            pass
+
+
+post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.receivers')
+post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.receivers')
+post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.receivers')
+post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.receivers')