Skip to content

Commit

Permalink
Add some utility methods to PMBatchMail, make "send" in PMBatchMail c…
Browse files Browse the repository at this point in the history
…all _check_values for each PMMail object before attempting to send
  • Loading branch information
themartorana committed Jun 18, 2012
1 parent 8c7a5e6 commit 52bdfbc
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion postmark/core.py
Expand Up @@ -308,7 +308,7 @@ def _check_values(self):
if not self.__api_key: if not self.__api_key:
raise PMMailMissingValueException('Cannot send an e-mail without a Postmark API Key') raise PMMailMissingValueException('Cannot send an e-mail without a Postmark API Key')
elif not self.__sender: elif not self.__sender:
raise PMMailMissingValueException('Cannot send an e-mail without a sender (.from field)') raise PMMailMissingValueException('Cannot send an e-mail without a sender (.sender field)')
elif not self.__to: elif not self.__to:
raise PMMailMissingValueException('Cannot send an e-mail without at least one recipient (.to field)') raise PMMailMissingValueException('Cannot send an e-mail without at least one recipient (.to field)')
elif not self.__subject: elif not self.__subject:
Expand Down Expand Up @@ -493,14 +493,39 @@ def __init__(self, **kwargs):
''' '''
) )


def add_message(self, message):
'''
Add a message to the batch
'''
self.__messages.append(message)

def remove_message(self, message):
'''
Remove a message from the batch
'''
if message in self.__messages:
self.__messages.remove(message)

def _check_values(self):
'''
Make sure all values are of the appropriate
type and are not missing.
'''
for message in self.__messages:
message._check_values()

def send(self, test=None): def send(self, test=None):
# Check messages for completeness prior to attempting to send
self._check_values()

# If test is not specified, attempt to read the Django setting # If test is not specified, attempt to read the Django setting
if test is None: if test is None:
try: try:
from django.conf import settings as django_settings from django.conf import settings as django_settings
test = getattr(django_settings, "POSTMARK_TEST_MODE", None) test = getattr(django_settings, "POSTMARK_TEST_MODE", None)
except ImportError: except ImportError:
pass pass

# Split up into groups of 500 messages for sending # Split up into groups of 500 messages for sending
for messages in _chunks(self.messages, PMBatchMail.MAX_MESSAGES): for messages in _chunks(self.messages, PMBatchMail.MAX_MESSAGES):
json_message = [] json_message = []
Expand Down

0 comments on commit 52bdfbc

Please sign in to comment.