Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sendgrid/helpers/mail/personalization.py: 66%
145 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1class Personalization(object):
2 """A Personalization defines who should receive an individual message and
3 how that message should be handled.
4 """
6 def __init__(self):
7 """Create an empty Personalization and initialize member variables."""
8 self._tos = []
9 self._from_email = None
10 self._ccs = []
11 self._bccs = []
12 self._subject = None
13 self._headers = []
14 self._substitutions = []
15 self._custom_args = []
16 self._send_at = None
17 self._dynamic_template_data = None
19 def add_email(self, email):
20 email_type = type(email)
21 if email_type.__name__ == 'To': 21 ↛ 24line 21 didn't jump to line 24, because the condition on line 21 was never false
22 self.add_to(email)
23 return
24 if email_type.__name__ == 'Cc':
25 self.add_cc(email)
26 return
27 if email_type.__name__ == 'Bcc':
28 self.add_bcc(email)
29 return
30 if email_type.__name__ == 'From':
31 self.from_email = email
32 return
33 raise ValueError('Please use a To, From, Cc or Bcc object.')
35 def _get_unique_recipients(self, recipients):
36 unique_recipients = []
38 for recipient in recipients:
39 recipient_email = recipient['email'].lower() if isinstance(recipient, dict) else recipient.email.lower()
40 if all( 40 ↛ 38line 40 didn't jump to line 38, because the condition on line 40 was never false
41 unique_recipient['email'].lower() != recipient_email for unique_recipient in unique_recipients
42 ):
43 new_unique_recipient = recipient if isinstance(recipient, dict) else recipient.get()
44 unique_recipients.append(new_unique_recipient)
46 return unique_recipients
49 @property
50 def tos(self):
51 """A list of recipients for this Personalization.
53 :rtype: list(dict)
54 """
55 return self._get_unique_recipients(self._tos)
57 @tos.setter
58 def tos(self, value):
59 self._tos = value
61 def add_to(self, email):
62 """Add a single recipient to this Personalization.
64 :type email: Email
65 """
66 if email.substitutions: 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true
67 if isinstance(email.substitutions, list):
68 for substitution in email.substitutions:
69 self.add_substitution(substitution)
70 else:
71 self.add_substitution(email.substitutions)
73 if email.dynamic_template_data: 73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true
74 self.dynamic_template_data = email.dynamic_template_data
76 if email.subject: 76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true
77 if isinstance(email.subject, str):
78 self.subject = email.subject
79 else:
80 self.subject = email.subject.get()
82 self._tos.append(email.get())
84 @property
85 def from_email(self):
86 return self._from_email
88 @from_email.setter
89 def from_email(self, value):
90 self._from_email = value
92 def set_from(self, email):
93 self._from_email = email.get()
95 @property
96 def ccs(self):
97 """A list of recipients who will receive copies of this email.
99 :rtype: list(dict)
100 """
101 return self._get_unique_recipients(self._ccs)
103 @ccs.setter
104 def ccs(self, value):
105 self._ccs = value
107 def add_cc(self, email):
108 """Add a single recipient to receive a copy of this email.
110 :param email: new recipient to be CCed
111 :type email: Email
112 """
113 self._ccs.append(email.get())
115 @property
116 def bccs(self):
117 """A list of recipients who will receive blind carbon copies of this email.
119 :rtype: list(dict)
120 """
121 return self._get_unique_recipients(self._bccs)
123 @bccs.setter
124 def bccs(self, value):
125 self._bccs = value
127 def add_bcc(self, email):
128 """Add a single recipient to receive a blind carbon copy of this email.
130 :param email: new recipient to be BCCed
131 :type email: Email
132 """
133 self._bccs.append(email.get())
135 @property
136 def subject(self):
137 """The subject of your email (within this Personalization).
139 Char length requirements, according to the RFC:
140 https://stackoverflow.com/a/1592310
142 :rtype: string
143 """
144 return self._subject
146 @subject.setter
147 def subject(self, value):
148 self._subject = value
150 @property
151 def headers(self):
152 """The headers for emails in this Personalization.
154 :rtype: list(dict)
155 """
156 return self._headers
158 @headers.setter
159 def headers(self, value):
160 self._headers = value
162 def add_header(self, header):
163 """Add a single Header to this Personalization.
165 :type header: Header
166 """
167 self._headers.append(header.get())
169 @property
170 def substitutions(self):
171 """Substitutions to be applied within this Personalization.
173 :rtype: list(dict)
174 """
175 return self._substitutions
177 @substitutions.setter
178 def substitutions(self, value):
179 self._substitutions = value
181 def add_substitution(self, substitution):
182 """Add a new Substitution to this Personalization.
184 :type substitution: Substitution
185 """
186 if not isinstance(substitution, dict):
187 substitution = substitution.get()
189 self._substitutions.append(substitution)
191 @property
192 def custom_args(self):
193 """The CustomArgs that will be carried along with this Personalization.
195 :rtype: list(dict)
196 """
197 return self._custom_args
199 @custom_args.setter
200 def custom_args(self, value):
201 self._custom_args = value
203 def add_custom_arg(self, custom_arg):
204 """Add a CustomArg to this Personalization.
206 :type custom_arg: CustomArg
207 """
208 self._custom_args.append(custom_arg.get())
210 @property
211 def send_at(self):
212 """A unix timestamp allowing you to specify when you want emails from
213 this Personalization to be delivered. Scheduling more than 72 hours in
214 advance is forbidden.
216 :rtype: int
217 """
218 return self._send_at
220 @send_at.setter
221 def send_at(self, value):
222 self._send_at = value
224 @property
225 def dynamic_template_data(self):
226 """Data for dynamic transactional template.
227 Should be JSON-serializable structure.
229 :rtype: JSON-serializable structure
230 """
231 return self._dynamic_template_data
233 @dynamic_template_data.setter
234 def dynamic_template_data(self, value):
235 if not isinstance(value, dict): 235 ↛ 236line 235 didn't jump to line 236, because the condition on line 235 was never true
236 value = value.get()
238 self._dynamic_template_data = value
240 def get(self):
241 """
242 Get a JSON-ready representation of this Personalization.
244 :returns: This Personalization, ready for use in a request body.
245 :rtype: dict
246 """
247 personalization = {}
249 for key in ['tos', 'ccs', 'bccs']:
250 value = getattr(self, key)
251 if value:
252 personalization[key[:-1]] = value
254 from_value = getattr(self, 'from_email')
255 if from_value: 255 ↛ 256line 255 didn't jump to line 256, because the condition on line 255 was never true
256 personalization['from'] = from_value
258 for key in ['subject', 'send_at', 'dynamic_template_data']:
259 value = getattr(self, key)
260 if value:
261 personalization[key] = value
263 for prop_name in ['headers', 'substitutions', 'custom_args']:
264 prop = getattr(self, prop_name)
265 if prop: 265 ↛ 266line 265 didn't jump to line 266, because the condition on line 265 was never true
266 obj = {}
267 for key in prop:
268 obj.update(key)
269 personalization[prop_name] = obj
271 return personalization