2017-08-01 23:45:38 +00:00
|
|
|
from django.db.models.signals import pre_save, post_save
|
2017-05-30 20:27:45 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.conf import settings
|
2017-08-01 23:45:38 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-08-02 10:02:57 +00:00
|
|
|
from django.contrib.auth.models import User
|
2017-08-11 21:25:42 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2017-11-30 19:34:12 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-05-30 20:27:45 +00:00
|
|
|
|
2017-07-30 16:01:41 +00:00
|
|
|
from ponyconf.decorators import disable_for_loaddata
|
2017-08-01 23:45:38 +00:00
|
|
|
from mailing.models import MessageThread, Message
|
2017-11-30 19:34:12 +00:00
|
|
|
from mailing.utils import send_message
|
2017-10-05 23:33:57 +00:00
|
|
|
from .models import Participant, Talk, Conference, Volunteer
|
2017-05-30 20:27:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Site, dispatch_uid="Create Conference for Site")
|
|
|
|
@disable_for_loaddata
|
|
|
|
def create_conference(sender, instance, **kwargs):
|
2017-07-30 16:01:41 +00:00
|
|
|
conference, created = Conference.objects.get_or_create(site=instance)
|
2017-05-30 20:27:45 +00:00
|
|
|
|
|
|
|
|
2017-08-01 23:45:38 +00:00
|
|
|
def create_conversation(sender, instance, **kwargs):
|
|
|
|
if not hasattr(instance, 'conversation'):
|
|
|
|
instance.conversation = MessageThread.objects.create()
|
|
|
|
pre_save.connect(create_conversation, sender=Participant)
|
|
|
|
pre_save.connect(create_conversation, sender=Talk)
|
2017-10-05 23:33:57 +00:00
|
|
|
pre_save.connect(create_conversation, sender=Volunteer)
|
2017-08-01 23:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Message, dispatch_uid="Send message notifications")
|
2017-08-11 20:25:16 +00:00
|
|
|
def send_message_notifications(sender, instance, **kwargs):
|
2017-08-01 23:45:38 +00:00
|
|
|
message = instance
|
2017-11-30 19:34:12 +00:00
|
|
|
author = message.author.author
|
2017-08-01 23:45:38 +00:00
|
|
|
thread = message.thread
|
2017-11-30 19:34:12 +00:00
|
|
|
if message.in_reply_to:
|
|
|
|
reference = message.in_reply_to.token
|
2017-08-01 23:45:38 +00:00
|
|
|
else:
|
2017-11-30 19:34:12 +00:00
|
|
|
reference = None
|
2017-08-01 23:45:38 +00:00
|
|
|
if hasattr(thread, 'participant'):
|
|
|
|
conf = thread.participant.site.conference
|
|
|
|
elif hasattr(thread, 'talk'):
|
|
|
|
conf = thread.talk.site.conference
|
2017-11-30 19:34:12 +00:00
|
|
|
elif hasattr(thread, 'volunteer'):
|
|
|
|
conf = thread.volunteer.site.conference
|
2017-08-01 23:45:38 +00:00
|
|
|
message_id = '<{id}@%s>' % conf.site.domain
|
|
|
|
if conf.reply_email:
|
2017-11-30 19:34:12 +00:00
|
|
|
reply_to = (str(conf), conf.reply_email)
|
2017-08-01 23:45:38 +00:00
|
|
|
else:
|
|
|
|
reply_to = None
|
2017-11-30 19:34:12 +00:00
|
|
|
if type(author) == get_user_model():
|
|
|
|
sender = author.get_full_name()
|
|
|
|
else:
|
|
|
|
sender = str(author)
|
|
|
|
sender = (sender, conf.contact_email)
|
|
|
|
staff_dests = [ (user, user.get_full_name(), user.email) for user in conf.staff.all() ]
|
|
|
|
if hasattr(thread, 'participant') or hasattr(thread, 'volunteer'):
|
|
|
|
if hasattr(thread, 'participant'):
|
|
|
|
user = thread.participant
|
2017-08-01 23:45:38 +00:00
|
|
|
else:
|
2017-11-30 19:34:12 +00:00
|
|
|
user = thread.volunteer
|
|
|
|
dests = [ (user, user.name, user.email) ]
|
|
|
|
if author == user: # message from the user, notify the staff
|
|
|
|
message.send_notification(sender=sender, dests=staff_dests, reply_to=reply_to, message_id=message_id, reference=reference)
|
|
|
|
else: # message to the user, notify the user, and the staff if the message is not a conference notification
|
|
|
|
message.send_notification(sender=sender, dests=dests, reply_to=reply_to, message_id=message_id, reference=reference)
|
|
|
|
if author != conf:
|
|
|
|
message.send_notification(sender=sender, dests=staff_dests, reply_to=reply_to, message_id=message_id, reference=reference)
|
2017-08-01 23:45:38 +00:00
|
|
|
elif hasattr(thread, 'talk'):
|
2017-11-30 19:34:12 +00:00
|
|
|
message.send_notification(sender=sender, dests=staff_dests,
|
|
|
|
reply_to=reply_to, message_id=message_id, reference=reference)
|
2017-08-01 23:45:38 +00:00
|
|
|
|
|
|
|
|
2017-07-30 16:01:41 +00:00
|
|
|
# connected in apps.py
|
2017-05-30 20:27:45 +00:00
|
|
|
def call_first_site_post_save(apps, **kwargs):
|
|
|
|
try:
|
|
|
|
site = Site.objects.get(id=getattr(settings, 'SITE_ID', 1))
|
|
|
|
except Site.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
site.save()
|