Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Metadata without additional classes #178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions docs/django/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,28 @@ Django is now configured to use the SparkPost email backend. You can now send ma
html_message='<p>Hello Rock stars!</p>',
)


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": "[email protected]",
"substitution_data": {
"key": "value"
}
'address': '[email protected]',
'substitution_data': {
'reward-level': 'Silver'
},
'metadata': {'user-id': '46576432465'}
}
],
from_email='[email protected]'
)
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:
Expand Down
32 changes: 30 additions & 2 deletions sparkpost/django/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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

Expand Down
103 changes: 92 additions & 11 deletions test/django/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,36 +186,117 @@ def test_campaign():
assert actual == expected


def test_substitution_data():
def test_recipient_attributes():
email_message = EmailMessage(
to=[
{
"address": "[email protected]",
"substitution_data": {
"key": "value"
}
'address': '[email protected]',
'substitution_data': {
'sub': 'value'
},
'metadata': {
'meta': 'value'
},
'tags': ['tag1']
}
],
from_email='[email protected]'
)

email_message.template = 'template-id'
email_message.substitution_data = {"key2": "value2"}
actual = SparkPostMessage(email_message)

expected = dict(
recipients=[
{
"address": "[email protected]",
"substitution_data": {
"key": "value"
}
'address': '[email protected]',
'substitution_data': {
'sub': 'value'
},
'metadata': {
'meta': 'value'
},
'tags': ['tag1']
}
],
from_email='[email protected]',
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': '[email protected]',
'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': '[email protected]'}],
from_email='[email protected]'
)
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': '[email protected]'}],
from_email='[email protected]',
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': '[email protected]'}],
from_email='[email protected]'
)
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': '[email protected]'}],
from_email='[email protected]',
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


Expand Down