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
|
2016-06-19 23:00:08 +00:00
|
|
|
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
|
2016-06-19 23:00:08 +00:00
|
|
|
from proposals.models import Talk
|
2016-07-11 19:33:34 +00:00
|
|
|
from proposals.signals import talk_added, talk_edited
|
2016-06-19 23:00:08 +00:00
|
|
|
|
|
|
|
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):
|
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)
|
2016-06-15 17:03:18 +00:00
|
|
|
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,
|
2016-07-07 17:46:19 +00:00
|
|
|
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,
|
2016-07-07 17:46:19 +00:00
|
|
|
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)
|