mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
DjrillMessage class fixes
* Don't crash if no tags * Allow `None` to omit options entirely from Mandrill send call * Default `preserve_recipients` to None (= use setting from Mandrill account) * ImproperlyConfigured --> ValueError for bad tags
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
|
||||
|
||||
@@ -8,23 +7,28 @@ class DjrillMessage(EmailMultiAlternatives):
|
||||
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
|
||||
connection=None, attachments=None, headers=None, alternatives=None,
|
||||
cc=None, from_name=None, tags=None, track_opens=True,
|
||||
track_clicks=True, preserve_recipients=False):
|
||||
track_clicks=True, preserve_recipients=None):
|
||||
|
||||
super(DjrillMessage, self).__init__(subject, body, from_email, to, bcc,
|
||||
connection, attachments, headers, alternatives, cc)
|
||||
|
||||
self.from_name = from_name
|
||||
self.tags = self._set_mandrill_tags(tags)
|
||||
self.track_opens = track_opens
|
||||
self.track_clicks = track_clicks
|
||||
self.preserve_recipients = preserve_recipients
|
||||
if from_name:
|
||||
self.from_name = from_name
|
||||
if tags:
|
||||
self.tags = self._set_mandrill_tags(tags)
|
||||
if track_opens is not None:
|
||||
self.track_opens = track_opens
|
||||
if track_clicks is not None:
|
||||
self.track_clicks = track_clicks
|
||||
if preserve_recipients is not None:
|
||||
self.preserve_recipients = preserve_recipients
|
||||
|
||||
def _set_mandrill_tags(self, tags):
|
||||
"""
|
||||
Check that all tags are below 50 chars and that they do not start
|
||||
with an underscore.
|
||||
|
||||
Raise ImproperlyConfigured if an underscore tag is passed in to
|
||||
Raise ValueError if an underscore tag is passed in to
|
||||
alert the user. Any tag over 50 chars is left out of the list.
|
||||
"""
|
||||
tag_list = []
|
||||
@@ -33,8 +37,8 @@ class DjrillMessage(EmailMultiAlternatives):
|
||||
if len(tag) <= 50 and not tag.startswith("_"):
|
||||
tag_list.append(tag)
|
||||
elif tag.startswith("_"):
|
||||
raise ImproperlyConfigured(
|
||||
raise ValueError(
|
||||
"Tags starting with an underscore are reserved for "
|
||||
"internal use and will cause errors with Mandill's API")
|
||||
"internal use and will cause errors with Mandrill's API")
|
||||
|
||||
return tag_list
|
||||
|
||||
@@ -392,7 +392,7 @@ class DjrillMessageTests(TestCase):
|
||||
self.assertEqual(msg.alternatives[0][0], self.html_content)
|
||||
|
||||
def test_djrill_message_tag_failure(self):
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
with self.assertRaises(ValueError):
|
||||
DjrillMessage(self.subject, self.text_content, self.from_email,
|
||||
self.to, tags=["_fail"])
|
||||
|
||||
@@ -409,3 +409,15 @@ class DjrillMessageTests(TestCase):
|
||||
self.assertIn(tags[0], msg.tags)
|
||||
self.assertIn(tags[1], msg.tags)
|
||||
self.assertNotIn(tags[2], msg.tags)
|
||||
|
||||
def test_djrill_message_no_options(self):
|
||||
"""DjrillMessage with only basic EmailMessage options should work"""
|
||||
msg = DjrillMessage(self.subject, self.text_content,
|
||||
self.from_email, self.to) # no Mandrill-specific options
|
||||
|
||||
self.assertIsInstance(msg, DjrillMessage)
|
||||
self.assertEqual(msg.body, self.text_content)
|
||||
self.assertEqual(msg.recipients(), self.to)
|
||||
self.assertFalse(hasattr(msg, 'tags'))
|
||||
self.assertFalse(hasattr(msg, 'from_name'))
|
||||
self.assertFalse(hasattr(msg, 'preserve_recipients'))
|
||||
|
||||
Reference in New Issue
Block a user