PonyConf/conversations/signals.py

49 lines
1.8 KiB
Python
Raw Normal View History

2016-06-14 21:58:04 +00:00
from django.db.models.signals import post_save, m2m_changed
2016-06-13 00:00:30 +00:00
from django.dispatch import receiver
2016-06-14 19:39:04 +00:00
from django.conf import settings
from django.core.urlresolvers import reverse
2016-06-14 21:58:04 +00:00
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.auth.models import User
2016-06-14 19:39:04 +00:00
2016-06-14 21:58:04 +00:00
from .models import ConversationWithParticipant, ConversationAboutTalk, Message
from .utils import notify_by_email
2016-06-14 19:39:04 +00:00
from proposals.models import Talk, Topic
2016-06-14 21:58:04 +00:00
from proposals.signals import new_talk
2016-06-14 19:39:04 +00:00
from accounts.models import Participation
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-06-14 21:58:04 +00:00
@receiver(new_talk, dispatch_uid="Notify new talk")
def notify_new_talk(sender, instance, **kwargs):
# Subscribe reviewer for these topics to the conversation
topics = instance.topics.all()
reviewers = User.objects.filter(participation__review_topics=topics).all()
instance.conversation.subscribers.add(*reviewers)
# Notification of this new talk
message = Message(conversation=instance.conversation, author=instance.proposer,
content='The talk has been proposed.')
message.save()
2016-06-12 21:39:04 +00:00
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)