bgneal@75: """
bgneal@469: Signal handlers & signals for the forums application.
bgneal@469: 
bgneal@75: """
bgneal@75: from django.db.models.signals import post_save
bgneal@75: from django.db.models.signals import post_delete
bgneal@469: import django.dispatch
bgneal@181: 
bgneal@348: from forums.models import Forum, Topic, 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@522:     forums.latest.notify_topic_delete(topic)
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@348:     try:
bgneal@348:         post.topic.post_count_update()
bgneal@348:         post.topic.save()
bgneal@348:     except Topic.DoesNotExist:
bgneal@348:         pass
bgneal@348:     else:
bgneal@348:         # update the forum
bgneal@348:         try:
bgneal@348:             post.topic.forum.post_count_update()
bgneal@348:             post.topic.forum.save()
bgneal@348:         except Forum.DoesNotExist:
bgneal@348:             pass
bgneal@75: 
bgneal@75: 
bgneal@260: post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.signals')
bgneal@260: post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals')
bgneal@75: 
bgneal@260: post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals')
bgneal@260: post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals')
bgneal@469: 
bgneal@469: 
bgneal@469: # Signals for the forums application.
bgneal@469: #
bgneal@469: # This signal is sent when a topic has had its textual content (title) changed.
bgneal@469: # The provided arguments are: 
bgneal@469: #   sender - the topic model instance 
bgneal@469: #   created - True if the topic is new, False if updated
bgneal@469: 
bgneal@470: topic_content_update = django.dispatch.Signal(providing_args=['created'])
bgneal@469: 
bgneal@469: # This signal is sent when a post has had its textual content (body) changed.
bgneal@469: # The provided arguments are: 
bgneal@469: #   sender - the post model instance 
bgneal@469: #   created - True if the post is new, False if updated
bgneal@469: 
bgneal@470: post_content_update = django.dispatch.Signal(providing_args=['created'])
bgneal@469: 
bgneal@469: 
bgneal@469: def notify_new_topic(topic):
bgneal@469:     """
bgneal@469:     Sends the topic_content_update signal for a new topic instance.
bgneal@469: 
bgneal@469:     """
bgneal@470:     topic_content_update.send_robust(topic, created=True)
bgneal@469: 
bgneal@469: 
bgneal@469: def notify_updated_topic(topic):
bgneal@469:     """
bgneal@469:     Sends the topic_content_update signal for an updated topic instance.
bgneal@469: 
bgneal@469:     """
bgneal@470:     topic_content_update.send_robust(topic, created=False)
bgneal@469: 
bgneal@469: 
bgneal@469: def notify_new_post(post):
bgneal@469:     """
bgneal@469:     Sends the post_content_update signal for a new post instance.
bgneal@469: 
bgneal@469:     """
bgneal@470:     post_content_update.send_robust(post, created=True)
bgneal@469: 
bgneal@469: 
bgneal@469: def notify_updated_post(post):
bgneal@469:     """
bgneal@469:     Sends the post_content_update signal for an updated post instance.
bgneal@469: 
bgneal@469:     """
bgneal@470:     post_content_update.send_robust(post, created=False)
bgneal@522: 
bgneal@522: 
bgneal@522: # Avoid circular imports
bgneal@522: import forums.latest