improve mailing system senders handling

This commit is contained in:
Élie Bouttier 2017-08-02 12:02:57 +02:00
parent b1667592ba
commit 13b61f4266
7 changed files with 110 additions and 15 deletions

View File

@ -3,6 +3,7 @@ from django.dispatch import receiver
from django.contrib.sites.models import Site
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from ponyconf.decorators import disable_for_loaddata
from mailing.models import MessageThread, Message
@ -22,6 +23,33 @@ pre_save.connect(create_conversation, sender=Participant)
pre_save.connect(create_conversation, sender=Talk)
@receiver(pre_save, sender=Message, dispatch_uid="Set message author")
def set_message_author(sender, instance, **kwargs):
message = instance
if message.author is None:
# Try users
try:
instance.author = User.objects.get(email=message.from_email)
except User.DoesNotExist:
pass
else:
return
# Try participants
try:
instance.author = Participant.objects.get(email=message.from_email)
except User.DoesNotExist:
pass
else:
return
# Try conferences
try:
instance.author = Conference.objects.get(contact_email=message.from_email)
except Conference.DoesNotExist:
pass
else:
return
@receiver(post_save, sender=Message, dispatch_uid="Send message notifications")
def send_message_notification(sender, instance, **kwargs):
message = instance
@ -41,7 +69,7 @@ def send_message_notification(sender, instance, **kwargs):
reply_to = (conf.name, conf.reply_email)
else:
reply_to = None
sender = (conf.name, conf.contact_email)
sender = (message.author_display, conf.contact_email)
staff_dests = [ (user.get_full_name(), user.email) for user in conf.staff.all() ]
if hasattr(thread, 'participant'):
conf = thread.participant.site.conference
@ -49,18 +77,22 @@ def send_message_notification(sender, instance, **kwargs):
participant_dests = [ (participant.name, participant.email) ]
participant_subject = _('[%(prefix)s] Message from the staff') % {'prefix': conf.name}
staff_subject = _('[%(prefix)s] Conversation with %(dest)s') % {'prefix': conf.name, 'dest': participant.name}
if message.author == conf.contact_email: # this is a talk notification message
if message.from_email == conf.contact_email: # this is a talk notification message
# sent it only the participant
message.send_notification(subject=participant_subject, sender=sender, dests=participant_dests, reply_to=reply_to, message_id=message_id, reference=reference)
message.send_notification(subject=subject_prefix+participant_subject, sender=sender, dests=participant_dests,
reply_to=reply_to, message_id=message_id, reference=reference)
else:
# this is a message between the staff and the participant
message.send_notification(subject=staff_subject, sender=sender, dests=staff_dests, reply_to=reply_to, message_id=message_id, reference=reference)
if message.author != thread.participant.email: # message from staff: sent it to the participant too
message.send_notification(subject=participant_subject, sender=sender, dests=participant_dests, reply_to=reply_to, message_id=message_id, reference=reference)
message.send_notification(subject=subject_prefix+staff_subject, sender=sender, dests=staff_dests,
reply_to=reply_to, message_id=message_id, reference=reference)
if message.from_email != thread.participant.email: # message from staff: sent it to the participant too
message.send_notification(subject=subject_prefix+participant_subject, sender=sender, dests=participant_dests,
reply_to=reply_to, message_id=message_id, reference=reference)
elif hasattr(thread, 'talk'):
conf = thread.talk.site.conference
subject = _('[%(prefix)s] Talk: %(talk)s') % {'prefix': conf.name, 'talk': thread.talk.title}
message.send_notification(subject=subject, sender=sender, dests=staff_dests, reply_to=reply_to, message_id=message_id, reference=reference)
message.send_notification(subject=subject_prefix+subject, sender=sender, dests=staff_dests,
reply_to=reply_to, message_id=message_id, reference=reference)
# connected in apps.py

View File

@ -87,7 +87,8 @@ Thanks!
Message.objects.create(
thread=participant.conversation,
author=conference.contact_email,
author=conference,
from_email=conference.contact_email,
content=body,
)
@ -209,7 +210,8 @@ def talk_details(request, conference, talk_id):
message_form = MessageForm(request.POST or None)
if request.method == 'POST' and message_form.is_valid():
message = message_form.save(commit=False)
message.author = request.user.email
message.author = request.user
message.from_email = request.user.email
message.thread = talk.conversation
message.save()
messages.success(request, _('Message sent!'))
@ -270,7 +272,8 @@ def participant_details(request, conference, participant_id):
message_form = MessageForm(request.POST or None)
if request.method == 'POST' and message_form.is_valid():
message = message_form.save(commit=False)
message.author = request.user.email
message.author = request.user
message.from_email = request.user.email
message.thread = participant.conversation
message.save()
messages.success(request, _('Message sent!'))

6
mailing/admin.py Normal file
View File

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Message
admin.site.register(Message)

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-08-02 09:27
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('mailing', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='message',
old_name='author',
new_name='from_email',
),
migrations.AlterField(
model_name='message',
name='from_email',
field=models.EmailField(max_length=254),
),
migrations.AddField(
model_name='message',
name='author_id',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='message',
name='author_type',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
),
]

View File

@ -2,6 +2,10 @@ from django.db import models
from django.utils.crypto import get_random_string
from django.core.mail import EmailMessage, get_connection
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
import hashlib
@ -31,7 +35,10 @@ class MessageThread(models.Model):
class Message(models.Model):
created = models.DateTimeField(auto_now_add=True)
thread = models.ForeignKey(MessageThread)
author = models.EmailField(blank=True)
author_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
author_id = models.PositiveIntegerField(null=True, blank=True)
author = GenericForeignKey('author_type', 'author_id')
from_email = models.EmailField()
content = models.TextField(blank=True)
token = models.CharField(max_length=64, default=generate_message_token, unique=True)
@ -69,5 +76,15 @@ class Message(models.Model):
connection = get_connection()
connection.send_messages(messages)
@property
def author_display(self):
if self.author:
if type(self.author) == User:
return self.author.get_full_name()
else:
return str(self.author)
else:
return self.from_email
def __str__(self):
return "Message from %s" % self.author
return _("Message from %(author)s") % {'author': self.author_display}

View File

@ -1,9 +1,9 @@
{% load i18n %}
{% for message in messages %}
<div class="panel panel-{% if message.author == participant.email %}info{% else %}default{% endif %}">
<div class="panel panel-{% if message.from_email == participant.email %}info{% else %}default{% endif %}">
<div class="panel-heading">
{{ message.created }} | {{ message.author }}
{{ message.created }} | {{ message.author_display }}
</div>
<div class="panel-body">
{{ message.content|linebreaksbr }}

View File

@ -118,4 +118,4 @@ def process_email(raw_email):
if key != hexdigest_sha256(settings.SECRET_KEY, thread.token, sender.token)[:16]:
raise InvalidKeyException
Message.objects.create(thread=thread, author=sender.email, content=content)
Message.objects.create(thread=thread, from_email=sender.email, content=content)