This repository has been archived on 2025-05-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
s2forums/apps/forum/signals.py

43 lines
1.5 KiB
Python
Raw Normal View History

2009-05-24 18:50:57 +03:00
from datetime import datetime
from django.db.models.signals import post_save, pre_save, post_delete
from forum.subscription import notify_topic_subscribers, notify_pm_recipients
2009-05-24 18:50:57 +03:00
from forum.models import Topic, Post, PrivateMessage
2009-01-19 19:50:01 +02:00
def post_saved(instance, **kwargs):
2009-05-24 18:50:57 +03:00
created = kwargs.get('created')
post = instance
if created:
updated_time = datetime.now()
post.topic.updated = updated_time
post.topic.post_count = Post.objects.filter(topic=post.topic).count()
post.topic.save(force_update=True)
post.topic.forum.updated = updated_time
post.topic.forum.post_count = Post.objects.filter(topic__forum=post.topic.forum).count()
post.topic.forum.last_post = post
post.topic.forum.save(force_update=True)
notify_topic_subscribers(post)
def post_deleted(instance, **kwargs):
post = instance
post.topic.post_count = Post.objects.filter(topic=post.topic).count()
post.topic.save()
post.topic.forum.post_count = Post.objects.filter(topic__forum=post.topic.forum).count()
post.topic.forum.save()
2009-01-19 19:50:01 +02:00
def pm_saved(instance, **kwargs):
notify_pm_recipients(instance)
2009-05-24 18:50:57 +03:00
def topic_saved(instance, **kwargs):
created = kwargs.get('created')
topic = instance
if created:
topic.forum.topic_count = topic.forum.topics.count()
topic.forum.save(force_update=True)
2009-01-19 19:50:01 +02:00
post_save.connect(post_saved, sender=Post)
2009-05-24 18:50:57 +03:00
post_save.connect(pm_saved, sender=PrivateMessage)
post_save.connect(topic_saved, sender=Topic)
post_delete.connect(post_deleted, sender=Post)