From 3932ac38b3ba422c70db12e9b17cc6f37af4dfe4 Mon Sep 17 00:00:00 2001 From: Denis Vlasov Date: Wed, 19 Oct 2016 00:54:08 +0300 Subject: [PATCH 1/2] refactor helpers using property getter/setter (sendgrid/sendgrid-python#207) --- sendgrid/helpers/mail/mail.py | 1176 ++++++++++++++++++++++++--------- test/test_mail.py | 70 +- 2 files changed, 881 insertions(+), 365 deletions(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index f16993d9f..4c3c34fcf 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -1,35 +1,35 @@ """v3/mail/send response body builder""" -import json class Mail(object): """Creates the response body for v3/mail/send""" - def __init__(self, from_email = None, subject = None, to_email = None, content = None): - self.from_email = None - self.subject = None - self.personalizations = None - self.contents = None - self.attachments = None - self.template_id = None - self.sections = None - self.headers = None - self.categories = None - self.custom_args = None - self.send_at = None - self.batch_id = None - self.asm = None - self.ip_pool_name = None - self.mail_settings = None - self.tracking_settings = None - self.reply_to = None + def __init__(self, from_email=None, subject=None, to_email=None, content=None): + self._from_email = None + self._subject = None + self._template_id = None + self._send_at = None + self._batch_id = None + self._asm = None + self._ip_pool_name = None + self._mail_settings = None + self._tracking_settings = None + self._reply_to = None + + self._personalizations = None + self._contents = None + self._attachments = None + self._sections = None + self._headers = None + self._categories = None + self._custom_args = None # Minimum required to send an email if from_email and subject and to_email and content: - self.set_from(from_email) + self.from_email = from_email + self.subject = subject personalization = Personalization() personalization.add_to(to_email) self.add_personalization(personalization) - self.set_subject(subject) self.add_content(content) def __str__(self): @@ -40,119 +40,197 @@ def get(self): :return: response body dict """ mail = {} - if self.from_email != None: + if self.from_email is not None: mail["from"] = self.from_email.get() - if self.subject != None: + if self.subject is not None: mail["subject"] = self.subject - if self.personalizations != None: + if self.personalizations is not None: mail["personalizations"] = [personalization.get() for personalization in self.personalizations] - if self.contents != None: + if self.contents is not None: mail["content"] = [ob.get() for ob in self.contents] - if self.attachments != None: + if self.attachments is not None: mail["attachments"] = [ob.get() for ob in self.attachments] - if self.template_id != None: + if self.template_id is not None: mail["template_id"] = self.template_id - if self.sections != None: + if self.sections is not None: sections = {} for key in self.sections: sections.update(key.get()) mail["sections"] = sections - if self.headers != None: + if self.headers is not None: headers = {} for key in self.headers: headers.update(key.get()) mail["headers"] = headers - if self.categories != None: + if self.categories is not None: mail["categories"] = [category.get() for category in self.categories] - if self.custom_args != None: + if self.custom_args is not None: custom_args = {} for key in self.custom_args: custom_args.update(key.get()) mail["custom_args"] = custom_args - if self.send_at != None: + if self.send_at is not None: mail["send_at"] = self.send_at - if self.batch_id != None: + if self.batch_id is not None: mail["batch_id"] = self.batch_id - if self.asm != None: - mail["asm"] = self.asm - if self.ip_pool_name != None: + 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 != None: + if self.mail_settings is not None: mail["mail_settings"] = self.mail_settings.get() - if self.tracking_settings != None: + if self.tracking_settings is not None: mail["tracking_settings"] = self.tracking_settings.get() - if self.reply_to != None: + if self.reply_to is not None: mail["reply_to"] = self.reply_to.get() return mail - def set_from(self, email): - self.from_email = email + @property + def from_email(self): + return self._from_email - def set_subject(self, subject): - self.subject = subject + @from_email.setter + def from_email(self, value): + self._from_email = value + + @property + def subject(self): + return self._subject + + @subject.setter + def subject(self, value): + self._subject = value + + @property + def template_id(self): + return self._template_id + + @template_id.setter + def template_id(self, value): + self._template_id = value + + @property + def send_at(self): + return self._send_at + + @send_at.setter + def send_at(self, value): + self._send_at = value + + @property + def batch_id(self): + return self._batch_id + + @batch_id.setter + def batch_id(self, value): + self._batch_id = value + + @property + def asm(self): + return self._asm + + @asm.setter + def asm(self, value): + self._asm = value + + @property + def mail_settings(self): + return self._mail_settings + + @mail_settings.setter + def mail_settings(self, value): + self._mail_settings = value + + @property + def tracking_settings(self): + return self._tracking_settings + + @tracking_settings.setter + def tracking_settings(self, value): + self._tracking_settings = value + + @property + def ip_pool_name(self): + return self._ip_pool_name + + @ip_pool_name.setter + def ip_pool_name(self, value): + self._ip_pool_name = value + + @property + def reply_to(self): + return self._reply_to + + @reply_to.setter + def reply_to(self, value): + self._reply_to = value + + @property + def personalizations(self): + return self._personalizations def add_personalization(self, personalizations): - if self.personalizations is None: - self.personalizations = [] - self.personalizations.append(personalizations) + if self._personalizations is None: + self._personalizations = [] + self._personalizations.append(personalizations) + + @property + def contents(self): + return self._contents def add_content(self, content): - if self.contents is None: - self.contents = [] - self.contents.append(content) + if self._contents is None: + self._contents = [] + self._contents.append(content) + + @property + def attachments(self): + return self._attachments def add_attachment(self, attachment): - if self.attachments is None: - self.attachments = [] - self.attachments.append(attachment) + if self._attachments is None: + self._attachments = [] + self._attachments.append(attachment) - def set_template_id(self, template_id): - self.template_id = template_id + @property + def sections(self): + return self._sections def add_section(self, section): - if self.sections is None: - self.sections = [] - self.sections.append(section) + if self._sections is None: + self._sections = [] + self._sections.append(section) + + @property + def headers(self): + return self._headers def add_header(self, header): - if self.headers is None: - self.headers = [] + if self._headers is None: + self._headers = [] if isinstance(header, dict): - (k,v) = list(header.items())[0] - self.headers.append(Header(k,v)) + (k, v) = list(header.items())[0] + self._headers.append(Header(k, v)) else: - self.headers.append(header) + self._headers.append(header) - def add_category(self, category): - if self.categories is None: - self.categories = [] - self.categories.append(category) + @property + def categories(self): + return self._categories - def add_custom_arg(self, custom_arg): - if self.custom_args is None: - self.custom_args = [] - self.custom_args.append(custom_arg) - - def set_send_at(self, send_at): - self.send_at = send_at - - def set_batch_id(self, batch_id): - self.batch_id = batch_id - - def set_asm(self, asm): - self.asm = asm.get() - - def set_mail_settings(self, mail_settings): - self.mail_settings = mail_settings - - def set_tracking_settings(self, tracking_settings): - self.tracking_settings = tracking_settings + def add_category(self, category): + if self._categories is None: + self._categories = [] + self._categories.append(category) - def set_ip_pool_name(self, ip_pool_name): - self.ip_pool_name = ip_pool_name + @property + def custom_args(self): + return self._custom_args - def set_reply_to(self, reply_to): - self.reply_to = reply_to + def add_custom_arg(self, custom_arg): + if self._custom_args is None: + self._custom_args = [] + self._custom_args.append(custom_arg) ################################################################ # The following objects are meant to be extended with validation @@ -161,234 +239,417 @@ def set_reply_to(self, reply_to): class Email(object): def __init__(self, email=None, name=None): - self.name = name if name != None else None - self.email = email if email != None else None + self._name = None + self._email = None + + if email is not None: + self.email = email + if name is not None: + self.name = name + + @property + def name(self): + return self._name + + @name.setter + def name(self, value): + self._name = value - def set_name(self, name): - self.name = name + @property + def email(self): + return self._email - def set_email(self, email): - self.email = email + @email.setter + def email(self, value): + self._email = value def get(self): email = {} - if self.name != None: + if self.name is not None: email["name"] = self.name - if self.email != None: + if self.email is not None: email["email"] = self.email return email class Content(object): def __init__(self, type=None, value=None): - self.type = type if type != None else None - self.value = value if value != None else None + self._type = None + self._value = None - def set_type(self, type): - self.type = type + if type is not None: + self.type = type + if value is not None: + self.value = value - def set_value(self, value): - self.value = value + @property + def type(self): + return self._type + + @type.setter + def type(self, value): + self._type = value + + @property + def value(self): + return self._value + + @value.setter + def value(self, value): + self._value = value def get(self): content = {} - if self.type != None: + if self.type is not None: content["type"] = self.type - if self.value != None: + if self.value is not None: content["value"] = self.value return content class Header(object): def __init__(self, key=None, value=None): - self.key = key if key != None else None - self.value = value if value != None else None + self._key = None + self._value = None + + if key is not None: + self.key = key + if value is not None: + self.value = value - def set_key(self, key): - self.key = key + @property + def key(self): + return self._key - def set_value(self, value): - self.value = value + @key.setter + def key(self, value): + self._key = value + + @property + def value(self): + return self._value + + @value.setter + def value(self, value): + self._value = value def get(self): header = {} - if self.key != None and self.value != None: + if self.key is not None and self.value is not None: header[self.key] = self.value return header class Substitution(object): def __init__(self, key=None, value=None): - self.key = str(key) if key != None else None - self.value = str(value) if value != None else None + self._key = None + self._value = None + + if key is not None: + self.key = key + if value is not None: + self.value = value + + @property + def key(self): + return self._key - def set_key(self, key): - self.key = str(key) if key != None else None + @key.setter + def key(self, value): + self._key = value - def set_value(self, value): - self.value = str(value) if value != None else None + @property + def value(self): + return self._value + + @value.setter + def value(self, value): + self._value = value def get(self): substitution = {} - if self.key != None and self.value != None: + if self.key is not None and self.value is not None: substitution[self.key] = self.value return substitution class Section(object): def __init__(self, key=None, value=None): - self.key = key if key != None else None - self.value = value if value != None else None + self._key = None + self._value = None + + if key is not None: + self.key = key + if value is not None: + self.value = value + + @property + def key(self): + return self._key - def set_key(self, key): - self.key = key + @key.setter + def key(self, value): + self._key = value - def set_value(self, value): - self.value = value + @property + def value(self): + return self._value + + @value.setter + def value(self, value): + self._value = value def get(self): section = {} - if self.key != None and self.value != None: + if self.key is not None and self.value is not None: section[self.key] = self.value return section class CustomArg(object): def __init__(self, key=None, value=None): - self.key = key if key != None else None - self.value = value if value != None else None + self._key = None + self._value = None + + if key is not None: + self.key = key + if value is not None: + self.value = value + + @property + def key(self): + return self._key + + @key.setter + def key(self, value): + self._key = value - def set_key(self, key): - self.key = key + @property + def value(self): + return self._value - def set_value(self, value): - self.value = value + @value.setter + def value(self, value): + self._value = value def get(self): custom_arg = {} - if self.key != None and self.value != None: + if self.key is not None and self.value is not None: custom_arg[self.key] = self.value return custom_arg class Personalization(object): def __init__(self): - self.tos = None - self.ccs = None - self.bccs = None - self.subject = None - self.headers = None - self.substitutions = None - self.custom_args = None - self.send_at = None + self._tos = None + self._ccs = None + self._bccs = None + self._subject = None + self._headers = None + self._substitutions = None + self._custom_args = None + self._send_at = None + + @property + def tos(self): + return self._tos + + @tos.setter + def tos(self, value): + self._tos = value def add_to(self, email): - if self.tos is None: - self.tos = [] - self.tos.append(email.get()) + if self._tos is None: + self._tos = [] + self._tos.append(email.get()) + + @property + def ccs(self): + return self._ccs + + @ccs.setter + def ccs(self, value): + self._ccs = value def add_cc(self, email): - if self.ccs is None: - self.ccs = [] - self.ccs.append(email.get()) + if self._ccs is None: + self._ccs = [] + self._ccs.append(email.get()) + + @property + def bccs(self): + return self._bccs + + @bccs.setter + def bccs(self, value): + self._bccs = value def add_bcc(self, email): - if self.bccs is None: - self.bccs = [] - self.bccs.append(email.get()) + if self._bccs is None: + self._bccs = [] + self._bccs.append(email.get()) + + @property + def subject(self): + return self._subject + + @subject.setter + def subject(self, value): + self._subject = value + + @property + def headers(self): + return self._headers - def set_subject(self, subject): - self.subject = subject + @headers.setter + def headers(self, value): + self._headers = value def add_header(self, header): - if self.headers is None: - self.headers = [] - self.headers.append(header.get()) + if self._headers is None: + self._headers = [] + self._headers.append(header.get()) + + @property + def substitutions(self): + return self._substitutions + + @substitutions.setter + def substitutions(self, value): + self.substitutions = value def add_substitution(self, substitution): - if self.substitutions is None: - self.substitutions = [] - self.substitutions.append(substitution.get()) + if self._substitutions is None: + self._substitutions = [] + self._substitutions.append(substitution.get()) + + @property + def custom_args(self): + return self._custom_args + + @custom_args.setter + def custom_args(self, value): + self._custom_args = value def add_custom_arg(self, custom_arg): - if self.custom_args is None: - self.custom_args = [] - self.custom_args.append(custom_arg.get()) + if self._custom_args is None: + self._custom_args = [] + self._custom_args.append(custom_arg.get()) + + @property + def send_at(self): + return self._send_at - def set_send_at(self, send_at): - self.send_at = send_at + @send_at.setter + def send_at(self, value): + self._send_at = value def get(self): personalization = {} - if self.tos != None: + if self.tos is not None: personalization["to"] = self.tos - if self.ccs != None: + if self.ccs is not None: personalization["cc"] = self.ccs - if self.bccs != None: + if self.bccs is not None: personalization["bcc"] = self.bccs - if self.subject != None: + if self.subject is not None: personalization["subject"] = self.subject - if self.headers != None: + if self.headers is not None: headers = {} for key in self.headers: headers.update(key) personalization["headers"] = headers - if self.substitutions != None: + if self.substitutions is not None: substitutions = {} for key in self.substitutions: substitutions.update(key) personalization["substitutions"] = substitutions - if self.custom_args != None: + if self.custom_args is not None: custom_args = {} for key in self.custom_args: custom_args.update(key) personalization["custom_args"] = custom_args - if self.send_at != None: + if self.send_at is not None: personalization["send_at"] = self.send_at return personalization class Attachment(object): def __init__(self): - self.content = None - self.type = None - self.filename = None - self.disposition = None - self.content_id = None + self._content = None + self._type = None + self._filename = None + self._disposition = None + self._content_id = None + + @property + def content(self): + return self._content + + @content.setter + def content(self, value): + self._content = value - def set_content(self, content): - self.content = content + @property + def type(self): + return self._type - def set_type(self, type): - self.type = type + @type.setter + def type(self, value): + self._type = value - def set_filename(self, filename): - self.filename = filename + @property + def filename(self): + return self._filename - def set_disposition(self, disposition): - self.disposition = disposition + @filename.setter + def filename(self, value): + self._filename = value - def set_content_id(self, content_id): - self.content_id = content_id + @property + def disposition(self): + return self._disposition + + @disposition.setter + def disposition(self, value): + self._disposition = value + + @property + def content_id(self): + return self._content_id + + @content_id.setter + def content_id(self, value): + self._content_id = value def get(self): attachment = {} - if self.content != None: + if self.content is not None: attachment["content"] = self.content - if self.type != None: + if self.type is not None: attachment["type"] = self.type - if self.filename != None: + if self.filename is not None: attachment["filename"] = self.filename - if self.disposition != None: + if self.disposition is not None: attachment["disposition"] = self.disposition - if self.content_id != None: + if self.content_id is not None: attachment["content_id"] = self.content_id return attachment class Category(object): def __init__(self, name=None): - self.name = name if name != None else None + self._name = None + if name is not None: + self._name = name + + @property + def name(self): + return self._name + + @name.setter + def name(self, value): + self._name = value def get(self): return self.name @@ -396,28 +657,70 @@ def get(self): class ASM(object): def __init__(self, group_id=None, groups_to_display=None): - self.group_id = group_id if group_id != None else None - self.groups_to_display = groups_to_display if groups_to_display != None else None + self._group_id = None + self._groups_to_display = None + + if group_id is not None: + self._group_id = group_id + if groups_to_display is not None: + self._groups_to_display = groups_to_display + + @property + def group_id(self): + return self._group_id + + @group_id.setter + def group_id(self, value): + self._group_id = value + + @property + def groups_to_display(self): + return self._groups_to_display + + @groups_to_display.setter + def groups_to_display(self, value): + self._groups_to_display = value def get(self): asm = {} - if self.group_id != None: + if self.group_id is not None: asm["group_id"] = self.group_id - if self.groups_to_display != None: + if self.groups_to_display is not None: asm["groups_to_display"] = self.groups_to_display return asm class BCCSettings(object): def __init__(self, enable=None, email=None): - self.enable = enable if enable != None else None - self.email = email if email != None else None + self._enable = None + self._email = None + + if enable is not None: + self.enable = enable + if email is not None: + self.email = email + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value + + @property + def email(self): + return self._email + + @email.setter + def email(self, value): + self._email = value def get(self): bcc_settings = {} - if self.enable != None: + if self.enable is not None: bcc_settings["enable"] = self.enable - if self.email != None: + if self.email is not None: email = self.email.get() bcc_settings["email"] = email["email"] return bcc_settings @@ -425,184 +728,334 @@ def get(self): class BypassListManagement(object): def __init__(self, enable=None): - self.enable = enable if enable != None else None + self._enable = None + + if enable is not None: + self.enable = enable + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value def get(self): bypass_list_management = {} - if self.enable != None: + if self.enable is not None: bypass_list_management["enable"] = self.enable return bypass_list_management class FooterSettings(object): def __init__(self, enable=None, text=None, html=None): - self.enable = enable if enable != None else None - self.text = text if text else text - self.html = html if html else html + self._enable = None + self._text = None + self._html = None - def set_enable(self, enable): - self.enable = enable + if enable is not None: + self.enable = enable + if text is not None: + self.text = text + if html is not None: + self.html = html - def set_text(self, text): - self.text = text + @property + def enable(self): + return self._enable - def set_html(self, html): - self.html = html + @enable.setter + def enable(self, value): + self._enable = value + + @property + def text(self): + return self._text + + @text.setter + def text(self, value): + self._text = value + + @property + def html(self): + return self._html + + @html.setter + def html(self, value): + self._html = value def get(self): footer_settings = {} - if self.enable != None: + if self.enable is not None: footer_settings["enable"] = self.enable - if self.text != None: + if self.text is not None: footer_settings["text"] = self.text - if self.html != None: + if self.html is not None: footer_settings["html"] = self.html return footer_settings class SandBoxMode(object): def __init__(self, enable=None): - self.enable = enable if enable else False + self._enable = None + + if enable is not None: + self.enable = enable + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value def get(self): sandbox_mode = {} - if self.enable != None: + if self.enable is not None: sandbox_mode["enable"] = self.enable return sandbox_mode class SpamCheck(object): def __init__(self, enable=None, threshold=None, post_to_url=None): - self.enable = enable if enable != None else None - self.threshold = threshold if threshold != None else None - self.post_to_url = post_to_url if post_to_url != None else None + self._enable = None + self._threshold = None + self._post_to_url = None - def set_enable(self, enable): - self.enable = enable + if enable is not None: + self.enable = enable + if threshold is not None: + self.threshold = threshold + if post_to_url is not None: + self.post_to_url = post_to_url - def set_threshold(self, threshold): - self.threshold = threshold + @property + def enable(self): + return self._enable - def set_post_to_url(self, post_to_url): - self.post_to_url = post_to_url + @enable.setter + def enable(self, value): + self._enable = value + + @property + def threshold(self): + return self._threshold + + @threshold.setter + def threshold(self, value): + self._threshold = value + + @property + def post_to_url(self): + return self._post_to_url + + @post_to_url.setter + def post_to_url(self, value): + self._post_to_url = value def get(self): spam_check = {} - if self.enable != None: + if self.enable is not None: spam_check["enable"] = self.enable - if self.threshold != None: + if self.threshold is not None: spam_check["threshold"] = self.threshold - if self.post_to_url != None: + if self.post_to_url is not None: spam_check["post_to_url"] = self.post_to_url return spam_check class MailSettings(object): def __init__(self): - self.bcc_settings = None - self.bypass_list_management = None - self.footer_settings = None - self.sandbox_mode = None - self.spam_check = None + self._bcc_settings = None + self._bypass_list_management = None + self._footer_settings = None + self._sandbox_mode = None + self._spam_check = None + + @property + def bcc_settings(self): + return self._bcc_settings + + @bcc_settings.setter + def bcc_settings(self, value): + self._bcc_settings = value + + @property + def bypass_list_management(self): + return self._bypass_list_management + + @bypass_list_management.setter + def bypass_list_management(self, value): + self._bypass_list_management = value - def set_bcc_settings(self, bcc_settings): - self.bcc_settings = bcc_settings + @property + def footer_settings(self): + return self._footer_settings - def set_bypass_list_management(self, bypass_list_management): - self.bypass_list_management = bypass_list_management + @footer_settings.setter + def footer_settings(self, value): + self._footer_settings = value - def set_footer_settings(self, footer_settings): - self.footer_settings = footer_settings + @property + def sandbox_mode(self): + return self._sandbox_mode - def set_sandbox_mode(self, sandbox_mode): - self.sandbox_mode = sandbox_mode + @sandbox_mode.setter + def sandbox_mode(self, value): + self._sandbox_mode = value - def set_spam_check(self, spam_check): - self.spam_check = spam_check + @property + def spam_check(self): + return self._spam_check + + @spam_check.setter + def spam_check(self, value): + self._spam_check = value def get(self): mail_settings = {} - if self.bcc_settings != None: + if self.bcc_settings is not None: mail_settings["bcc"] = self.bcc_settings.get() - if self.bypass_list_management != None: + if self.bypass_list_management is not None: mail_settings["bypass_list_management"] = self.bypass_list_management.get() - if self.footer_settings != None: + if self.footer_settings is not None: mail_settings["footer"] = self.footer_settings.get() - if self.sandbox_mode != None: + if self.sandbox_mode is not None: mail_settings["sandbox_mode"] = self.sandbox_mode.get() - if self.spam_check != None: + if self.spam_check is not None: mail_settings["spam_check"] = self.spam_check.get() return mail_settings class ClickTracking(object): def __init__(self, enable=None, enable_text=None): - self.enable = enable if enable else None - self.enable_text = enable_text if enable_text !=None else None + self._enable = None + self._enable_text = None + + if enable is not None: + self.enable = enable + if enable_text is not None: + self.enable_text = enable_text + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value - def set_enable(self, enable): - self.enable = enable + @property + def enable_text(self): + return self._enable_text - def set_enable_text(self, enable_text): - self.enable_text = enable_text + @enable_text.setter + def enable_text(self, value): + self._enable_text = value def get(self): click_tracking = {} - if self.enable != None: + if self.enable is not None: click_tracking["enable"] = self.enable - if self.enable_text != None: + if self.enable_text is not None: click_tracking["enable_text"] = self.enable_text return click_tracking class OpenTracking(object): def __init__(self, enable=None, substitution_tag=None): - self.enable = enable if enable != None else None - self.substitution_tag = substitution_tag if substitution_tag !=None else None + self._enable = None + self._substitution_tag = None + + if enable is not None: + self.enable = enable + if substitution_tag is not None: + self.substitution_tag = substitution_tag + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value - def set_enable(self, enable): - self.enable = enable + @property + def substitution_tag(self): + return self._substitution_tag - def set_substitution_tag(self, substitution_tag): - self.substitution_tag = substitution_tag + @substitution_tag.setter + def substitution_tag(self, value): + self._substitution_tag = value def get(self): open_tracking = {} - if self.enable != None: + if self.enable is not None: open_tracking["enable"] = self.enable - if self.substitution_tag != None: + if self.substitution_tag is not None: open_tracking["substitution_tag"] = self.substitution_tag return open_tracking class SubscriptionTracking(object): def __init__(self, enable=None, text=None, html=None, substitution_tag=None): - self.enable = enable if enable != None else None - self.text = text if text != None else None - self.html = html if html != None else None - self.substitution_tag = substitution_tag if substitution_tag != None else None - - def set_enable(self, enable): - self.enable = enable - - def set_text(self, text): - self.text = text - - def set_html(self, html): - self.html = html - - def set_substitution_tag(self, substitution_tag): - self.substitution_tag = substitution_tag + self._enable = None + self._text = None + self._html = None + self._substitution_tag = None + + if enable is not None: + self.enable = enable + if text is not None: + self.text = text + if html is not None: + self.html = html + if substitution_tag is not None: + self.substitution_tag = substitution_tag + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value + + @property + def text(self): + return self._text + + @text.setter + def text(self, value): + self._text = value + + @property + def html(self): + return self._html + + @html.setter + def html(self, value): + self._html = value + + @property + def substitution_tag(self): + return self._substitution_tag + + @substitution_tag.setter + def substitution_tag(self, value): + self._substitution_tag = value def get(self): subscription_tracking = {} - if self.enable != None: + if self.enable is not None: subscription_tracking["enable"] = self.enable - if self.text != None: + if self.text is not None: subscription_tracking["text"] = self.text - if self.html != None: + if self.html is not None: subscription_tracking["html"] = self.html - if self.substitution_tag != None: + if self.substitution_tag is not None: subscription_tracking["substitution_tag"] = self.substitution_tag return subscription_tracking @@ -615,75 +1068,138 @@ def __init__(self, utm_term=None, utm_content=None, utm_campaign=None): - self.enable = enable if enable != None else None - self.utm_source = utm_source if utm_source != None else None - self.utm_medium = utm_medium if utm_medium != None else None - self.utm_term = utm_term if utm_term != None else None - self.utm_content = utm_content if utm_content != None else None - self.utm_campaign = utm_campaign if utm_campaign != None else None - - def set_enable(self, enable): - self.enable = enable - - def set_utm_source(self, utm_source): - self.utm_source = utm_source - - def set_utm_medium(self, utm_medium): - self.utm_medium = utm_medium - - def set_utm_term(self, utm_term): - self.utm_term = utm_term - - def set_utm_content(self, utm_content): - self.utm_content = utm_content - - def set_utm_campaign(self, utm_campaign): - self.utm_campaign = utm_campaign + self._enable = None + self._utm_source = None + self._utm_medium = None + self._utm_term = None + self._utm_content = None + self._utm_campaign = None + + if enable is not None: + self.enable = enable + if utm_source is not None: + self.utm_source = utm_source + if utm_medium is not None: + self.utm_medium = utm_medium + if utm_term is not None: + self.utm_term = utm_term + if utm_content is not None: + self.utm_content = utm_content + if utm_campaign is not None: + self.utm_campaign = utm_campaign + + @property + def enable(self): + return self._enable + + @enable.setter + def enable(self, value): + self._enable = value + + @property + def utm_source(self): + return self._utm_source + + @utm_source.setter + def utm_source(self, value): + self._utm_source = value + + @property + def utm_medium(self): + return self._utm_medium + + @utm_medium.setter + def utm_medium(self, value): + self._utm_medium = value + + @property + def utm_term(self): + return self._utm_term + + @utm_term.setter + def utm_term(self, value): + self._utm_term = value + + @property + def utm_content(self): + return self._utm_content + + @utm_content.setter + def utm_content(self, value): + self._utm_content = value + + @property + def utm_campaign(self): + return self._utm_campaign + + @utm_campaign.setter + def utm_campaign(self, value): + self._utm_campaign = value def get(self): ganalytics = {} - if self.enable != None: + if self.enable is not None: ganalytics["enable"] = self.enable - if self.utm_source != None: + if self.utm_source is not None: ganalytics["utm_source"] = self.utm_source - if self.utm_medium != None: + if self.utm_medium is not None: ganalytics["utm_medium"] = self.utm_medium - if self.utm_term != None: + if self.utm_term is not None: ganalytics["utm_term"] = self.utm_term - if self.utm_content != None: + if self.utm_content is not None: ganalytics["utm_content"] = self.utm_content - if self.utm_campaign != None: + if self.utm_campaign is not None: ganalytics["utm_campaign"] = self.utm_campaign return ganalytics class TrackingSettings(object): def __init__(self): - self.click_tracking = None - self.open_tracking = None - self.subscription_tracking = None - self.ganalytics = None + self._click_tracking = None + self._open_tracking = None + self._subscription_tracking = None + self._ganalytics = None + + @property + def click_tracking(self): + return self._click_tracking + + @click_tracking.setter + def click_tracking(self, value): + self._click_tracking = value + + @property + def open_tracking(self): + return self._open_tracking + + @open_tracking.setter + def open_tracking(self, value): + self._open_tracking = value - def set_click_tracking(self, click_tracking): - self.click_tracking = click_tracking + @property + def subscription_tracking(self): + return self._subscription_tracking - def set_open_tracking(self, open_tracking): - self.open_tracking = open_tracking + @subscription_tracking.setter + def subscription_tracking(self, value): + self._subscription_tracking = value - def set_subscription_tracking(self, subscription_tracking): - self.subscription_tracking = subscription_tracking + @property + def ganalytics(self): + return self._ganalytics - def set_ganalytics(self, ganalytics): - self.ganalytics = ganalytics + @ganalytics.setter + def ganalytics(self, value): + self._ganalytics = value def get(self): tracking_settings = {} - if self.click_tracking != None: + if self.click_tracking is not None: tracking_settings["click_tracking"] = self.click_tracking.get() - if self.open_tracking != None: + if self.open_tracking is not None: tracking_settings["open_tracking"] = self.open_tracking.get() - if self.subscription_tracking != None: + if self.subscription_tracking is not None: tracking_settings["subscription_tracking"] = self.subscription_tracking.get() - if self.ganalytics != None: + if self.ganalytics is not None: tracking_settings["ganalytics"] = self.ganalytics.get() return tracking_settings diff --git a/test/test_mail.py b/test/test_mail.py index 9b482dc9d..d1e6110ba 100644 --- a/test/test_mail.py +++ b/test/test_mail.py @@ -16,9 +16,9 @@ def test_helloEmail(self): """Minimum required to send an email""" mail = Mail() - mail.set_from(Email("test@example.com")) + mail.from_email = Email("test@example.com") - mail.set_subject("Hello World from the SendGrid Python Library") + mail.subject = "Hello World from the SendGrid Python Library" personalization = Personalization() personalization.add_to(Email("test@example.com")) @@ -35,9 +35,9 @@ def test_kitchenSink(self): """All settings set""" mail = Mail() - mail.set_from(Email("test@example.com", "Example User")) + mail.from_email = Email("test@example.com", "Example User") - mail.set_subject("Hello World from the SendGrid Python Library") + mail.subject = "Hello World from the SendGrid Python Library" personalization = Personalization() personalization.add_to(Email("test@example.com", "Example User")) @@ -46,14 +46,14 @@ def test_kitchenSink(self): personalization.add_cc(Email("test@example.com", "Example User")) personalization.add_bcc(Email("test@example.com")) personalization.add_bcc(Email("test@example.com")) - personalization.set_subject("Hello World from the Personalized SendGrid Python Library") + personalization.subject = "Hello World from the Personalized SendGrid Python Library" personalization.add_header(Header("X-Test", "test")) personalization.add_header(Header("X-Mock", "true")) personalization.add_substitution(Substitution("%name%", "Example User")) personalization.add_substitution(Substitution("%city%", "Denver")) personalization.add_custom_arg(CustomArg("user_id", "343")) personalization.add_custom_arg(CustomArg("type", "marketing")) - personalization.set_send_at(1443636843) + personalization.send_at = 1443636843 mail.add_personalization(personalization) personalization2 = Personalization() @@ -63,36 +63,36 @@ def test_kitchenSink(self): personalization2.add_cc(Email("test@example.com", "Example User")) personalization2.add_bcc(Email("test@example.com")) personalization2.add_bcc(Email("test@example.com")) - personalization2.set_subject("Hello World from the Personalized SendGrid Python Library") + personalization2.subject = "Hello World from the Personalized SendGrid Python Library" personalization2.add_header(Header("X-Test", "test")) personalization2.add_header(Header("X-Mock", "true")) personalization2.add_substitution(Substitution("%name%", "Example User")) personalization2.add_substitution(Substitution("%city%", "Denver")) personalization2.add_custom_arg(CustomArg("user_id", "343")) personalization2.add_custom_arg(CustomArg("type", "marketing")) - personalization2.set_send_at(1443636843) + personalization2.send_at = 1443636843 mail.add_personalization(personalization2) mail.add_content(Content("text/plain", "some text here")) mail.add_content(Content("text/html", "some text here")) attachment = Attachment() - attachment.set_content("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") - attachment.set_type("application/pdf") - attachment.set_filename("balance_001.pdf") - attachment.set_disposition("attachment") - attachment.set_content_id("Balance Sheet") + attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12" + attachment.type = "application/pdf" + attachment.filename = "balance_001.pdf" + attachment.disposition = "attachment" + attachment.content_id = "Balance Sheet" mail.add_attachment(attachment) attachment2 = Attachment() - attachment2.set_content("BwdW") - attachment2.set_type("image/png") - attachment2.set_filename("banner.png") - attachment2.set_disposition("inline") - attachment2.set_content_id("Banner") + attachment2.content = "BwdW" + attachment2.type = "image/png" + attachment2.filename = "banner.png" + attachment2.disposition = "inline" + attachment2.content_id = "Banner" mail.add_attachment(attachment2) - mail.set_template_id("13b8f94f-bcae-4ec6-b752-70d6cb59f932") + mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932" mail.add_section(Section("%section1%", "Substitution Text for Section 1")) mail.add_section(Section("%section2%", "Substitution Text for Section 2")) @@ -108,29 +108,29 @@ def test_kitchenSink(self): mail.add_custom_arg(CustomArg("campaign", "welcome")) mail.add_custom_arg(CustomArg("weekday", "morning")) - mail.set_send_at(1443636842) + mail.send_at = 1443636842 - mail.set_batch_id("sendgrid_batch_id") + mail.batch_id = "sendgrid_batch_id" - mail.set_asm(ASM(99, [4, 5, 6, 7, 8])) + mail.asm = ASM(99, [4, 5, 6, 7, 8]) - mail.set_ip_pool_name("24") + mail.ip_pool_name = "24" mail_settings = MailSettings() - mail_settings.set_bcc_settings(BCCSettings(True, Email("test@example.com"))) - mail_settings.set_bypass_list_management(BypassListManagement(True)) - mail_settings.set_footer_settings(FooterSettings(True, "Footer Text", "Footer Text")) - mail_settings.set_sandbox_mode(SandBoxMode(True)) - mail_settings.set_spam_check(SpamCheck(True, 1, "https://spamcatcher.sendgrid.com")) - mail.set_mail_settings(mail_settings) + mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com")) + mail_settings.bypass_list_management = BypassListManagement(True) + mail_settings.footer_settings = FooterSettings(True, "Footer Text", "Footer Text") + mail_settings.sandbox_mode = SandBoxMode(True) + mail_settings.spam_check = SpamCheck(True, 1, "https://spamcatcher.sendgrid.com") + mail.mail_settings = mail_settings tracking_settings = TrackingSettings() - tracking_settings.set_click_tracking(ClickTracking(True, True)) - tracking_settings.set_open_tracking(OpenTracking(True, "Optional tag to replace with the open image in the body of the message")) - tracking_settings.set_subscription_tracking(SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "html to insert into the text/html portion of the message", "Optional tag to replace with the open image in the body of the message")) - tracking_settings.set_ganalytics(Ganalytics(True, "some source", "some medium", "some term", "some content", "some campaign")) - mail.set_tracking_settings(tracking_settings) + tracking_settings.click_tracking = ClickTracking(True, True) + tracking_settings.open_tracking = OpenTracking(True, "Optional tag to replace with the open image in the body of the message") + tracking_settings.subscription_tracking = SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "html to insert into the text/html portion of the message", "Optional tag to replace with the open image in the body of the message") + tracking_settings.ganalytics = Ganalytics(True, "some source", "some medium", "some term", "some content", "some campaign") + mail.tracking_settings = tracking_settings - mail.set_reply_to(Email("test@example.com")) + mail.reply_to = Email("test@example.com") self.assertEqual(json.dumps(mail.get(), sort_keys=True), '{"asm": {"group_id": 99, "groups_to_display": [4, 5, 6, 7, 8]}, "attachments": [{"content": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12", "content_id": "Balance Sheet", "disposition": "attachment", "filename": "balance_001.pdf", "type": "application/pdf"}, {"content": "BwdW", "content_id": "Banner", "disposition": "inline", "filename": "banner.png", "type": "image/png"}], "batch_id": "sendgrid_batch_id", "categories": ["May", "2016"], "content": [{"type": "text/plain", "value": "some text here"}, {"type": "text/html", "value": "some text here"}], "custom_args": {"campaign": "welcome", "weekday": "morning"}, "from": {"email": "test@example.com", "name": "Example User"}, "headers": {"X-Test1": "test1", "X-Test3": "test2", "X-Test4": "test4"}, "ip_pool_name": "24", "mail_settings": {"bcc": {"email": "test@example.com", "enable": true}, "bypass_list_management": {"enable": true}, "footer": {"enable": true, "html": "Footer Text", "text": "Footer Text"}, "sandbox_mode": {"enable": true}, "spam_check": {"enable": true, "post_to_url": "https://spamcatcher.sendgrid.com", "threshold": 1}}, "personalizations": [{"bcc": [{"email": "test@example.com"}, {"email": "test@example.com"}], "cc": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}], "custom_args": {"type": "marketing", "user_id": "343"}, "headers": {"X-Mock": "true", "X-Test": "test"}, "send_at": 1443636843, "subject": "Hello World from the Personalized SendGrid Python Library", "substitutions": {"%city%": "Denver", "%name%": "Example User"}, "to": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}]}, {"bcc": [{"email": "test@example.com"}, {"email": "test@example.com"}], "cc": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}], "custom_args": {"type": "marketing", "user_id": "343"}, "headers": {"X-Mock": "true", "X-Test": "test"}, "send_at": 1443636843, "subject": "Hello World from the Personalized SendGrid Python Library", "substitutions": {"%city%": "Denver", "%name%": "Example User"}, "to": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}]}], "reply_to": {"email": "test@example.com"}, "sections": {"%section1%": "Substitution Text for Section 1", "%section2%": "Substitution Text for Section 2"}, "send_at": 1443636842, "subject": "Hello World from the SendGrid Python Library", "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932", "tracking_settings": {"click_tracking": {"enable": true, "enable_text": true}, "ganalytics": {"enable": true, "utm_campaign": "some campaign", "utm_content": "some content", "utm_medium": "some medium", "utm_source": "some source", "utm_term": "some term"}, "open_tracking": {"enable": true, "substitution_tag": "Optional tag to replace with the open image in the body of the message"}, "subscription_tracking": {"enable": true, "html": "html to insert into the text/html portion of the message", "substitution_tag": "Optional tag to replace with the open image in the body of the message", "text": "text to insert into the text/plain portion of the message"}}}') From 04bbcea2b3dd734a2120c401b9e94a3d53d6dd39 Mon Sep 17 00:00:00 2001 From: Denis Vlasov Date: Sat, 1 Apr 2017 00:04:12 +0300 Subject: [PATCH 2/2] fix: use asm.get() --- sendgrid/helpers/mail/mail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py index e84d770a7..59d218b8d 100644 --- a/sendgrid/helpers/mail/mail.py +++ b/sendgrid/helpers/mail/mail.py @@ -89,7 +89,7 @@ def get(self): mail["batch_id"] = self.batch_id if self.asm is not None: - mail["asm"] = self.asm + mail["asm"] = self.asm.get() if self.ip_pool_name is not None: mail["ip_pool_name"] = self.ip_pool_name