PonyConf/conversations/signals.py

59 lines
2.1 KiB
Python
Raw Normal View History

2016-06-14 21:58:04 +00:00
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
2016-06-14 19:39:04 +00:00
from accounts.models import Participation
from proposals.models import Talk
2016-07-06 16:19:00 +00:00
from proposals.signals import *
from .models import ConversationAboutTalk, ConversationWithParticipant, Message
2016-06-14 19:39:04 +00:00
2016-06-14 21:58:04 +00:00
@receiver(post_save, sender=Participation, dispatch_uid="Create ConversationWithParticipant")
def create_conversation_with_participant(sender, instance, created, **kwargs):
2016-06-14 19:39:04 +00:00
if not created:
return
2016-06-14 21:58:04 +00:00
conversation = ConversationWithParticipant(participation=instance)
conversation.save()
2016-06-12 21:39:04 +00:00
2016-06-14 21:58:04 +00:00
@receiver(post_save, sender=Talk, dispatch_uid="Create ConversationAboutTalk")
def create_conversation_about_talk(sender, instance, created, **kwargs):
2016-06-12 21:39:04 +00:00
if not created:
return
2016-06-14 21:58:04 +00:00
conversation = ConversationAboutTalk(talk=instance)
conversation.save()
2016-06-12 21:39:04 +00:00
2016-07-06 16:19:00 +00:00
def check_talk(talk):
# Subscribe reviewer for these topics to conversations
2016-07-09 16:42:24 +00:00
reviewers = User.objects.filter(topic__talk=talk)
2016-07-06 16:19:00 +00:00
talk.conversation.subscribers.add(*reviewers)
for user in talk.speakers.all():
participation, created = Participation.on_site.get_or_create(user=user, site=talk.site)
participation.conversation.subscribers.add(*reviewers)
2016-07-06 16:19:00 +00:00
@receiver(talk_added, dispatch_uid="Notify talk added")
def notify_talk_added(sender, instance, author, **kwargs):
check_talk(instance)
message = Message(conversation=instance.conversation, author=author,
content='The talk has been proposed.', system=True)
2016-06-14 21:58:04 +00:00
message.save()
2016-06-12 21:39:04 +00:00
2016-07-06 16:19:00 +00:00
@receiver(talk_edited, dispatch_uid="Notify talk edited")
def notify_talk_edited(sender, instance, author, **kwargs):
check_talk(instance)
message = Message(conversation=instance.conversation, author=author,
content='The talk has been modified.', system=True)
2016-07-06 16:19:00 +00:00
message.save()
2016-06-14 21:58:04 +00:00
@receiver(post_save, sender=Message, dispatch_uid="Notify new message")
def notify_new_message(sender, instance, created, **kwargs):
if not created:
# Possibly send a modification notification?
return
instance.conversation.new_message(instance)