diff options
-rw-r--r-- | askbot/templates/group_messaging/email_alert.html | 13 | ||||
-rw-r--r-- | group_messaging/models.py | 49 | ||||
-rw-r--r-- | group_messaging/tests.py | 7 |
3 files changed, 69 insertions, 0 deletions
diff --git a/askbot/templates/group_messaging/email_alert.html b/askbot/templates/group_messaging/email_alert.html new file mode 100644 index 00000000..90fea52b --- /dev/null +++ b/askbot/templates/group_messaging/email_alert.html @@ -0,0 +1,13 @@ +{% extends "email/base_mail.html"%} +{% from "email/macros.html" import start_quote, end_quote %} +{% set level = 0 %} +{% for message in messages %} + {{ start_quote(level) }} + <p>{% trans author=message.sender %}{{ author }} wrote:{% endtrans %}</p> + {{ message.html|escape }} + {{ end_quote(level) }} + {% set level = level + 1 %} +{% endfor %} +{% block footer %} +{% include "email/footer.html" %} +{% endblock %} diff --git a/group_messaging/models.py b/group_messaging/models.py index d84b202f..12fe3620 100644 --- a/group_messaging/models.py +++ b/group_messaging/models.py @@ -1,12 +1,16 @@ """models for the ``group_messaging`` app """ import datetime +from coffin.template.loader import get_template from django.db import models +from django.db.models import signals from django.contrib.auth.models import Group from django.contrib.auth.models import User +from django.utils.translation import ugettext as _ MAX_HEADLINE_LENGTH = 80 MAX_SENDERS_INFO_LENGTH = 64 +MAX_SUBJECT_LINE_LENGTH = 30 #dummy parse message function parse_message = lambda v: v @@ -167,6 +171,7 @@ class MessageManager(models.Manager): text=text, ) message.add_recipients(recipients) + message.send_email_alert() return message def create_response(self, sender=None, text=None, parent=None): @@ -192,6 +197,7 @@ class MessageManager(models.Manager): message.root.update_senders_info() #unarchive the thread for all recipients message.root.unarchive() + message.send_email_alert() return message @@ -261,6 +267,49 @@ class Message(models.Model): sender_list, created = SenderList.objects.get_or_create(recipient=recipient) sender_list.senders.add(self.sender) + def get_email_subject_line(self): + """forms subject line based on the root message + and prepends 'Re': if message is non-root + """ + subject = self.get_root_message().text[:MAX_SUBJECT_LINE_LENGTH] + if self.root: + subject = _('Re: ') + subject + return subject + + def get_root_message(self): + """returns root message or self + if current message is root + """ + return self.root or self + + def get_recipients_users(self): + """returns query set of users""" + groups = self.recipients.all() + return User.objects.filter( + groups__in=groups + ).exclude( + id=self.sender.id + ).distinct() + + def get_timeline(self): + """returns ordered query set of messages in the thread + with the newest first""" + root = self.get_root_message() + root_qs = Message.objects.filter(id=root.id) + return (root.descendants.all() | root_qs).order_by('-sent_at') + + + def send_email_alert(self): + """signal handler for the message post-save""" + subject = self.get_email_subject_line() + template = get_template('group_messaging/email_alert.html') + data = {'messages': self.get_timeline()} + body_text = template.render(data) + recipients = map(lambda v: v.email, self.get_recipients_users()) + from askbot.mail import send_mail + send_mail(recipient_list=recipients, subject_line=subject, body_text=body_text) + + def update_senders_info(self): """update the contributors info, meant to be used on a root message only diff --git a/group_messaging/tests.py b/group_messaging/tests.py index badbbb5a..9cc69fb8 100644 --- a/group_messaging/tests.py +++ b/group_messaging/tests.py @@ -266,3 +266,10 @@ class ModelTests(TestCase): parent=root ) self.assertEqual(root.headline, 'some response') + + def test_email_alert_sent(self): + root = self.create_thread_for_user(self.recipient) + from django.core.mail import outbox + self.assertEqual(len(outbox), 1) + self.assertEqual(len(outbox[0].recipients()), 1) + self.assertEqual(outbox[0].recipients()[0], self.recipient.email) |