Skip to content

# Fixes #500 #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 53 additions & 67 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,70 +61,27 @@ def get(self):

:rtype: dict
"""
mail = {}
if self.from_email is not None:
mail["from"] = self.from_email.get()
if self.subject is not None:
mail["subject"] = self.subject

if self.personalizations:
mail["personalizations"] = [
personalization.get()
for personalization in self.personalizations
]

if self.contents:
mail["content"] = [ob.get() for ob in self.contents]

if self.attachments:
mail["attachments"] = [ob.get() for ob in self.attachments]

if self.template_id is not None:
mail["template_id"] = self.template_id

if self.sections:
sections = {}
for key in self.sections:
sections.update(key.get())
mail["sections"] = sections

if self.headers:
headers = {}
for key in self.headers:
headers.update(key.get())
mail["headers"] = headers

if self.categories:
mail["categories"] = [category.get() for category in
self.categories]

if self.custom_args:
custom_args = {}
for key in self.custom_args:
custom_args.update(key.get())
mail["custom_args"] = custom_args

if self.send_at is not None:
mail["send_at"] = self.send_at

if self.batch_id is not None:
mail["batch_id"] = self.batch_id

if self.asm is not None:
mail["asm"] = self.asm.get()

if self.ip_pool_name is not None:
mail["ip_pool_name"] = self.ip_pool_name

if self.mail_settings is not None:
mail["mail_settings"] = self.mail_settings.get()

if self.tracking_settings is not None:
mail["tracking_settings"] = self.tracking_settings.get()

if self.reply_to is not None:
mail["reply_to"] = self.reply_to.get()
return mail
mail = {
'from': _get(self.from_email),
'subject': self.subject,
'personalizations': _get_list(self.personalizations),
'content': _get_list(self.contents),
'attachments': _get_list(self.attachments),
'template_id': self.template_id,
'sections': _merge_dicts(self.sections),
'headers': _merge_dicts(self.headers),
'categories': _get_list(self.categories),
'custom_args': _merge_dicts(self.custom_args),
'send_at': self.send_at,
'batch_id': self.batch_id,
'asm': _get(self.asm),
'ip_pool_name': self.ip_pool_name,
'mail_settings': _get(self.mail_settings),
'tracking_settings': _get(self.tracking_settings),
'reply_to': _get(self.reply_to),
}

return _trim_none(mail)

@property
def from_email(self):
Expand Down Expand Up @@ -189,7 +146,8 @@ def batch_id(self):
This represents a batch of emails sent at the same time. Including a
batch_id in your request allows you include this email in that batch,
and also enables you to cancel or pause the delivery of that batch.
For more information, see https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.html
For more information, see:
https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.html

:rtype: int
"""
Expand Down Expand Up @@ -351,8 +309,8 @@ def add_header(self, header):
:type header: object
"""
if isinstance(header, dict):
(k, v) = list(header.items())[0]
self._headers.append(Header(k, v))
key, val = list(header.items())[0]
self._headers.append(Header(key, val))
else:
self._headers.append(header)

Expand Down Expand Up @@ -384,3 +342,31 @@ def add_custom_arg(self, custom_arg):
if self._custom_args is None:
self._custom_args = []
self._custom_args.append(custom_arg)


def _get(getter):
if getter is None:
return None
return getter.get()


def _get_list(getters):
if getters is None:
return None
return [x.get() for x in getters]


def _merge_dicts(getters):
if getters is None:
return None
out = {}
for key in getters:
out.update(key.get())
return out


def _trim_none(dictionary):
keys = [key for key, val in dictionary.items() if val is None]
for key in keys:
del dictionary[key]
return dictionary
3 changes: 1 addition & 2 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ def test_hello_world(self):
content = Content(
"text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
self.assertTrue(mail.get() == {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [
{'to': [{'email': '[email protected]'}]}], 'from': {'email': '[email protected]'}, 'subject': 'Sending with SendGrid is Fun'})
self.assertEqual(mail.get(), {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [{'to': [{'email': '[email protected]'}]}], 'from': {'email': '[email protected]'}, 'subject': 'Sending with SendGrid is Fun'})

def test_access_settings_activity_get(self):
params = {'limit': 1}
Expand Down