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

Custom_Args Feature #53

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ Example
"<p>This is a simple HTML email body</p>", "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()


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change implies that django-sendgrid is compatible with sendgrid 4. Is this actually the case? And if so, why don't we correct the entire version range?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the latest version of the python-sendgrid package is version 4.0.0. It was unnecessary that I changed it to 4.0.4. I do not remember why I did it.

$ Pip freeze | Grep -i sendgrid
Sendgrid == 4.0.0
-e git + https: //github.com/lucassimon/sendgrid-django.git@8df68d527f1783392dca7e84bbf04606120c4ac7#egg=sendgrid_django

And if so, why do not we fix the whole range of versions?

What are the other versions that should be changed? The same dependency is only from python-sendgrid and changed the main version of this package:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand that the python-sendgrid package has hit version 4. I also understand that we previously did not allow django-sendgrid to be installed alongside python-sendgrid version 4.

This change now allows for django-sendgrid to be installed along python-sendgrid version 4. Does django-sendgrid actually support python-sendgrid version 4?

classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
Expand Down
31 changes: 20 additions & 11 deletions sgbackend/mail.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .version import __version__


import base64
import sys
from email.mime.base import MIMEBase
Expand Down Expand Up @@ -27,7 +28,8 @@
Email,
Mail,
Personalization,
Substitution
Substitution,
CustomArg
)


Expand Down Expand Up @@ -73,8 +75,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:
Expand All @@ -83,7 +87,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:
Expand All @@ -99,29 +103,34 @@ 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))

if hasattr(email, 'custom_args'):
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})

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)
Expand Down
2 changes: 1 addition & 1 deletion sgbackend/version.py
Original file line number Diff line number Diff line change
@@ -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
67 changes: 46 additions & 21 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import EmailMessage
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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': ''
}
)