Skip to content

Commit f39ccfd

Browse files
committed
Postmark templates feature refactor
1 parent 6970b51 commit f39ccfd

3 files changed

Lines changed: 30 additions & 42 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ Using templates in python-postmark is straightforward:
8888

8989
3. Make a dict in python that contains the values of the template variables. Postmark calls this the "TemplateModel"
9090

91-
4. Instantiate PMMail with the the appropriate kwargs, including "template_id" and "template_model". NOTE: do not set the "subject" kwarg.
91+
4. Instantiate PMMail with the the appropriate kwargs, including "template_id" and "template_model". NOTE: "subject" kwarg is ignored.
9292

93-
5. Call the send_with_template() method
93+
5. Call the send() method
9494

9595
Here is an example:
9696

@@ -118,7 +118,7 @@ template_model = {
118118
}
119119
pm = PMMail(to='[email protected]', sender='[email protected]',
120120
template_id=123456, template_model=template_model)
121-
pm.send_with_template()
121+
pm.send()
122122
```
123123

124124

postmark/core.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self, **kwargs):
6464
bcc: Who to blind copy the email to, in either
6565
"[email protected]" or "First Last <[email protected]>" format
6666
Can be multiple values separated by commas (limit 20)
67-
subject: Subject of the email. ***DO NOT SET if using Postmark templates
67+
subject: Subject of the email, ignored if using Postmark templates
6868
tag: Use for adding categorizations to your email
6969
html_body: Email message in HTML
7070
text_body: Email message in plain text
@@ -328,7 +328,7 @@ def _set_attachments(self, value):
328328
#####################
329329

330330

331-
def _check_values(self, endpoint='email'):
331+
def _check_values(self):
332332
'''
333333
Make sure all values are of the appropriate
334334
type and are not missing.
@@ -339,13 +339,11 @@ def _check_values(self, endpoint='email'):
339339
raise PMMailMissingValueException('Cannot send an e-mail without a sender (.sender field)')
340340
elif not self.__to:
341341
raise PMMailMissingValueException('Cannot send an e-mail without at least one recipient (.to field)')
342-
elif endpoint == 'email/withTemplate/' and (not self.__template_id or not self.__template_model):
342+
elif (self.__template_id or self.__template_model) and not all([self.__template_id, self.__template_model]):
343343
raise PMMailMissingValueException(
344344
'Cannot send a template e-mail without a both template_id and template_model set')
345-
elif not self.__subject and endpoint == 'email':
345+
elif not any([self.__template_id, self.__template_model, self.__subject]):
346346
raise PMMailMissingValueException('Cannot send an e-mail without a subject')
347-
elif self.__template_id and self.__subject:
348-
raise PMMailMissingValueException('If using Postmark templates, do not set the subject value')
349347
elif not self.__html_body and not self.__text_body and not self.__template_id:
350348
raise PMMailMissingValueException('Cannot send an e-mail without either an HTML or text version of your e-mail body')
351349
if self.__track_opens and not self.__html_body:
@@ -429,16 +427,13 @@ def to_json_message(self):
429427

430428
return json_message
431429

432-
def send(self, endpoint='email', test=None):
430+
def send(self, test=None):
433431
'''
434432
Send the email through the Postmark system.
435433
Pass test=True to just print out the resulting
436434
JSON message being sent to Postmark
437-
438-
endpoint: the name of the Postmark API endpoints
439-
440435
'''
441-
self._check_values(endpoint=endpoint)
436+
self._check_values()
442437

443438
# Set up message dictionary
444439
json_message = self.to_json_message()
@@ -460,9 +455,14 @@ def send(self, endpoint='email', test=None):
460455
print('JSON message is:\n%s' % json.dumps(json_message, cls=PMJSONEncoder))
461456
return
462457

458+
if self.__template_id:
459+
endpoint_url = __POSTMARK_URL__ + 'email/withTemplate/'
460+
else:
461+
endpoint_url = __POSTMARK_URL__ + 'email'
462+
463463
# Set up the url Request
464464
req = Request(
465-
__POSTMARK_URL__ + endpoint,
465+
endpoint_url,
466466
json.dumps(json_message, cls=PMJSONEncoder).encode('utf8'),
467467
{
468468
'Accept': 'application/json',
@@ -509,9 +509,6 @@ def send(self, endpoint='email', test=None):
509509
else:
510510
raise PMMailURLException('URLError: The server couldn\'t fufill the request. (See "inner_exception" for details)', err)
511511

512-
def send_with_template(self, **kwargs):
513-
self.send(endpoint='email/withTemplate/', **kwargs)
514-
515512

516513
# Simple utility that returns a generator to chunk up a list into equal parts
517514
def _chunks(l, n):

tests.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ def assert_missing_value_exception(self, message_func, error_message):
6060
message_func()
6161
self.assertEqual(error_message, cm.exception.parameter)
6262

63-
def test_send_with_template(self):
64-
# Confirm send() still works as before send_with_template() was added
63+
def test_send(self):
64+
# Confirm send() still works as before use_template was added
6565
message = PMMail(sender='[email protected]', to='[email protected]',
6666
subject='Subject', text_body='Body', api_key='test')
6767

6868
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
6969
200, '', {}, None)):
70-
self.assertTrue(message.send)
70+
message.send()
7171

72+
def test_missing_subject(self):
7273
# No subject should raise exception when using send()
7374
message = PMMail(sender='[email protected]', to='[email protected]',
7475
text_body='Body', api_key='test')
@@ -77,31 +78,21 @@ def test_send_with_template(self):
7778
'Cannot send an e-mail without a subject'
7879
)
7980

80-
# Test new _check_values()
81-
# Try sending with template without a template ID or template model
82-
for kwargs in [{'subject': 'Subject', 'text_body': 'Body'},
83-
{'template_id': 1},
84-
{'template_model': {'junk': 'more junk'}}]:
85-
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]', **kwargs)
86-
self.assert_missing_value_exception(
87-
message.send_with_template,
88-
'Cannot send a template e-mail without a both template_id and template_model set'
89-
)
90-
91-
# Both template_id and template_model are set, so send_with_template should work.
81+
def test_check_values_bad_template_data(self):
82+
# Try sending with template ID only
83+
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]', template_id=1)
84+
self.assert_missing_value_exception(
85+
message.send,
86+
'Cannot send a template e-mail without a both template_id and template_model set'
87+
)
88+
89+
def test_send_with_template(self):
90+
# Both template_id and template_model are set, so send should work.
9291
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]',
9392
template_id=1, template_model={'junk': 'more junk'})
9493
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
9594
200, '', {}, None)):
96-
self.assertTrue(message.send_with_template)
97-
98-
# Setting a subject with a template should raise an error
99-
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]',
100-
template_id=1, template_model={'junk': 'more junk'}, subject='Subject')
101-
self.assert_missing_value_exception(
102-
message.send_with_template,
103-
'If using Postmark templates, do not set the subject value'
104-
)
95+
message.send()
10596

10697
def test_inline_attachments(self):
10798
image = MIMEImage(b'image_file', 'png', name='image.png')

0 commit comments

Comments
 (0)