Mercurial > public > sg101
view gpp/forums/signals.py @ 469:3b30286adba5
Smarter search index updating for forums. This work is for #227.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 17 Aug 2011 01:02:08 +0000 |
parents | e0523e17ea43 |
children | d9b6c4ec1977 |
line wrap: on
line source
""" Signal handlers & signals for the forums application. """ from django.db.models.signals import post_save from django.db.models.signals import post_delete import django.dispatch from forums.models import Forum, Topic, Post from forums.views.subscriptions import notify_topic_subscribers from forums.tools import auto_favorite, auto_subscribe 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() # send out any email notifications notify_topic_subscribers(post) # perform any auto-favorite and auto-subscribe actions for the new post auto_favorite(post) auto_subscribe(post) 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.signals') post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals') post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals') post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals') # Signals for the forums application. # # This signal is sent when a topic has had its textual content (title) changed. # The provided arguments are: # sender - the topic model instance # instance - the topic model instance # created - True if the topic is new, False if updated topic_content_update = django.dispatch.Signal(providing_args=['action']) # This signal is sent when a post has had its textual content (body) changed. # The provided arguments are: # sender - the post model instance # instance - the topic model instance # created - True if the post is new, False if updated post_content_update = django.dispatch.Signal(providing_args=['action']) def notify_new_topic(topic): """ Sends the topic_content_update signal for a new topic instance. """ topic_content_update.send_robust(topic, instance=topic, created=True) def notify_updated_topic(topic): """ Sends the topic_content_update signal for an updated topic instance. """ topic_content_update.send_robust(topic, instance=topic, created=False) def notify_new_post(post): """ Sends the post_content_update signal for a new post instance. """ post_content_update.send_robust(post, instance=post, created=True) def notify_updated_post(post): """ Sends the post_content_update signal for an updated post instance. """ post_content_update.send_robust(post, instance=post, created=False)