bgneal@75: """
bgneal@75: Signal handlers for the forums application.
bgneal@75: """
bgneal@75: from django.db.models.signals import post_save
bgneal@75: from django.db.models.signals import post_delete
bgneal@75: from models import Topic
bgneal@75: from models import Post
bgneal@75: 
bgneal@75: 
bgneal@75: def on_topic_save(sender, **kwargs):
bgneal@75:     if kwargs['created']:
bgneal@75:         topic = kwargs['instance']
bgneal@75:         topic.forum.topic_count_update()
bgneal@75:         topic.forum.save()
bgneal@75: 
bgneal@75: 
bgneal@75: def on_topic_delete(sender, **kwargs):
bgneal@75:     topic = kwargs['instance']
bgneal@75:     topic.forum.topic_count_update()
bgneal@75:     topic.forum.save()
bgneal@75: 
bgneal@75: 
bgneal@75: def on_post_save(sender, **kwargs):
bgneal@75:     if kwargs['created']:
bgneal@75:         post = kwargs['instance']
bgneal@75: 
bgneal@75:         # update the topic
bgneal@75:         post.topic.post_count_update()
bgneal@75:         post.topic.save()
bgneal@75: 
bgneal@75:         # update the forum
bgneal@75:         post.topic.forum.post_count_update()
bgneal@75:         post.topic.forum.save()
bgneal@75: 
bgneal@75: 
bgneal@75: def on_post_delete(sender, **kwargs):
bgneal@75:     post = kwargs['instance']
bgneal@75: 
bgneal@75:     # update the topic
bgneal@75:     post.topic.post_count_update()
bgneal@75:     post.topic.save()
bgneal@75: 
bgneal@75:     # update the forum
bgneal@75:     post.topic.forum.post_count_update()
bgneal@75:     post.topic.forum.save()
bgneal@75: 
bgneal@75: 
bgneal@75: post_save.connect(on_topic_save, sender=Topic)
bgneal@75: post_delete.connect(on_topic_delete, sender=Topic)
bgneal@75: 
bgneal@75: post_save.connect(on_post_save, sender=Post)
bgneal@75: post_delete.connect(on_post_delete, sender=Post)