PonyConf/conversations/signals.py

65 lines
2.4 KiB
Python
Raw Normal View History

2016-06-14 21:58:04 +00:00
from django.contrib.auth.models import User
2016-07-11 19:33:34 +00:00
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
2016-06-14 19:39:04 +00:00
2016-10-24 20:35:56 +00:00
from ponyconf.decorators import disable_for_loaddata
2016-06-14 19:39:04 +00:00
from accounts.models import Participation
from proposals.models import Talk
2016-07-11 19:33:34 +00:00
from proposals.signals import talk_added, talk_edited
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")
2016-10-24 20:35:56 +00:00
@disable_for_loaddata
2016-06-14 21:58:04 +00:00
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")
2016-10-24 20:35:56 +00:00
@disable_for_loaddata
2016-06-14 21:58:04 +00:00
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):
2016-07-09 19:38:36 +00:00
reviewers = User.objects.filter(Q(topic__talk=talk) | Q(participation__site=talk.site, participation__orga=True))
# Subscribe the reviewers to the conversation about the talk
2016-07-06 16:19:00 +00:00
talk.conversation.subscribers.add(*reviewers)
2016-07-09 19:38:36 +00:00
# Subscribe the reviewers to the conversations with each speaker
2016-07-06 16:19:00 +00:00
for user in talk.speakers.all():
2016-07-09 19:30:49 +00:00
participation, created = Participation.objects.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")
2016-10-24 20:35:56 +00:00
@disable_for_loaddata
2016-06-14 21:58:04 +00:00
def notify_new_message(sender, instance, created, **kwargs):
if not created:
# Possibly send a modification notification?
return
instance.conversation.new_message(instance)