From 9ed5ce0213cdb1471483f8a4ea34ebfa8c1ef1ee Mon Sep 17 00:00:00 2001 From: medmunds Date: Mon, 22 Feb 2021 17:46:39 -0800 Subject: [PATCH] Postmark: Fix API error with template but no merge data Fixes #223 --- CHANGELOG.rst | 15 ++++++++++++++- anymail/backends/postmark.py | 4 ++++ tests/test_postmark_backend.py | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8269d59..e59e2a5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,6 +25,18 @@ Release history ^^^^^^^^^^^^^^^ .. This extra heading level keeps the ToC from becoming unmanageably long +vNext +----- + +*unreleased changes* + +Fixes +~~~~~ + +* **Postmark:** Fix Postmark API error when sending with a template that doesn't + require any merge data. (Thanks to `@Tobeyforce`_ for reporting it.) + + v8.2 ----- @@ -1225,8 +1237,9 @@ Features .. _@sebbacon: https://github.com/sebbacon .. _@slinkymanbyday: https://github.com/slinkymanbyday .. _@swrobel: https://github.com/swrobel -.. _@Thorbenl: https://github.com/Thorbenl .. _@tcourtqtm: https://github.com/tcourtqtm +.. _@Thorbenl: https://github.com/Thorbenl +.. _@Tobeyforce: https://github.com/Tobeyforce .. _@varche1: https://github.com/varche1 .. _@vgrebenschikov: https://github.com/vgrebenschikov .. _@yourcelf: https://github.com/yourcelf diff --git a/anymail/backends/postmark.py b/anymail/backends/postmark.py index 3061451..9ca82a4 100644 --- a/anymail/backends/postmark.py +++ b/anymail/backends/postmark.py @@ -301,6 +301,10 @@ class PostmarkPayload(RequestsPayload): except ValueError: self.data["TemplateAlias"] = template_id + # Postmark requires TemplateModel (empty ok) when TemplateId/TemplateAlias + # specified. (This may get overwritten by a real TemplateModel later.) + self.data.setdefault("TemplateModel", {}) + # Subject, TextBody, and HtmlBody aren't allowed with TemplateId; # delete Django default subject and body empty strings: for field in ("Subject", "TextBody", "HtmlBody"): diff --git a/tests/test_postmark_backend.py b/tests/test_postmark_backend.py index 46a00a2..5f10921 100644 --- a/tests/test_postmark_backend.py +++ b/tests/test_postmark_backend.py @@ -373,7 +373,6 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase): # Omit subject and body (Postmark prohibits them with templates) from_email='from@example.com', to=['to@example.com'], template_id=1234567, - # Postmark doesn't support per-recipient merge_data merge_global_data={'name': "Alice", 'group': "Developers"}, ) message.send() @@ -396,6 +395,8 @@ class PostmarkBackendAnymailFeatureTests(PostmarkBackendMockAPITestCase): self.assert_esp_called('/email/withTemplate/') data = self.get_api_call_json() self.assertEqual(data['TemplateAlias'], 'welcome-message') + # Postmark requires TemplateModel (can be empty) with TemplateId/TemplateAlias + self.assertEqual(data['TemplateModel'], {}) _mock_batch_response = json.dumps([{ "ErrorCode": 0,