Skip to content

Commit

Permalink
Merge branch 'mark5280-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Sklar committed Dec 12, 2019
2 parents 4977069 + d1e835e commit 7b1a291
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ build/
.noseids
.vscode/
.tox

\.eggs/
.idea
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ To use the backend, simply install the package (using pip), set the `EMAIL_BACKE
1. To err on the side of caution, this defaults to True, so emails sent in DEBUG mode will not be delivered, unless this setting is explicitly set to False.
2. `SENDGRID_ECHO_TO_STDOUT` will echo to stdout or any other file-like
object that is passed to the backend via the `stream` kwarg.
3. `SENDGRID_TRACK_EMAIL_OPENS` - defaults to true and tracks email open events via the Sendgrid service. These events are logged in the Statistics UI, Email Activity interface, and are reported by the Event Webhook.
4. `SENDGRID_TRACK_CLICKS_HTML` - defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the HTML message sent.
5. `SENDGRID_TRACK_CLICKS_PLAIN` - defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the plain text message sent.
24 changes: 13 additions & 11 deletions sendgrid_backend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sendgrid
from sendgrid.helpers.mail import (
Attachment, Category, Content, Email, Header, Mail, MailSettings, OpenTracking,
ClickTracking, SubscriptionTracking, Personalization, SandBoxMode, Substitution, TrackingSettings, CustomArg
Personalization, SandBoxMode, Substitution, TrackingSettings, CustomArg, ClickTracking
)

from python_http_client.exceptions import HTTPError
Expand Down Expand Up @@ -67,15 +67,17 @@ def __init__(self, *args, **kwargs):
track_email = settings.SENDGRID_TRACK_EMAIL_OPENS
self.track_email = track_email

track_click = True
if hasattr(settings, "SENDGRID_TRACK_EMAIL_CLICKS"):
track_click = settings.SENDGRID_TRACK_EMAIL_CLICKS
self.track_click = track_click
track_clicks_html = True
if hasattr(settings, "SENDGRID_TRACK_CLICKS_HTML"):
track_clicks_html = settings.SENDGRID_TRACK_CLICKS_HTML

subscription = False
if hasattr(settings, "SENDGRID_SUBSCRIPTION_ENABLE"):
subscription = settings.SENDGRID_SUBSCRIPTION_ENABLE
self.subscription = subscription
self.track_clicks_html = track_clicks_html

track_clicks_plain = True
if hasattr(settings, "SENDGRID_TRACK_CLICKS_PLAIN"):
track_clicks_plain = settings.SENDGRID_TRACK_CLICKS_PLAIN

self.track_clicks_plain = track_clicks_plain

if hasattr(settings, "SENDGRID_ECHO_TO_STDOUT") and settings.SENDGRID_ECHO_TO_STDOUT:
self._lock = threading.RLock()
Expand Down Expand Up @@ -300,8 +302,8 @@ def _build_sg_mail(self, msg):

tracking_settings = TrackingSettings()
tracking_settings.open_tracking = OpenTracking(self.track_email)
tracking_settings.click_tracking = ClickTracking(self.track_click)
tracking_settings.subscription_tracking = SubscriptionTracking(self.subscription)
tracking_settings.click_tracking = ClickTracking(self.track_clicks_html, self.track_clicks_plain)

mail.tracking_settings = tracking_settings

return mail.get()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="django-sendgrid-v5",
version="0.8.0",
version="0.8.1",
description="An implementation of Django's EmailBackend compatible with sendgrid-python v5+",
long_description=long_description,
url="https://github.com/sklarsa/django-sendgrid-v5",
Expand Down
36 changes: 15 additions & 21 deletions test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ def test_EmailMessage(self):
"name": "Sam Smith"
},
"subject": "Hello, World!",
"tracking_settings": {
"click_tracking": {"enable": True},
'open_tracking': {'enable': True},
'subscription_tracking': {'enable': False}},
"tracking_settings": {"click_tracking": {"enable": True, "enable_text": True},
"open_tracking": {"enable": True}
},
"content": [{
"type": "text/plain",
"value": "Hello, World!"
Expand Down Expand Up @@ -121,10 +120,9 @@ def test_EmailMessage_attributes(self):
}
},
"subject": "Hello, World!",
"tracking_settings": {
"click_tracking": {"enable": True},
'open_tracking': {'enable': True},
'subscription_tracking': {'enable': False}},
"tracking_settings": {"click_tracking": {"enable": True, "enable_text": True},
"open_tracking": {"enable": True}
},
"content": [{
"type": "text/plain",
"value": "Hello, World!"
Expand Down Expand Up @@ -183,10 +181,9 @@ def test_EmailMultiAlternatives(self):
"name": "Sam Smith"
},
"subject": "Hello, World!",
"tracking_settings": {
"click_tracking": {"enable": True},
'open_tracking': {'enable': True},
'subscription_tracking': {'enable': False}},
"tracking_settings": {"click_tracking": {"enable": True, "enable_text": True},
"open_tracking": {"enable": True}
},
"attachments": [{
"content": "MSwyLDMsNA==",
"filename": "file.csv",
Expand Down Expand Up @@ -262,10 +259,9 @@ def test_EmailMultiAlternatives__unicode_attachment(self):
"name": "Sam Smith"
},
"subject": "Hello, World!",
"tracking_settings": {
"click_tracking": {"enable": True},
'open_tracking': {'enable': True},
'subscription_tracking': {'enable': False}},
"tracking_settings": {"click_tracking": {"enable": True, "enable_text": True},
"open_tracking": {"enable": True}
},
"attachments": [
{
"content": "0A==",
Expand Down Expand Up @@ -428,11 +424,9 @@ def test_EmailMessage_custom_args(self):
"name": "Sam Smith"
},
"subject": "Hello, World!",
"tracking_settings": {
"click_tracking": {"enable": True},
'open_tracking': {'enable': True},
'subscription_tracking': {'enable': False}
},
"tracking_settings": {"click_tracking": {"enable": True, "enable_text": True},
"open_tracking": {"enable": True}
},
"content": [{
"type": "text/plain",
"value": "Hello, World!"
Expand Down

0 comments on commit 7b1a291

Please sign in to comment.