From 565a353375dd961a8b0a54c6efd06a8fd7942ea7 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sun, 21 Aug 2016 15:28:22 +1000 Subject: [PATCH 1/4] send_mail is no longer hardwired to point at symposion/emails (who does that?!) --- symposion/utils/mail.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/symposion/utils/mail.py b/symposion/utils/mail.py index 8789e044..4204b4b6 100644 --- a/symposion/utils/mail.py +++ b/symposion/utils/mail.py @@ -1,3 +1,5 @@ +import os + from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string @@ -6,7 +8,30 @@ from django.utils.html import strip_tags from django.contrib.sites.models import Site -def send_email(to, kind, **kwargs): +def sender(template_prefix): + ''' Creates a function called `send_email` ''' + + def send_email(to, kind, **kwargs): + ''' Sends an e-mail to the given address. + + to: The address + kind: the ID for an e-mail kind; it should point to a subdirectory of + %(template_prefix)s containing subject.txt and message.html, which + are django templates for the subject and HTML message respectively. + + context: a context for rendering the e-mail. + + ''' % {"template_prefix": template_prefix} + + return __send_email__(template_prefix, to, kind, **kwargs) + + return send_email + + +send_email = sender("symposion/emails") + + +def __send_email__(template_prefix, to, kind, **kwargs): current_site = Site.objects.get_current() @@ -15,16 +40,22 @@ def send_email(to, kind, **kwargs): "STATIC_URL": settings.STATIC_URL, } ctx.update(kwargs.get("context", {})) + subject_template = os.path.join(template_prefix, "%s/subject.txt" % kind) + message_template = os.path.join(template_prefix, "%s/message.html" % kind) subject = "[%s] %s" % ( current_site.name, - render_to_string("symposion/emails/%s/subject.txt" % kind, ctx).strip() + render_to_string(subject_template, ctx).strip() ) - message_html = render_to_string("symposion/emails/%s/message.html" % kind, ctx) + message_html = render_to_string(message_template, ctx) message_plaintext = strip_tags(message_html) from_email = settings.DEFAULT_FROM_EMAIL - bcc_email = settings.ENVELOPE_BCC_LIST + + try: + bcc_email = settings.ENVELOPE_BCC_LIST + except AttributeError: + bcc_email = None email = EmailMultiAlternatives(subject, message_plaintext, from_email, to, bcc=bcc_email) email.attach_alternative(message_html, "text/html") From b6b6c51cc17293d453210110380591885278adbb Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sun, 21 Aug 2016 15:31:09 +1000 Subject: [PATCH 2/4] Refactors to be a bit less obtuse --- symposion/utils/mail.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/symposion/utils/mail.py b/symposion/utils/mail.py index 4204b4b6..2ba691b6 100644 --- a/symposion/utils/mail.py +++ b/symposion/utils/mail.py @@ -8,27 +8,28 @@ from django.utils.html import strip_tags from django.contrib.sites.models import Site -def sender(template_prefix): - ''' Creates a function called `send_email` ''' +class Sender(object): + ''' Class for sending e-mails under a templete prefix. ''' - def send_email(to, kind, **kwargs): + def __init__(self, template_prefix): + self.template_prefix = template_prefix + + def send_email(self, to, kind, **kwargs): ''' Sends an e-mail to the given address. to: The address kind: the ID for an e-mail kind; it should point to a subdirectory of - %(template_prefix)s containing subject.txt and message.html, which + self.template_prefix containing subject.txt and message.html, which are django templates for the subject and HTML message respectively. context: a context for rendering the e-mail. - ''' % {"template_prefix": template_prefix} + ''' - return __send_email__(template_prefix, to, kind, **kwargs) - - return send_email + return __send_email__(self.template_prefix, to, kind, **kwargs) -send_email = sender("symposion/emails") +send_email = Sender("symposion/emails").send_email def __send_email__(template_prefix, to, kind, **kwargs): From d54d47487eccaa14a6e19ead404d3da1990c5951 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sun, 21 Aug 2016 15:28:22 +1000 Subject: [PATCH 3/4] send_mail is no longer hardwired to point at symposion/emails (who does that?!) --- symposion/utils/mail.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/symposion/utils/mail.py b/symposion/utils/mail.py index e9759d09..858e637e 100644 --- a/symposion/utils/mail.py +++ b/symposion/utils/mail.py @@ -1,3 +1,5 @@ +import os + from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string @@ -6,7 +8,30 @@ from django.utils.html import strip_tags from django.contrib.sites.models import Site -def send_email(to, kind, **kwargs): +def sender(template_prefix): + ''' Creates a function called `send_email` ''' + + def send_email(to, kind, **kwargs): + ''' Sends an e-mail to the given address. + + to: The address + kind: the ID for an e-mail kind; it should point to a subdirectory of + %(template_prefix)s containing subject.txt and message.html, which + are django templates for the subject and HTML message respectively. + + context: a context for rendering the e-mail. + + ''' % {"template_prefix": template_prefix} + + return __send_email__(template_prefix, to, kind, **kwargs) + + return send_email + + +send_email = sender("symposion/emails") + + +def __send_email__(template_prefix, to, kind, **kwargs): current_site = Site.objects.get_current() @@ -15,16 +40,23 @@ def send_email(to, kind, **kwargs): "STATIC_URL": settings.STATIC_URL, } ctx.update(kwargs.get("context", {})) + subject_template = os.path.join(template_prefix, "%s/subject.txt" % kind) + message_template = os.path.join(template_prefix, "%s/message.html" % kind) subject = "[%s] %s" % ( current_site.name, - render_to_string("symposion/emails/%s/subject.txt" % kind, ctx).strip() + render_to_string(subject_template, ctx).strip() ) - message_html = render_to_string("symposion/emails/%s/message.html" % kind, ctx) + message_html = render_to_string(message_template, ctx) message_plaintext = strip_tags(message_html) from_email = settings.DEFAULT_FROM_EMAIL + try: + bcc_email = settings.ENVELOPE_BCC_LIST + except AttributeError: + bcc_email = None + email = EmailMultiAlternatives(subject, message_plaintext, from_email, to) email.attach_alternative(message_html, "text/html") email.send() From 96683b6d7dba9187845331db3c88c1e0eb032f7a Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sun, 21 Aug 2016 15:31:09 +1000 Subject: [PATCH 4/4] Refactors to be a bit less obtuse --- symposion/utils/mail.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/symposion/utils/mail.py b/symposion/utils/mail.py index 858e637e..b301c4bb 100644 --- a/symposion/utils/mail.py +++ b/symposion/utils/mail.py @@ -8,27 +8,28 @@ from django.utils.html import strip_tags from django.contrib.sites.models import Site -def sender(template_prefix): - ''' Creates a function called `send_email` ''' +class Sender(object): + ''' Class for sending e-mails under a templete prefix. ''' - def send_email(to, kind, **kwargs): + def __init__(self, template_prefix): + self.template_prefix = template_prefix + + def send_email(self, to, kind, **kwargs): ''' Sends an e-mail to the given address. to: The address kind: the ID for an e-mail kind; it should point to a subdirectory of - %(template_prefix)s containing subject.txt and message.html, which + self.template_prefix containing subject.txt and message.html, which are django templates for the subject and HTML message respectively. context: a context for rendering the e-mail. - ''' % {"template_prefix": template_prefix} + ''' - return __send_email__(template_prefix, to, kind, **kwargs) - - return send_email + return __send_email__(self.template_prefix, to, kind, **kwargs) -send_email = sender("symposion/emails") +send_email = Sender("symposion/emails").send_email def __send_email__(template_prefix, to, kind, **kwargs):