1414 string_types = str # Python 3 doesn't have basestring
1515
1616
17+ model_remap = {
18+ 'campaign' : 'campaign_id' ,
19+ 'start_time' : 'options/start_time' ,
20+ 'track_opens' : 'options/open_tracking' ,
21+ 'track_clicks' : 'options/click_tracking' ,
22+ 'transactional' : 'options/transactional' ,
23+ 'use_sandbox' : 'options/sandbox' ,
24+ 'skip_suppression' : 'options/skip_suppression' ,
25+ 'ip_pool' : 'options/ip_pool' ,
26+ 'inline_css' : 'options/inline_css' ,
27+ 'email_rfc822' : 'content/email_rfc822' ,
28+ 'custom_headers' : 'content/headers' ,
29+ 'use_draft_template' : 'content/use_draft_template' ,
30+ 'reply_to' : 'content/reply_to' ,
31+ 'subject' : 'content/subject' ,
32+ 'from_email' : 'content/from' ,
33+ 'html' : 'content/html' ,
34+ 'text' : 'content/text' ,
35+ 'template' : 'content/template_id' ,
36+ 'attachments' : 'content/attachments' ,
37+ 'inline_images' : 'content/inline_images' ,
38+ 'recipient_list' : 'recipients/list_id' ,
39+ }
40+ model_remap_keys = frozenset (model_remap .keys ())
41+
42+
1743class Transmissions (Resource ):
1844 """
1945 Transmission class used to send, list and get transmissions. For detailed
@@ -24,71 +50,74 @@ class Transmissions(Resource):
2450 key = 'transmissions'
2551
2652 def _translate_keys (self , ** kwargs ):
27- model = {
28- 'content' : {},
29- 'options' : {},
30- 'recipients' : {}
31- }
32-
33- model ['description' ] = kwargs .get ('description' )
34- model ['return_path' ] = kwargs .get ('return_path' )
35- model ['campaign_id' ] = kwargs .get ('campaign' )
36- model ['metadata' ] = kwargs .get ('metadata' )
37- model ['substitution_data' ] = kwargs .get ('substitution_data' )
38-
39- model ['options' ]['start_time' ] = kwargs .get ('start_time' )
40- model ['options' ]['open_tracking' ] = kwargs .get ('track_opens' )
41- model ['options' ]['click_tracking' ] = kwargs .get ('track_clicks' )
42- model ['options' ]['transactional' ] = kwargs .get ('transactional' )
43- model ['options' ]['sandbox' ] = kwargs .get ('use_sandbox' )
44- model ['options' ]['skip_suppression' ] = kwargs .get ('skip_suppression' )
45- model ['options' ]['ip_pool' ] = kwargs .get ('ip_pool' )
46- model ['options' ]['inline_css' ] = kwargs .get ('inline_css' )
47-
48- rfc822 = kwargs .get ('email_rfc822' )
49- if rfc822 :
50- model ['content' ]['email_rfc822' ] = rfc822
51- else :
52- model ['content' ]['headers' ] = kwargs .get ('custom_headers' , {})
53- model ['content' ]['use_draft_template' ] = \
54- kwargs .get ('use_draft_template' , False )
55- model ['content' ]['reply_to' ] = kwargs .get ('reply_to' )
56- model ['content' ]['subject' ] = kwargs .get ('subject' )
57- from_email = kwargs .get ('from_email' )
53+ model = copy .deepcopy (kwargs )
54+
55+ # Intersection of keys that need to be remapped
56+ data_remap_keys = model_remap_keys .intersection (model .keys ())
57+
58+ for from_key in data_remap_keys :
59+ # Remap model keys to match API
60+ if from_key in model :
61+ to_model = model
62+ to_key = model_remap [from_key ]
63+ if to_key .index ('/' ):
64+ # Nested within a dict
65+ into_list = to_key .split ('/' )
66+ to_key = into_list [- 1 ]
67+ to_model = model .setdefault (into_list [0 ], {})
68+
69+ # Move from current key and place into new key
70+ to_model [to_key ] = model .pop (from_key )
71+
72+ content = model .setdefault ('content' , {})
73+ recipients = model .setdefault ('recipients' , [])
74+
75+ if content .get ('email_rfc822' ):
76+ # Remove unnecessary keys from model['content'], if rfc822
77+ rfc822_keys = frozenset ([
78+ 'headers' ,
79+ 'use_draft_template' ,
80+ 'reply_to' ,
81+ 'subject' ,
82+ 'from' ,
83+ 'html' ,
84+ 'text' ,
85+ 'template_id' ,
86+ 'attachments' ,
87+ 'inline_images' ,
88+ ])
89+ del_content_keys = rfc822_keys .intersection (content .keys ())
90+ for key in del_content_keys :
91+ del content [key ]
92+
93+ if 'from' in content :
94+ from_email = content .get ('from' )
5895 if isinstance (from_email , string_types ):
59- from_email = self ._parse_address (from_email )
60- model ['content' ]['from' ] = from_email
61- model ['content' ]['html' ] = kwargs .get ('html' )
62- model ['content' ]['text' ] = kwargs .get ('text' )
63- model ['content' ]['template_id' ] = kwargs .get ('template' )
64-
65- attachments = kwargs .get ('attachments' , [])
66- model ['content' ]['attachments' ] = self ._extract_attachments (
67- attachments )
68-
69- if 'inline_images' in kwargs :
70- inline_images = kwargs ['inline_images' ]
71- model ['content' ]['inline_images' ] = self ._extract_attachments (
72- inline_images )
73-
74- recipient_list = kwargs .get ('recipient_list' )
75- if recipient_list :
76- model ['recipients' ]['list_id' ] = recipient_list
77- else :
78- recipients = kwargs .get ('recipients' , [])
79- recipients = self ._extract_recipients (recipients )
80- cc = kwargs .get ('cc' )
81- bcc = kwargs .get ('bcc' )
96+ content ['from' ] = self ._parse_address (from_email )
97+
98+ if 'attachments' in content :
99+ attachments = content .get ('attachments' )
100+ content ['attachments' ] = self ._extract_attachments (attachments )
82101
102+ if 'inline_images' in content :
103+ inline_images = content .get ('inline_images' )
104+ content ['inline_images' ] = self ._extract_attachments (inline_images )
105+
106+ if recipients and not isinstance (recipients , dict ):
107+ model ['recipients' ] = self ._extract_recipients (recipients )
108+ recipients = model ['recipients' ]
109+
110+ cc = model .pop ('cc' , None )
83111 if cc :
84- model ['content' ]['headers' ]['CC' ] = ',' .join (cc )
112+ headers = content .setdefault ('headers' , {})
113+ headers ['CC' ] = ',' .join (cc )
85114 cc_copies = self ._format_copies (recipients , cc )
86- recipients = recipients + cc_copies
115+ recipients .extend (cc_copies )
116+
117+ bcc = model .pop ('bcc' , None )
87118 if bcc :
88119 bcc_copies = self ._format_copies (recipients , bcc )
89- recipients = recipients + bcc_copies
90-
91- model ['recipients' ] = recipients
120+ recipients .extend (bcc_copies )
92121
93122 return model
94123
0 commit comments