From 746cc769229cbe40eaa954843e2ca611c6ad4619 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:49:32 -0700 Subject: [PATCH 1/4] feat: filter-clicktrack --- tasks.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 1d92cbb..145f2d4 100644 --- a/tasks.py +++ b/tasks.py @@ -12,7 +12,7 @@ import celery from python_http_client.exceptions import HTTPError import sendgrid -from sendgrid.helpers.mail import (Mail, From, To, Cc, Bcc, Personalization, Subject, Asm, GroupId, GroupsToDisplay) +from sendgrid.helpers.mail import (Mail, From, To, Cc, Bcc, Personalization, Subject, Asm, GroupId, GroupsToDisplay, TrackingSettings, ClickTracking) from jinja2 import Environment, BaseLoader from jinja2.filters import FILTERS, pass_environment from jinja2.exceptions import TemplateNotFound @@ -155,6 +155,19 @@ def generate_message(data): message.asm = Asm(GroupId(data['asm']['group_id']), GroupsToDisplay(data['asm']['groups_to_display'])) + # https://docs.sendgrid.com/for-developers/sending-email/smtp-filters#filter-clicktrack + if 'filters' in data \ + and 'clicktrack' in data['filters'] \ + and 'settings' in data['filters']['clicktrack']: + + clicktrack_settings = data['filters']['clicktrack']['settings'] + clicktrack_enable = bool(clicktrack_settings['enable']) + clicktrack_enable_text = bool(clicktrack_settings['enable_text']) + + tracking_settings = TrackingSettings() + tracking_settings.click_tracking = ClickTracking(clicktrack_enable, clicktrack_enable_text) + message.tracking_settings = tracking_settings + func_switcher = { "content": HelperService.get_content, "attachments": HelperService.get_attachments, From 2c35703283d68d4540f27e5494284da96a9cdd27 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:58:50 -0700 Subject: [PATCH 2/4] chore: default clicktrack to False --- tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks.py b/tasks.py index 145f2d4..f053a39 100644 --- a/tasks.py +++ b/tasks.py @@ -161,8 +161,8 @@ def generate_message(data): and 'settings' in data['filters']['clicktrack']: clicktrack_settings = data['filters']['clicktrack']['settings'] - clicktrack_enable = bool(clicktrack_settings['enable']) - clicktrack_enable_text = bool(clicktrack_settings['enable_text']) + clicktrack_enable = bool(clicktrack_settings.get('enable', False)) + clicktrack_enable_text = bool(clicktrack_settings.get('enable_text', False)) tracking_settings = TrackingSettings() tracking_settings.click_tracking = ClickTracking(clicktrack_enable, clicktrack_enable_text) From 2e37407e84ad6aff9f626ca94aa35147eb2840c6 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Mon, 23 Oct 2023 01:13:41 -0700 Subject: [PATCH 3/4] test: add filters to mocks --- tests/mocks.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/mocks.py b/tests/mocks.py index a7081f7..fbfe1b1 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -41,6 +41,14 @@ "asm": { "group_id": 1, "groups_to_display": [1, 2] + }, + "filters" : { + "clicktrack" : { + "settings" : { + "enable" : 0, + "enable_text" : False + } + } } } From 7d0fb6296b39a9d9837a55886fa75c132869143a Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Mon, 23 Oct 2023 01:14:38 -0700 Subject: [PATCH 4/4] docs: add filter clicktrack to README --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index ab983b7..1d0db17 100755 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ brew services list ``` #### Environment and Dependencies + Install Pipenv (if needed) ```bash @@ -161,6 +162,38 @@ You can provide attachments either inline (with the base64-encoded `content` fie } ``` +### Filter: clicktrack + +[docs.sendgrid.com](https://docs.sendgrid.com/for-developers/sending-email/smtp-filters#filter-clicktrack) + +```json +{ + "subject": "Hi Diddly Ho", + "to": [{ + "email": "homer@springfield.com", + "name": "Homer Simpson" + }], + "from": { + "email": "ned@flandersfamily.com", + "name": "Ned Flanders" + }, + "content": [ + { + "type": "text/plain", + "value": "Try search with https://google.com" + } + ], + "filters" : { + "clicktrack" : { + "settings" : { + "enable" : 0, + "enable_text" : false + } + } + } +} +``` + ## Testing Code coverage command with missing statement line numbers