| 1 | from datetime import datetime |
|---|
| 2 | from django.db.models.signals import post_save, pre_save, post_delete |
|---|
| 3 | |
|---|
| 4 | from forum.subscription import notify_topic_subscribers, notify_pm_recipients |
|---|
| 5 | from forum.models import Topic, Post, PrivateMessage |
|---|
| 6 | |
|---|
| 7 | def post_saved(instance, **kwargs): |
|---|
| 8 | created = kwargs.get('created') |
|---|
| 9 | post = instance |
|---|
| 10 | if created: |
|---|
| 11 | updated_time = datetime.now() |
|---|
| 12 | post.topic.updated = updated_time |
|---|
| 13 | post.topic.last_post = post |
|---|
| 14 | post.topic.post_count = Post.objects.filter(topic=post.topic).count() |
|---|
| 15 | post.topic.save(force_update=True) |
|---|
| 16 | |
|---|
| 17 | post.topic.forum.updated = updated_time |
|---|
| 18 | post.topic.forum.post_count = Post.objects.filter(topic__forum=post.topic.forum).count() |
|---|
| 19 | post.topic.forum.last_post = post |
|---|
| 20 | post.topic.forum.save(force_update=True) |
|---|
| 21 | |
|---|
| 22 | notify_topic_subscribers(post) |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | def post_deleted(instance, **kwargs): |
|---|
| 26 | post = instance |
|---|
| 27 | post.topic.post_count = Post.objects.filter(topic=post.topic).count() |
|---|
| 28 | post.topic.last_post = Post.objects.filter(topic=post.topic).latest() |
|---|
| 29 | post.topic.save() |
|---|
| 30 | post.topic.forum.post_count = Post.objects.filter(topic__forum=post.topic.forum).count() |
|---|
| 31 | post.topic.forum.last_post = Post.objects.filter(topic__forum=post.topic.forum).latest() |
|---|
| 32 | post.topic.forum.save() |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | def pm_saved(instance, **kwargs): |
|---|
| 36 | notify_pm_recipients(instance) |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | def topic_saved(instance, **kwargs): |
|---|
| 40 | created = kwargs.get('created') |
|---|
| 41 | topic = instance |
|---|
| 42 | if created: |
|---|
| 43 | topic.forum.topic_count = topic.forum.topics.count() |
|---|
| 44 | topic.forum.save(force_update=True) |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | post_save.connect(post_saved, sender=Post) |
|---|
| 48 | post_save.connect(pm_saved, sender=PrivateMessage) |
|---|
| 49 | post_save.connect(topic_saved, sender=Topic) |
|---|
| 50 | |
|---|
| 51 | post_delete.connect(post_deleted, sender=Post) |
|---|