Skip to content

Commit a47e0bf

Browse files
author
m-ewura
committed
Add optional return_json flag with Django/global fallback
1 parent c5179a0 commit a47e0bf

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

postmark/core.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def to_json_message(self):
504504

505505
return json_message
506506

507-
def send(self, test=None):
507+
def send(self, test=None, return_json=None):
508508
'''
509509
Send the email through the Postmark system.
510510
Pass test=True to just print out the resulting
@@ -537,6 +537,21 @@ def send(self, test=None):
537537
else:
538538
endpoint_url = __POSTMARK_URL__ + 'email'
539539

540+
"""
541+
Args:
542+
return_json (bool | None):
543+
True -> return parsed JSON
544+
False -> return True/False
545+
None -> fallback to settings.POSTMARK_RETURN_JSON (default False)
546+
"""
547+
548+
if return_json is None:
549+
try:
550+
from django.conf import settings as django_settings
551+
return_json = getattr(django_settings, "POSTMARK_RETURN_JSON", False)
552+
except ImportError:
553+
return_json = False
554+
540555
# Set up the url Request
541556
req = Request(
542557
endpoint_url,
@@ -558,7 +573,7 @@ def send(self, test=None):
558573
if result.code == 200:
559574
parsed = json.loads(jsontxt)
560575
self.message_id = parsed.get("MessageID", None)
561-
return parsed
576+
return parsed if return_json else True
562577
else:
563578
raise PMMailSendException('Return code %d: %s' % (result.code, result.msg))
564579
except HTTPError as err:
@@ -677,7 +692,7 @@ def _check_values(self):
677692

678693
message._check_values()
679694

680-
def send(self, test=None):
695+
def send(self, test=None, return_json=None):
681696
# Has one of the messages caused an inactive recipient error?
682697
inactive_recipient = False
683698

@@ -692,6 +707,21 @@ def send(self, test=None):
692707
except ImportError:
693708
pass
694709

710+
"""
711+
Args:
712+
return_json (bool | None):
713+
True -> return parsed JSON
714+
False -> return True/False
715+
None -> fallback to settings.POSTMARK_RETURN_JSON (default False)
716+
"""
717+
718+
if return_json is None:
719+
try:
720+
from django.conf import settings as django_settings
721+
return_json = getattr(django_settings, "POSTMARK_RETURN_JSON", False)
722+
except ImportError:
723+
return_json = False
724+
695725
# Split up into groups of 500 messages for sending
696726
for messages in _chunks(self.messages, PMBatchMail.MAX_MESSAGES):
697727
json_message = []
@@ -730,7 +760,7 @@ def send(self, test=None):
730760
results = json.loads(jsontxt)
731761
for i, res in enumerate(results):
732762
self.__messages[i].message_id = res.get("MessageID", None)
733-
return results
763+
return results if return_json else True
734764
else:
735765
raise PMMailSendException('Return code %d: %s' % (result.code, result.msg))
736766
except HTTPError as err:

0 commit comments

Comments
 (0)