From c3420d156eb040cbe78bbda2b3c7c647ae724d2b Mon Sep 17 00:00:00 2001 From: "nikolay.saskovets" Date: Fri, 16 May 2014 06:57:58 -0400 Subject: [PATCH 1/3] ability to use default Mandrill subject and from fields --- djrill/mail/backends/djrill.py | 8 ++++++++ docs/usage/templates.rst | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index 18ee6a8..f44ad02 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -87,6 +87,7 @@ class DjrillBackend(BaseEmailBackend): if getattr(message, 'alternatives', None): self._add_alternatives(message, msg_dict) self._add_attachments(message, msg_dict) + self._filter_msg_dict(message, msg_dict) api_params['message'] = msg_dict # check if template is set in message to send it via @@ -310,3 +311,10 @@ class DjrillBackend(BaseEmailBackend): 'content': content_b64.decode('ascii'), } return mandrill_attachment, is_embedded_image + + def _filter_msg_dict(self, message, msg_dict): + if hasattr(message, 'clear_from') and message.clear_from: + msg_dict['from_name'] = '' + msg_dict['from_email'] = '' + if hasattr(message, 'clear_subject') and message.clear_subject: + msg_dict['subject'] = '' diff --git a/docs/usage/templates.rst b/docs/usage/templates.rst index 8b9acfc..74a82ce 100644 --- a/docs/usage/templates.rst +++ b/docs/usage/templates.rst @@ -37,6 +37,26 @@ and will ignore any `body` text set on the `EmailMessage`. All of Djrill's other :ref:`Mandrill-specific options ` can be used with templates. +How To Use Default Mandrill Subject and From fields +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. versionadded:: dev + Mandrill default fields support + +To use default *Mandril* subject or default from field you need send message +to *Mandril* with empty subject or empty from field. This can be done using +the following attrs: :attr:`clear_subject` and :attr:`clear_from` on +your :class:`~django.core.mail.EmailMessage` object:: + # ... + msg.clear_subject = True + msg.clear_from = True + msg.send() + +If :attr:`clear_subject` is set, Djrill will send message without subject and +Mandrill will use default subject. + +If :attr:`clear_from` is set, Djrill will send message without from field and +Mandrill will use default from field. + .. _django-templates: From 0e27a62f3ca1c0baca461468601a5315be8ea996 Mon Sep 17 00:00:00 2001 From: "nikolay.saskovets" Date: Fri, 16 May 2014 07:37:34 -0400 Subject: [PATCH 2/3] prepare for PR to mainstream (tests, docs, ...) --- djrill/mail/backends/djrill.py | 1 + djrill/tests/test_mandrill_send_template.py | 15 +++++++++++++++ docs/usage/templates.rst | 2 -- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index f44ad02..47da10e 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -313,6 +313,7 @@ class DjrillBackend(BaseEmailBackend): return mandrill_attachment, is_embedded_image def _filter_msg_dict(self, message, msg_dict): + """Filter message data (e.g. clear subject field, or from field)""" if hasattr(message, 'clear_from') and message.clear_from: msg_dict['from_name'] = '' msg_dict['from_email'] = '' diff --git a/djrill/tests/test_mandrill_send_template.py b/djrill/tests/test_mandrill_send_template.py index ed987b9..0885241 100644 --- a/djrill/tests/test_mandrill_send_template.py +++ b/djrill/tests/test_mandrill_send_template.py @@ -27,6 +27,21 @@ class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase): 'content': "

Half off all fruit

"} ] ) + def test_send_template_without_from_field(self): + msg = mail.EmailMessage('Subject', 'Text Body', + 'from@example.com', ['to@example.com']) + msg.template_name = "PERSONALIZED_SPECIALS" + msg.clear_from = True + msg.clear_subject = True + msg.send() + self.assert_mandrill_called("/messages/send-template.json") + data = self.get_api_call_data() + print data + self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS") + self.assertEqual(data['message']['subject'], "") + self.assertEqual(data['message']['from_email'], "") + self.assertEqual(data['message']['from_name'], "") + def test_no_template_content(self): # Just a template, without any template_content to be merged msg = mail.EmailMessage('Subject', 'Text Body', diff --git a/docs/usage/templates.rst b/docs/usage/templates.rst index 74a82ce..beac901 100644 --- a/docs/usage/templates.rst +++ b/docs/usage/templates.rst @@ -39,8 +39,6 @@ can be used with templates. How To Use Default Mandrill Subject and From fields ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. versionadded:: dev - Mandrill default fields support To use default *Mandril* subject or default from field you need send message to *Mandril* with empty subject or empty from field. This can be done using From 3eb45824492bf15177a06aac4f9213e186494f15 Mon Sep 17 00:00:00 2001 From: "nikolay.saskovets" Date: Fri, 16 May 2014 07:57:53 -0400 Subject: [PATCH 3/3] fix test --- djrill/tests/test_mandrill_send_template.py | 1 - 1 file changed, 1 deletion(-) diff --git a/djrill/tests/test_mandrill_send_template.py b/djrill/tests/test_mandrill_send_template.py index 0885241..3242259 100644 --- a/djrill/tests/test_mandrill_send_template.py +++ b/djrill/tests/test_mandrill_send_template.py @@ -36,7 +36,6 @@ class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase): msg.send() self.assert_mandrill_called("/messages/send-template.json") data = self.get_api_call_data() - print data self.assertEqual(data['template_name'], "PERSONALIZED_SPECIALS") self.assertEqual(data['message']['subject'], "") self.assertEqual(data['message']['from_email'], "")