Skip to content
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

feat: remove duplicate emails ignoring case in Personalization #924

Merged
Merged
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
20 changes: 17 additions & 3 deletions sendgrid/helpers/mail/personalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ def add_email(self, email):
self.add_bcc(email)
return
raise ValueError('Please use a To, Cc or Bcc object.')

def _get_unique_recipients(self, recipients):
unique_recipients = []

for recipient in recipients:
recipient_email = recipient['email'].lower() if isinstance(recipient, dict) else recipient.email.lower()
if all(
unique_recipient['email'].lower() != recipient_email for unique_recipient in unique_recipients
):
new_unique_recipient = recipient if isinstance(recipient, dict) else recipient.get()
unique_recipients.append(new_unique_recipient)

return unique_recipients


@property
def tos(self):
"""A list of recipients for this Personalization.

:rtype: list(dict)
"""
return self._tos
return self._get_unique_recipients(self._tos)

@tos.setter
def tos(self, value):
Expand Down Expand Up @@ -69,7 +83,7 @@ def ccs(self):

:rtype: list(dict)
"""
return self._ccs
return self._get_unique_recipients(self._ccs)

@ccs.setter
def ccs(self, value):
Expand All @@ -89,7 +103,7 @@ def bccs(self):

:rtype: list(dict)
"""
return self._bccs
return self._get_unique_recipients(self._bccs)

@bccs.setter
def bccs(self, value):
Expand Down
218 changes: 218 additions & 0 deletions test/test_mail_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,224 @@ def test_error_is_not_raised_on_to_emails_includes_bcc_cc(self):
html_content=HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>'))

def test_personalization_add_email_filters_out_duplicate_to_emails(self):
self.maxDiff = None

p = Personalization()
to_email = To('[email protected]', 'Example To Name 0')
p.add_email(to_email)
p.add_email(to_email)

self.assertEqual([to_email.get()], p.tos)

def test_personalization_add_email_filters_out_duplicate_to_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
to_email = To('[email protected]', 'Example To Name 0')
to_email_with_caps = To('[email protected]', 'Example To Name 0')
p.add_email(to_email)
p.add_email(to_email_with_caps)

self.assertEqual([to_email.get()], p.tos)

def test_personalization_filters_out_duplicate_cc_emails(self):
self.maxDiff = None

p = Personalization()
cc_email = Cc('[email protected]', 'Example Cc Name 0')
p.add_email(cc_email)
p.add_email(cc_email)

self.assertEqual([cc_email.get()], p.ccs)

def test_personalization_filters_out_duplicate_cc_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
cc_email = Cc('[email protected]', 'Example Cc Name 0')
cc_email_with_caps = Cc('[email protected]', 'Example Cc Name 0')
p.add_email(cc_email)
p.add_email(cc_email_with_caps)

self.assertEqual([cc_email.get()], p.ccs)

def test_personalization_filters_out_duplicate_bcc_emails(self):
self.maxDiff = None

p = Personalization()
bcc_email = Bcc('[email protected]', 'Example Bcc Name 0')
p.add_email(bcc_email)
p.add_email(bcc_email)

self.assertEqual([bcc_email.get()], p.bccs)

def test_personalization_filters_out_duplicate_bcc_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
bcc_email = Bcc('[email protected]', 'Example Bcc Name 0')
bcc_email_with_caps = Bcc('[email protected]', 'Example Bcc Name 0')
p.add_email(bcc_email)
p.add_email(bcc_email_with_caps)

self.assertEqual([bcc_email.get()], p.bccs)

def test_personalization_tos_setter_filters_out_duplicate_dict_emails(self):
self.maxDiff = None

p = Personalization()
to_emails = [{ 'email': '[email protected]', 'name': 'Example To Name 0' }] * 2
p.tos = to_emails

self.assertEqual([to_emails[0]], p.tos)

def test_personalization_tos_setter_filters_out_duplicate_dict_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
to_email = { 'email': '[email protected]', 'name': 'Example To Name 0' }
to_email_with_caps = { 'email': '[email protected]', 'name': 'Example To Name 0' }
to_emails = [to_email, to_email_with_caps]
p.tos = to_emails

self.assertEqual([to_email], p.tos)

def test_personalization_tos_setter_filters_out_duplicate_to_emails(self):
self.maxDiff = None

p = Personalization()
to_emails = [To('[email protected]', 'Example To Name 0')] * 2
p.tos = to_emails

self.assertEqual([to_emails[0].get()], p.tos)


def test_personalization_tos_setter_filters_out_duplicate_to_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
to_email = To('[email protected]', 'Example To Name 0')
to_email_with_caps = To('[email protected]', 'Example To Name 0')
to_emails = [to_email, to_email_with_caps]
p.tos = to_emails

self.assertEqual([to_email.get()], p.tos)

def test_personalization_ccs_setter_filters_out_duplicate_dict_emails(self):
self.maxDiff = None

p = Personalization()
cc_emails = [{ 'email': '[email protected]', 'name': 'Example Cc Name 0' }] * 2
p.ccs = cc_emails

self.assertEqual([cc_emails[0]], p.ccs)

def test_personalization_ccs_setter_filters_out_duplicate_dict_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
cc_email = { 'email': '[email protected]', 'name': 'Example Cc Name 0' }
cc_email_with_caps = { 'email': '[email protected]', 'name': 'Example Cc Name 0' }
cc_emails = [cc_email, cc_email_with_caps]
p.ccs = cc_emails

self.assertEqual([cc_email], p.ccs)

def test_personalization_ccs_setter_filters_out_duplicate_cc_emails(self):
self.maxDiff = None

p = Personalization()
cc_emails = [Cc('[email protected]', 'Example Cc Name 0')] * 2
p.ccs = cc_emails

self.assertEqual([cc_emails[0].get()], p.ccs)

def test_personalization_ccs_setter_filters_out_duplicate_cc_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
cc_email = Cc('[email protected]', 'Example Cc Name 0')
cc_email_with_caps = Cc('[email protected]', 'Example Cc Name 0')
p.ccs = [cc_email, cc_email_with_caps]

self.assertEqual([cc_email.get()], p.ccs)

def test_personalization_bccs_setter_filters_out_duplicate_dict_emails(self):
self.maxDiff = None

p = Personalization()
bcc_emails = [{ 'email': '[email protected]', 'name': 'Example Bcc Name 0' }] * 2
p.bccs = bcc_emails

self.assertEqual([bcc_emails[0]], p.bccs)

def test_personalization_bccs_setter_filters_out_duplicate_dict_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
bcc_email = { 'email': '[email protected]', 'name': 'Example Bcc Name 0' }
bcc_email_with_caps = { 'email': '[email protected]', 'name': 'Example Bcc Name 0' }
bcc_emails = [bcc_email, bcc_email_with_caps]
p.bccs = bcc_emails

self.assertEqual([bcc_email], p.bccs)

def test_personalization_bccs_setter_filters_out_duplicate_bcc_emails(self):
self.maxDiff = None

p = Personalization()
bcc_emails = [Bcc('[email protected]', 'Example Bcc Name 0')] * 2
p.bccs = bcc_emails

self.assertEqual([bcc_emails[0].get()], p.bccs)

def test_personalization_bccs_setter_filters_out_duplicate_bcc_emails_ignoring_case(self):
self.maxDiff = None

p = Personalization()
bcc_email = Bcc('[email protected]', 'Example Bcc Name 0')
bcc_email_with_caps = Bcc('[email protected]', 'Example Bcc Name 0')
p.bccs = [bcc_email, bcc_email_with_caps]

self.assertEqual([bcc_email.get()], p.bccs)

def test_personalization_add_to_filters_out_duplicate_to_emails(self):
self.maxDiff = None

p = Personalization()
to_email = To('[email protected]', 'Example To Name 0')
p.add_to(to_email)
p.add_to(to_email)

expected = [to_email.get()]

self.assertEqual(expected, p.tos)

def test_personalization_add_bcc_filters_out_duplicate_bcc_emails(self):
self.maxDiff = None

p = Personalization()
bcc_email = Bcc('[email protected]', 'Example To Name 0')
p.add_bcc(bcc_email)
p.add_bcc(bcc_email)

expected = [bcc_email.get()]

self.assertEqual(expected, p.bccs)

def test_personalization_add_cc_filters_out_duplicate_cc_emails(self):
self.maxDiff = None

p = Personalization()
cc_email = Cc('[email protected]', 'Example To Name 0')
p.add_cc(cc_email)
p.add_cc(cc_email)

expected = [cc_email.get()]

self.assertEqual(expected, p.ccs)
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved

def test_dynamic_template_data(self):
self.maxDiff = None

Expand Down