Skip to content

Commit

Permalink
Adding all unsupported fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Zamora committed Oct 3, 2019
1 parent 47d9c34 commit 0805998
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 31 deletions.
35 changes: 30 additions & 5 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,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:
Expand Down
100 changes: 74 additions & 26 deletions test/django/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,69 +186,117 @@ def test_campaign():
assert actual == expected


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

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

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

0 comments on commit 0805998

Please sign in to comment.