From 413b2de4dd6abf58f5b2b836516cd085aa7c0b58 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Sun, 29 Oct 2017 21:21:00 +0530 Subject: [PATCH 1/5] refactor personalization.py reduce Cognitive Complexity of get method and refactor init and other methods of personalization class --- sendgrid/helpers/mail/personalization.py | 70 +++++++++--------------- 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 35d99c6a7..11ecc9be2 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -1,14 +1,14 @@ 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 = [] + self._ccs = [] + self._bccs = [] + self._subject = [] + self._headers = [] + self._substitutions = [] + self._custom_args = [] + self._send_at = [] @property def tos(self): @@ -19,8 +19,6 @@ def tos(self, value): self._tos = value def add_to(self, email): - if self._tos is None: - self._tos = [] self._tos.append(email.get()) @property @@ -32,8 +30,6 @@ def ccs(self, value): self._ccs = value def add_cc(self, email): - if self._ccs is None: - self._ccs = [] self._ccs.append(email.get()) @property @@ -45,8 +41,6 @@ def bccs(self, value): self._bccs = value def add_bcc(self, email): - if self._bccs is None: - self._bccs = [] self._bccs.append(email.get()) @property @@ -66,8 +60,6 @@ def headers(self, value): self._headers = value def add_header(self, header): - if self._headers is None: - self._headers = [] self._headers.append(header.get()) @property @@ -79,8 +71,6 @@ def substitutions(self, value): self.substitutions = value def add_substitution(self, substitution): - if self._substitutions is None: - self._substitutions = [] self._substitutions.append(substitution.get()) @property @@ -92,8 +82,6 @@ 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()) @property @@ -106,36 +94,30 @@ def send_at(self, value): def get(self): personalization = {} - if self.tos is not None: + + if self.tos: personalization["to"] = self.tos - if self.ccs is not None: + if self.ccs: personalization["cc"] = self.ccs - if self.bccs is not None: + if self.bccs: personalization["bcc"] = self.bccs - if self.subject is not None: + if self.subject: personalization["subject"] = self.subject + + for prop_name in ['headers', 'substitutions', 'custom_args']: + prop = getattr(self, prop_name) + if prop: + obj = {} + for key in prop: + obj.update(key.get()) + personalization[prop_name] = obj + + for key in ["send_at", "subject"]: + value = getattr(self, key, None) + if value: + mail[key] = value - if self.headers is not None: - headers = {} - for key in self.headers: - headers.update(key) - personalization["headers"] = headers - - if self.substitutions is not None: - substitutions = {} - for key in self.substitutions: - substitutions.update(key) - personalization["substitutions"] = substitutions - - 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 is not None: - personalization["send_at"] = self.send_at return personalization From 639b2f036ffe913456cc6001ca0364deb74377b9 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Sun, 29 Oct 2017 21:23:28 +0530 Subject: [PATCH 2/5] fixes codeclimate --- sendgrid/helpers/mail/personalization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 11ecc9be2..f77f377ab 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -106,7 +106,7 @@ def get(self): if self.subject: personalization["subject"] = self.subject - + for prop_name in ['headers', 'substitutions', 'custom_args']: prop = getattr(self, prop_name) if prop: @@ -118,6 +118,6 @@ def get(self): for key in ["send_at", "subject"]: value = getattr(self, key, None) if value: - mail[key] = value + mail[key] = value return personalization From 36bd766137e52ae6df068de3332e5d17799586b8 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Sun, 29 Oct 2017 22:01:35 +0530 Subject: [PATCH 3/5] fix travis error also replacing `"` with "`" --- sendgrid/helpers/mail/personalization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index f77f377ab..f1114dd76 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -112,10 +112,10 @@ def get(self): if prop: obj = {} for key in prop: - obj.update(key.get()) + obj.update(key) personalization[prop_name] = obj - for key in ["send_at", "subject"]: + for key in ['send_at', 'subject']: value = getattr(self, key, None) if value: mail[key] = value From 66a64a6175b6a6a30a487cfc331d319d86e965e2 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Sun, 29 Oct 2017 22:05:01 +0530 Subject: [PATCH 4/5] fix spelling mistake --- sendgrid/helpers/mail/personalization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index f1114dd76..f068e51aa 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -116,8 +116,8 @@ def get(self): personalization[prop_name] = obj for key in ['send_at', 'subject']: - value = getattr(self, key, None) + value = getattr(self, key) if value: - mail[key] = value + personalization[key] = value return personalization From 4594f02c464d83f7c11f57adbecbb1afbec55ab1 Mon Sep 17 00:00:00 2001 From: Vikash Kumar Date: Wed, 1 Nov 2017 00:30:43 +0530 Subject: [PATCH 5/5] extra space and method description change --- sendgrid/helpers/mail/personalization.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sendgrid/helpers/mail/personalization.py b/sendgrid/helpers/mail/personalization.py index 982710741..5764d292f 100644 --- a/sendgrid/helpers/mail/personalization.py +++ b/sendgrid/helpers/mail/personalization.py @@ -4,7 +4,7 @@ class Personalization(object): """ def __init__(self): - """Create an empty Personalization.""" + """Create an empty Personalization and initialize member variables.""" self._tos = [] self._ccs = [] self._bccs = [] @@ -14,7 +14,6 @@ def __init__(self): self._custom_args = [] self._send_at = [] - @property def tos(self): """A list of recipients for this Personalization.