diff --git a/sparkpost/django/message.py b/sparkpost/django/message.py index 903ece9..4fd0475 100644 --- a/sparkpost/django/message.py +++ b/sparkpost/django/message.py @@ -7,6 +7,26 @@ from .exceptions import UnsupportedContent +# API attributes to pass through to Transmissions Class +sparkpost_attributes = [ + 'substitution_data', + 'metadata', + 'description', + 'return_path', + 'ip_pool', + 'inline_css', + 'transactional', + 'start_time', + 'skip_suppression' +] + +# API attributes that need to be transformed for Transmissions Class +transform_attributes = { + 'sandbox': 'use_sandbox', + 'open_tracking': 'track_opens', + 'click_tracking': 'track_clicks' +} + class SparkPostMessage(dict): """ @@ -84,15 +104,20 @@ def __init__(self, message): 'type': mimetype }) - if hasattr(message, 'substitution_data'): - formatted['substitution_data'] = message.substitution_data + # Set all other extra attributes + for attribute in sparkpost_attributes: + if hasattr(message, attribute): + formatted[attribute] = getattr(message, attribute) + + # Set attributes that need to be transformed for Transmissions Class + for key, value in transform_attributes.items(): + if hasattr(message, key): + formatted[value] = getattr(message, key) + # Not in sparkpost_attributes for backwards comaptibility if hasattr(message, 'campaign'): formatted['campaign'] = message.campaign - if hasattr(message, 'metadata'): - formatted['metadata'] = message.metadata - if message.extra_headers: formatted['custom_headers'] = message.extra_headers if 'X-MSYS-API' in message.extra_headers: diff --git a/test/django/test_message.py b/test/django/test_message.py index 648867f..58314d3 100644 --- a/test/django/test_message.py +++ b/test/django/test_message.py @@ -186,69 +186,117 @@ def test_campaign(): assert actual == expected -def test_metadata(): +def test_recipient_attributes(): email_message = EmailMessage( to=[ { 'address': 'to@example.com', + 'substitution_data': { + 'sub': 'value' + }, 'metadata': { - 'key': 'value' - } + 'meta': 'value' + }, + 'tags': ['tag1'] } ], from_email='test@from.com' ) + email_message.template = 'template-id' - email_message.metadata = {'key2': 'value2'} actual = SparkPostMessage(email_message) expected = dict( recipients=[ { 'address': 'to@example.com', + 'substitution_data': { + 'sub': 'value' + }, 'metadata': { - 'key': 'value' - } + 'meta': 'value' + }, + 'tags': ['tag1'] } ], from_email='test@from.com', + template='template-id' + ) + + assert actual == expected + + +def test_pass_through_attr(): + + pass_through_attributes = { + 'substitution_data': {'sub': 'vale'}, + 'metadata': {'meta': 'value'}, + 'description': 'a description', + 'return_path': 'return@path.com', + 'ip_pool': 'pool-id', + 'inline_css': True, + 'transactional': True, + 'start_time': 'YYYY-MM-DDTHH:MM:SS+-HH:MM', + 'skip_suppression': True + } + + email_message = EmailMessage( + to=[{'address': 'to@example.com'}], + from_email='test@from.com' + ) + email_message.template = 'template-id' + + for key, value in pass_through_attributes.items(): + setattr(email_message, key, value) + + actual = SparkPostMessage(email_message) + + expected = dict( + recipients=[{'address': 'to@example.com'}], + from_email='test@from.com', template='template-id', - metadata={'key2': 'value2'} ) + for key, value in pass_through_attributes.items(): + expected[key] = value + assert actual == expected -def test_substitution_data(): +def test_transform_attr(): + + attributes_to_transform = { + 'sandbox': True, + 'open_tracking': False, + 'click_tracking': False, + } + email_message = EmailMessage( - to=[ - { - 'address': 'to@example.com', - 'substitution_data': { - 'key': 'value' - } - } - ], + to=[{'address': 'to@example.com'}], from_email='test@from.com' ) email_message.template = 'template-id' - email_message.substitution_data = {'key2': 'value2'} + + for key, value in attributes_to_transform.items(): + setattr(email_message, key, value) + actual = SparkPostMessage(email_message) expected = dict( - recipients=[ - { - 'address': 'to@example.com', - 'substitution_data': { - 'key': 'value' - } - } - ], + recipients=[{'address': 'to@example.com'}], from_email='test@from.com', template='template-id', - substitution_data={'key2': 'value2'} ) + transformed_attributes = { + 'use_sandbox': True, + 'track_opens': False, + 'track_clicks': False + } + + for key, value in transformed_attributes.items(): + expected[key] = value + assert actual == expected