diff gpp/forums/signals.py @ 75:374b24dd2f9a

First checkin of forums. Have noticed cascading delete behavior. Will try to prevent this next.
author Brian Neal <bgneal@gmail.com>
date Sun, 05 Jul 2009 00:03:40 +0000
parents
children 500e5875a306
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/signals.py	Sun Jul 05 00:03:40 2009 +0000
@@ -0,0 +1,52 @@
+"""
+Signal handlers for the forums application.
+"""
+from django.db.models.signals import post_save
+from django.db.models.signals import post_delete
+from models import Topic
+from models import Post
+
+
+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()
+
+
+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
+    post.topic.post_count_update()
+    post.topic.save()
+
+    # update the forum
+    post.topic.forum.post_count_update()
+    post.topic.forum.save()
+
+
+post_save.connect(on_topic_save, sender=Topic)
+post_delete.connect(on_topic_delete, sender=Topic)
+
+post_save.connect(on_post_save, sender=Post)
+post_delete.connect(on_post_delete, sender=Post)