annotate forums/signals.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
parents ee87ea74d46b
children 5902dc5e58a3
rev   line source
bgneal@75 1 """
bgneal@469 2 Signal handlers & signals for the forums application.
bgneal@469 3
bgneal@75 4 """
bgneal@75 5 from django.db.models.signals import post_save
bgneal@75 6 from django.db.models.signals import post_delete
bgneal@469 7 import django.dispatch
bgneal@181 8
bgneal@348 9 from forums.models import Forum, Topic, Post
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@522 23 forums.latest.notify_topic_delete(topic)
bgneal@75 24
bgneal@75 25
bgneal@75 26 def on_post_save(sender, **kwargs):
bgneal@75 27 if kwargs['created']:
bgneal@75 28 post = kwargs['instance']
bgneal@75 29
bgneal@75 30 # update the topic
bgneal@75 31 post.topic.post_count_update()
bgneal@75 32 post.topic.save()
bgneal@75 33
bgneal@75 34 # update the forum
bgneal@75 35 post.topic.forum.post_count_update()
bgneal@75 36 post.topic.forum.save()
bgneal@75 37
bgneal@75 38
bgneal@75 39 def on_post_delete(sender, **kwargs):
bgneal@75 40 post = kwargs['instance']
bgneal@75 41
bgneal@75 42 # update the topic
bgneal@348 43 try:
bgneal@348 44 post.topic.post_count_update()
bgneal@348 45 post.topic.save()
bgneal@348 46 except Topic.DoesNotExist:
bgneal@348 47 pass
bgneal@348 48 else:
bgneal@348 49 # update the forum
bgneal@348 50 try:
bgneal@348 51 post.topic.forum.post_count_update()
bgneal@348 52 post.topic.forum.save()
bgneal@348 53 except Forum.DoesNotExist:
bgneal@348 54 pass
bgneal@75 55
bgneal@75 56
bgneal@260 57 post_save.connect(on_topic_save, sender=Topic, dispatch_uid='forums.signals')
bgneal@260 58 post_delete.connect(on_topic_delete, sender=Topic, dispatch_uid='forums.signals')
bgneal@75 59
bgneal@260 60 post_save.connect(on_post_save, sender=Post, dispatch_uid='forums.signals')
bgneal@260 61 post_delete.connect(on_post_delete, sender=Post, dispatch_uid='forums.signals')
bgneal@469 62
bgneal@469 63
bgneal@469 64 # Signals for the forums application.
bgneal@469 65 #
bgneal@469 66 # This signal is sent when a topic has had its textual content (title) changed.
bgneal@469 67 # The provided arguments are:
bgneal@469 68 # sender - the topic model instance
bgneal@469 69 # created - True if the topic is new, False if updated
bgneal@469 70
bgneal@470 71 topic_content_update = django.dispatch.Signal(providing_args=['created'])
bgneal@469 72
bgneal@469 73 # This signal is sent when a post has had its textual content (body) changed.
bgneal@469 74 # The provided arguments are:
bgneal@469 75 # sender - the post model instance
bgneal@469 76 # created - True if the post is new, False if updated
bgneal@469 77
bgneal@470 78 post_content_update = django.dispatch.Signal(providing_args=['created'])
bgneal@469 79
bgneal@469 80
bgneal@469 81 def notify_new_topic(topic):
bgneal@469 82 """
bgneal@469 83 Sends the topic_content_update signal for a new topic instance.
bgneal@469 84
bgneal@469 85 """
bgneal@470 86 topic_content_update.send_robust(topic, created=True)
bgneal@469 87
bgneal@469 88
bgneal@469 89 def notify_updated_topic(topic):
bgneal@469 90 """
bgneal@469 91 Sends the topic_content_update signal for an updated topic instance.
bgneal@469 92
bgneal@469 93 """
bgneal@470 94 topic_content_update.send_robust(topic, created=False)
bgneal@469 95
bgneal@469 96
bgneal@469 97 def notify_new_post(post):
bgneal@469 98 """
bgneal@469 99 Sends the post_content_update signal for a new post instance.
bgneal@469 100
bgneal@469 101 """
bgneal@470 102 post_content_update.send_robust(post, created=True)
bgneal@469 103
bgneal@469 104
bgneal@469 105 def notify_updated_post(post):
bgneal@469 106 """
bgneal@469 107 Sends the post_content_update signal for an updated post instance.
bgneal@469 108
bgneal@469 109 """
bgneal@470 110 post_content_update.send_robust(post, created=False)
bgneal@522 111
bgneal@522 112
bgneal@522 113 # Avoid circular imports
bgneal@522 114 import forums.latest