From acf7baaf3a7cbd23b39f6f4aebba28d1a44b0c7e Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Wed, 3 May 2017 14:32:16 -0300 Subject: [PATCH 1/6] Implements custom_args --- sgbackend/mail.py | 6 +++++- sgbackend/version.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/sgbackend/mail.py b/sgbackend/mail.py index b902d9f..1896784 100644 --- a/sgbackend/mail.py +++ b/sgbackend/mail.py @@ -27,7 +27,8 @@ Email, Mail, Personalization, - Substitution + Substitution, + CustomArg ) @@ -103,6 +104,9 @@ def _build_sg_mail(self, email): if hasattr(email, 'substitutions'): for k, v in email.substitutions.items(): personalization.add_substitution(Substitution(k, v)) + if hasattr(email, 'custom_args'): + for k, v in email.custom_args.items(): + mail.add_custom_arg(CustomArg(k, v)) for k, v in email.extra_headers.items(): mail.add_header({k: v}) diff --git a/sgbackend/version.py b/sgbackend/version.py index 79ca81d..f66adc1 100644 --- a/sgbackend/version.py +++ b/sgbackend/version.py @@ -1,2 +1,2 @@ -version_info = (4, 0, 4) # pragma: no cover +version_info = (4, 0, 5) # pragma: no cover __version__ = '.'.join(str(v) for v in version_info) # pragma: no cover From e7119bc587b281fde60c9934c037727311686312 Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Wed, 3 May 2017 17:48:10 -0300 Subject: [PATCH 2/6] Upgrade the last version python-sendgrid --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c98c16e..fd2add0 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ license='MIT', description='SendGrid Backend for Django', long_description=open('./README.rst').read(), - install_requires=["sendgrid >= 3.5, < 4"], + install_requires=["sendgrid >= 3.5, < 4.0.4"], classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", From ea1633bf5f833d32549f0d7deac67844f2a30148 Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Wed, 3 May 2017 17:48:46 -0300 Subject: [PATCH 3/6] Improve SgBackend to send custom_args I updated the version of sendgrid and because of this I had to change how data is assigned to the helpers (Mail, Personalization and Attachment) classes. I also added the CustomArgs helper to be sent to sendgrid. --- sgbackend/mail.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/sgbackend/mail.py b/sgbackend/mail.py index 1896784..725e281 100644 --- a/sgbackend/mail.py +++ b/sgbackend/mail.py @@ -74,8 +74,10 @@ def _build_sg_mail(self, email): # sendgrid/helpers/mail/mail.py:164 if not from_name: from_name = None - mail.set_from(Email(from_email, from_name)) - mail.set_subject(email.subject) + + mail.from_email = Email(from_email, from_name) + + mail.subject = email.subject personalization = Personalization() for e in email.to: @@ -84,7 +86,7 @@ def _build_sg_mail(self, email): personalization.add_cc(Email(e)) for e in email.bcc: personalization.add_bcc(Email(e)) - personalization.set_subject(email.subject) + personalization.subject = email.subject mail.add_content(Content("text/plain", email.body)) if isinstance(email, EmailMultiAlternatives): for alt in email.alternatives: @@ -104,9 +106,11 @@ def _build_sg_mail(self, email): if hasattr(email, 'substitutions'): for k, v in email.substitutions.items(): personalization.add_substitution(Substitution(k, v)) + if hasattr(email, 'custom_args'): - for k, v in email.custom_args.items(): - mail.add_custom_arg(CustomArg(k, v)) + for item in email.custom_args: + for k, v in item.items(): + mail.add_custom_arg(CustomArg(k, v)) for k, v in email.extra_headers.items(): mail.add_header({k: v}) @@ -114,18 +118,18 @@ def _build_sg_mail(self, email): for attachment in email.attachments: if isinstance(attachment, MIMEBase): attach = Attachment() - attach.set_filename(attachment.get_filename()) - attach.set_content(base64.b64encode(attachment.get_payload())) + attach.filename = attachment.get_filename() + attach.content = base64.b64encode(attachment.get_payload()) mail.add_attachment(attach) elif isinstance(attachment, tuple): attach = Attachment() - attach.set_filename(attachment[0]) + attach.filename = attachment[0] base64_attachment = base64.b64encode(attachment[1]) if sys.version_info >= (3,): - attach.set_content(str(base64_attachment, 'utf-8')) + attach.content = str(base64_attachment, 'utf-8') else: - attach.set_content(base64_attachment) - attach.set_type(attachment[2]) + attach.content = base64_attachment + attach.type = attachment[2] mail.add_attachment(attach) mail.add_personalization(personalization) From ed7b99ff5434c8e4d7bf78612bfec16de2209977 Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Wed, 3 May 2017 17:55:55 -0300 Subject: [PATCH 4/6] Improve readme Added custom_args feature --- README.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.rst b/README.rst index ef9b76e..b04921b 100644 --- a/README.rst +++ b/README.rst @@ -64,6 +64,15 @@ Example "

This is a simple HTML email body

", "text/html" ) + # Custom args + # Dictionary values must be a string + + msg.custom_args = [ + {'invoice_code': '{}'.format(invoice.pk)}, + {'content_type': '{}'.format(ct.pk)}, + {'code': '{}'.format(invoice.code)}, + ] + mail.send() From 8f5eda3ce5ae493c310c9802d33988e781044a23 Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Thu, 4 May 2017 14:27:03 -0300 Subject: [PATCH 5/6] Fix template_id attribution --- sgbackend/mail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sgbackend/mail.py b/sgbackend/mail.py index 725e281..e69119f 100644 --- a/sgbackend/mail.py +++ b/sgbackend/mail.py @@ -1,5 +1,6 @@ from .version import __version__ + import base64 import sys from email.mime.base import MIMEBase @@ -102,7 +103,7 @@ def _build_sg_mail(self, email): mail.add_category(Category(c)) if hasattr(email, 'template_id'): - mail.set_template_id(email.template_id) + mail.template_id = email.template_id if hasattr(email, 'substitutions'): for k, v in email.substitutions.items(): personalization.add_substitution(Substitution(k, v)) From 8df68d527f1783392dca7e84bbf04606120c4ac7 Mon Sep 17 00:00:00 2001 From: Lucas Simon Date: Thu, 4 May 2017 14:27:26 -0300 Subject: [PATCH 6/6] Create a test of custom_args mail object --- tests/test_mail.py | 67 +++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/tests/test_mail.py b/tests/test_mail.py index ef1f10d..b4f16e1 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -1,3 +1,4 @@ +import pytest from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.core.mail import EmailMessage @@ -94,12 +95,13 @@ def test_build_sg_email_w_categories(self): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, - {'categories': ['name'], - 'content': [{'type': 'text/plain', 'value': ''}], - 'from': {'email': 'webmaster@localhost'}, - 'personalizations': [{'subject': ''}], - 'subject': '' - } + { + 'categories': ['name'], + 'content': [{'type': 'text/plain', 'value': ''}], + 'from': {'email': 'webmaster@localhost'}, + 'personalizations': [{'subject': ''}], + 'subject': '' + } ) def test_build_sg_email_w_template_id(self): @@ -109,12 +111,13 @@ def test_build_sg_email_w_template_id(self): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, - {'template_id': 'template_id_123456', - 'content': [{'type': 'text/plain', 'value': ''}], - 'from': {'email': 'webmaster@localhost'}, - 'personalizations': [{'subject': ''}], - 'subject': '' - } + { + 'template_id': 'template_id_123456', + 'content': [{'type': 'text/plain', 'value': ''}], + 'from': {'email': 'webmaster@localhost'}, + 'personalizations': [{'subject': ''}], + 'subject': '' + } ) def test_build_sg_email_w_substitutions(self): @@ -124,10 +127,12 @@ def test_build_sg_email_w_substitutions(self): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, - {'content': [{'type': 'text/plain', 'value': ''}], - 'from': {'email': 'webmaster@localhost'}, - 'personalizations': [{'subject': ''}], - 'subject': ''} + { + 'content': [{'type': 'text/plain', 'value': ''}], + 'from': {'email': 'webmaster@localhost'}, + 'personalizations': [{'subject': ''}], + 'subject': '' + } ) def test_build_sg_email_w_extra_headers(self): @@ -137,9 +142,29 @@ def test_build_sg_email_w_extra_headers(self): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, - {'content': [{'type': 'text/plain', 'value': ''}], - 'from': {'email': 'webmaster@localhost'}, - 'headers': {'EXTRA_HEADER': 'VALUE'}, - 'personalizations': [{'subject': ''}], - 'subject': ''} + { + 'content': [{'type': 'text/plain', 'value': ''}], + 'from': {'email': 'webmaster@localhost'}, + 'headers': {'EXTRA_HEADER': 'VALUE'}, + 'personalizations': [{'subject': ''}], + 'subject': '' + } + ) + + def test_build_sg_email_w_custom_args(self): + msg = EmailMessage() + msg.custom_args = [ + {'pk': '{}'.format('5')} + ] + with self.settings(SENDGRID_API_KEY='test_key'): + mail = SendGridBackend()._build_sg_mail(msg) + self.assertEqual( + mail, + { + 'content': [{'type': 'text/plain', 'value': ''}], + 'custom_args': {'pk': '5'}, + 'from': {'email': 'webmaster@localhost'}, + 'personalizations': [{'subject': ''}], + 'subject': '' + } )