annotate gpp/forums/signals.py @ 467:b910cc1460c8

Add the ability to conditionally add model instances to the search index on update. This is not perfect, as some instances should be deleted from the index if they are updated such that they should not be in the index anymore. Will think about and address that later.
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Jul 2011 18:12:20 +0000
parents e0523e17ea43
children 3b30286adba5
rev   line source
bgneal@75 1 """
bgneal@75 2 Signal handlers for the forums application.
bgneal@75 3 """
bgneal@75 4 from django.db.models.signals import post_save
bgneal@75 5 from django.db.models.signals import post_delete
bgneal@181 6
bgneal@348 7 from forums.models import Forum, Topic, Post
bgneal@232 8 from forums.views.subscriptions import notify_topic_subscribers
bgneal@390 9 from forums.tools import auto_favorite, auto_subscribe
bgneal@75 10
bgneal@75 11
bgneal@75 12 def on_topic_save(sender, **kwargs):
bgneal@75 13 if kwargs['created']:
bgneal@75 14 topic = kwargs['instance']
bgneal@75 15 topic.forum.topic_count_update()
bgneal@75 16 topic.forum.save()
bgneal@75 17
bgneal@75 18
bgneal@75 19 def on_topic_delete(sender, **kwargs):
bgneal@75 20 topic = kwargs['instance']
bgneal@75 21 topic.forum.topic_count_update()
bgneal@75 22 topic.forum.save()
bgneal@75 23
bgneal@75 24
bgneal@75 25 def on_post_save(sender, **kwargs):
bgneal@75 26 if kwargs['created']:
bgneal@75 27 post = kwargs['instance']
bgneal@75 28
bgneal@75 29 # update the topic
bgneal@75 30 post.topic.post_count_update()
bgneal@75 31 post.topic.save()
bgneal@75 32
bgneal@75 33 # update the forum
bgneal@75 34 post.topic.forum.post_count_update()
bgneal@75 35 post.topic.forum.save()
bgneal@75 36
bgneal@181 37 # send out any email notifications
bgneal@181 38 notify_topic_subscribers(post)
bgneal@181 39
bgneal@390 40 # perform any auto-favorite and auto-subscribe actions for the new post
bgneal@390 41 auto_favorite(post)
bgneal@390 42 auto_subscribe(post)
bgneal@390 43
bgneal@75 44
bgneal@75 45 def on_post_delete(sender, **kwargs):
bgneal@75 46 post = kwargs['instance']
bgneal@75 47
bgneal@75 48 # update the topic
bgneal@348 49 try:
bgneal@348 50 post.topic.post_count_update()
bgneal@348 51 post.topic.save()
bgneal@348 52 except Topic.DoesNotExist:
bgneal@348 53 pass
bgneal@348 54 else:
bgneal@348 55 # update the forum
bgneal@348 56 try:
bgneal@348 57 post.topic.forum.post_count_update()
bgneal@348 58 post.topic.forum.save()
bgneal@348 59 except Forum.DoesNotExist:
bgneal@348 60 pass
bgneal@75 61
bgneal@75 62
bgneal@260 63 post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.signals')
bgneal@260 64 post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals')
bgneal@75 65
bgneal@260 66 post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals')
bgneal@260 67 post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals')