diff --git a/docs/django/backend.rst b/docs/django/backend.rst index 39bb37d..d1fb9d3 100644 --- a/docs/django/backend.rst +++ b/docs/django/backend.rst @@ -47,23 +47,28 @@ Django is now configured to use the SparkPost email backend. You can now send ma html_message='

Hello Rock stars!

', ) - -You can also use `EmailMessage` or `EmailMultiAlternatives` class directly. That will give you access to more specific fileds like `template`: + +You can also use `EmailMessage` or `EmailMultiAlternatives` class directly. +This allows you to set additional SparkPost fields: `template`, `substitution_data`, `campaign`, `metadata`: .. code-block:: python - + email = EmailMessage( to=[ { - "address": "to@example.com", - "substitution_data": { - "key": "value" - } + 'address': 'to@example.com', + 'substitution_data': { + 'reward-level': 'Silver' + }, + 'metadata': {'user-id': '46576432465'} } ], from_email='test@from.com' ) email.template = 'template-id' + email.substitution_data = {'season': 'Winter'} + email.metadata = {'cart-id': '74562657874'} + email.campaign = 'campaign-id' email.send() Or cc, bcc, reply to, or attachments fields: diff --git a/sparkpost/django/message.py b/sparkpost/django/message.py index 9b12222..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,9 +104,17 @@ 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 diff --git a/test/django/test_message.py b/test/django/test_message.py index ff57ca3..58314d3 100644 --- a/test/django/test_message.py +++ b/test/django/test_message.py @@ -186,36 +186,117 @@ def test_campaign(): assert actual == expected -def test_substitution_data(): +def test_recipient_attributes(): email_message = EmailMessage( to=[ { - "address": "to@example.com", - "substitution_data": { - "key": "value" - } + 'address': 'to@example.com', + 'substitution_data': { + 'sub': 'value' + }, + 'metadata': { + 'meta': 'value' + }, + 'tags': ['tag1'] } ], from_email='test@from.com' ) + email_message.template = 'template-id' - email_message.substitution_data = {"key2": "value2"} actual = SparkPostMessage(email_message) expected = dict( recipients=[ { - "address": "to@example.com", - "substitution_data": { - "key": "value" - } + 'address': 'to@example.com', + 'substitution_data': { + 'sub': 'value' + }, + 'metadata': { + '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', + ) + + for key, value in pass_through_attributes.items(): + expected[key] = value + + assert actual == expected + + +def test_transform_attr(): + + attributes_to_transform = { + 'sandbox': True, + 'open_tracking': False, + 'click_tracking': False, + } + + email_message = EmailMessage( + to=[{'address': 'to@example.com'}], + from_email='test@from.com' + ) + email_message.template = 'template-id' + + for key, value in attributes_to_transform.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', - 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