Skip to content

Commit f910332

Browse files
author
Jose Zamora
committed
Adding all unsupported fields
1 parent cc143a2 commit f910332

File tree

2 files changed

+104
-31
lines changed

2 files changed

+104
-31
lines changed

sparkpost/django/message.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@
77

88
from .exceptions import UnsupportedContent
99

10+
# API attributes to pass through to Transmissions Class
11+
sparkpost_attributes = [
12+
'substitution_data',
13+
'metadata',
14+
'description',
15+
'return_path',
16+
'ip_pool',
17+
'inline_css',
18+
'transactional',
19+
'start_time',
20+
'skip_suppression'
21+
]
22+
23+
# API attributes that need to be transformed for Transmissions Class
24+
transform_attributes = {
25+
'sandbox': 'use_sandbox',
26+
'open_tracking': 'track_opens',
27+
'click_tracking': 'track_clicks'
28+
}
29+
1030

1131
class SparkPostMessage(dict):
1232
"""
@@ -84,15 +104,20 @@ def __init__(self, message):
84104
'type': mimetype
85105
})
86106

87-
if hasattr(message, 'substitution_data'):
88-
formatted['substitution_data'] = message.substitution_data
107+
# Set all other extra attributes
108+
for attribute in sparkpost_attributes:
109+
if hasattr(message, attribute):
110+
formatted[attribute] = getattr(message, attribute)
111+
112+
# Set attributes that need to be transformed for Transmissions Class
113+
for key, value in transform_attributes.items():
114+
if hasattr(message, key):
115+
formatted[value] = getattr(message, key)
89116

117+
# Not in sparkpost_attributes for backwards comaptibility
90118
if hasattr(message, 'campaign'):
91119
formatted['campaign'] = message.campaign
92120

93-
if hasattr(message, 'metadata'):
94-
formatted['metadata'] = message.metadata
95-
96121
if message.extra_headers:
97122
formatted['custom_headers'] = message.extra_headers
98123
if 'X-MSYS-API' in message.extra_headers:

test/django/test_message.py

+74-26
Original file line numberDiff line numberDiff line change
@@ -186,69 +186,117 @@ def test_campaign():
186186
assert actual == expected
187187

188188

189-
def test_metadata():
189+
def test_recipient_attributes():
190190
email_message = EmailMessage(
191191
to=[
192192
{
193193
'address': '[email protected]',
194+
'substitution_data': {
195+
'sub': 'value'
196+
},
194197
'metadata': {
195-
'key': 'value'
196-
}
198+
'meta': 'value'
199+
},
200+
'tags': ['tag1']
197201
}
198202
],
199203
from_email='[email protected]'
200204
)
205+
201206
email_message.template = 'template-id'
202-
email_message.metadata = {'key2': 'value2'}
203207
actual = SparkPostMessage(email_message)
204208

205209
expected = dict(
206210
recipients=[
207211
{
208212
'address': '[email protected]',
213+
'substitution_data': {
214+
'sub': 'value'
215+
},
209216
'metadata': {
210-
'key': 'value'
211-
}
217+
'meta': 'value'
218+
},
219+
'tags': ['tag1']
212220
}
213221
],
214222
from_email='[email protected]',
223+
template='template-id'
224+
)
225+
226+
assert actual == expected
227+
228+
229+
def test_pass_through_attr():
230+
231+
pass_through_attributes = {
232+
'substitution_data': {'sub': 'vale'},
233+
'metadata': {'meta': 'value'},
234+
'description': 'a description',
235+
'return_path': '[email protected]',
236+
'ip_pool': 'pool-id',
237+
'inline_css': True,
238+
'transactional': True,
239+
'start_time': 'YYYY-MM-DDTHH:MM:SS+-HH:MM',
240+
'skip_suppression': True
241+
}
242+
243+
email_message = EmailMessage(
244+
to=[{'address': '[email protected]'}],
245+
from_email='[email protected]'
246+
)
247+
email_message.template = 'template-id'
248+
249+
for key, value in pass_through_attributes.items():
250+
setattr(email_message, key, value)
251+
252+
actual = SparkPostMessage(email_message)
253+
254+
expected = dict(
255+
recipients=[{'address': '[email protected]'}],
256+
from_email='[email protected]',
215257
template='template-id',
216-
metadata={'key2': 'value2'}
217258
)
218259

260+
for key, value in pass_through_attributes.items():
261+
expected[key] = value
262+
219263
assert actual == expected
220264

221265

222-
def test_substitution_data():
266+
def test_transform_attr():
267+
268+
attributes_to_transform = {
269+
'sandbox': True,
270+
'open_tracking': False,
271+
'click_tracking': False,
272+
}
273+
223274
email_message = EmailMessage(
224-
to=[
225-
{
226-
'address': '[email protected]',
227-
'substitution_data': {
228-
'key': 'value'
229-
}
230-
}
231-
],
275+
to=[{'address': '[email protected]'}],
232276
from_email='[email protected]'
233277
)
234278
email_message.template = 'template-id'
235-
email_message.substitution_data = {'key2': 'value2'}
279+
280+
for key, value in attributes_to_transform.items():
281+
setattr(email_message, key, value)
282+
236283
actual = SparkPostMessage(email_message)
237284

238285
expected = dict(
239-
recipients=[
240-
{
241-
'address': '[email protected]',
242-
'substitution_data': {
243-
'key': 'value'
244-
}
245-
}
246-
],
286+
recipients=[{'address': '[email protected]'}],
247287
from_email='[email protected]',
248288
template='template-id',
249-
substitution_data={'key2': 'value2'}
250289
)
251290

291+
transformed_attributes = {
292+
'use_sandbox': True,
293+
'track_opens': False,
294+
'track_clicks': False
295+
}
296+
297+
for key, value in transformed_attributes.items():
298+
expected[key] = value
299+
252300
assert actual == expected
253301

254302

0 commit comments

Comments
 (0)