Merge branch 'feature/use-mandrill-template-defaults' of git://github.com/nikolay-saskovets/Djrill into nikolay-saskovets-feature/use-mandrill-template-defaults

This commit is contained in:
medmunds
2014-05-28 20:42:36 -07:00
3 changed files with 41 additions and 0 deletions

View File

@@ -87,6 +87,7 @@ class DjrillBackend(BaseEmailBackend):
if getattr(message, 'alternatives', None): if getattr(message, 'alternatives', None):
self._add_alternatives(message, msg_dict) self._add_alternatives(message, msg_dict)
self._add_attachments(message, msg_dict) self._add_attachments(message, msg_dict)
self._filter_msg_dict(message, msg_dict)
api_params['message'] = msg_dict api_params['message'] = msg_dict
# check if template is set in message to send it via # check if template is set in message to send it via
@@ -313,3 +314,11 @@ class DjrillBackend(BaseEmailBackend):
'content': content_b64.decode('ascii'), 'content': content_b64.decode('ascii'),
} }
return mandrill_attachment, is_embedded_image 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'] = ''
if hasattr(message, 'clear_subject') and message.clear_subject:
msg_dict['subject'] = ''

View File

@@ -27,6 +27,20 @@ class DjrillMandrillSendTemplateTests(DjrillBackendMockAPITestCase):
'content': "<p><em>Half off</em> all fruit</p>"} ] 'content': "<p><em>Half off</em> all fruit</p>"} ]
) )
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()
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): def test_no_template_content(self):
# Just a template, without any template_content to be merged # Just a template, without any template_content to be merged
msg = mail.EmailMessage('Subject', 'Text Body', msg = mail.EmailMessage('Subject', 'Text Body',

View File

@@ -37,6 +37,24 @@ and will ignore any `body` text set on the `EmailMessage`.
All of Djrill's other :ref:`Mandrill-specific options <mandrill-send-support>` All of Djrill's other :ref:`Mandrill-specific options <mandrill-send-support>`
can be used with templates. can be used with templates.
How To Use Default Mandrill Subject and From fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: .. _django-templates: