Skip to content

Commit

Permalink
Clean up copies handling, test for multiple ccs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Leland committed Mar 2, 2016
1 parent 93bfaa4 commit 83f69e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
28 changes: 17 additions & 11 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,16 @@ def _translate_keys(self, **kwargs):
else:
recipients = kwargs.get('recipients', [])
cc = kwargs.get('cc')
if cc and len(recipients) > 0:
model['content']['headers']['CC'] = ','.join(cc)
formatted_ccs = self._extractRecipients(cc)
for recipient in formatted_ccs:
recipient['address'].update({'header_to': recipients[0]})
recipients = recipients + formatted_ccs
bcc = kwargs.get('bcc')
if bcc and len(recipients) > 0:
formatted_ccs = self._extractRecipients(bcc)
for recipient in formatted_ccs:
recipient['address'].update({'header_to': recipients[0]})
recipients = recipients + formatted_ccs

if cc:
model['content']['headers']['CC'] = ','.join(cc)
cc_copies = self._format_copies(recipients, cc)
recipients = recipients + cc_copies
if bcc:
bcc_copies = self._format_copies(recipients, bcc)
recipients = recipients + bcc_copies

model['recipients'] = self._extractRecipients(recipients)

attachments = kwargs.get('attachments', [])
Expand All @@ -71,6 +69,14 @@ def _translate_keys(self, **kwargs):

return model

def _format_copies(self, recipients, copies):
formatted_copies = []
if len(recipients) > 0:
formatted_copies = self._extractRecipients(copies)
for recipient in formatted_copies:
recipient['address'].update({'header_to': recipients[0]})
return formatted_copies

def _extract_attachments(self, attachments):
formatted_attachments = []
for attachment in attachments:
Expand Down
16 changes: 16 additions & 0 deletions test/test_transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ def test_translate_keys_with_cc():
}


def test_translate_keys_with_multiple_cc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
cc=['ccone@example.com', 'cctwo@example.com'])
assert results['recipients'] == [
{'address': {'email': 'primary@example.com'}},
{'address': {'email': 'ccone@example.com',
'header_to': 'primary@example.com'}},
{'address': {'email': 'cctwo@example.com',
'header_to': 'primary@example.com'}},
]
assert results['content']['headers'] == {
'CC': 'ccone@example.com,cctwo@example.com'
}


def test_translate_keys_with_bcc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
Expand Down

0 comments on commit 83f69e0

Please sign in to comment.