Created
August 1, 2018 00:39
-
-
Save qgreg/4ea19b0241b8b74b88011e36b2641c3a to your computer and use it in GitHub Desktop.
Sendgrid introduced dynamic templates, but hasn't released the feature in the Python SDK. Here's a bit of a path in the meantime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Until Sendgrid updates the Python SDK to handle the new dynamic template data, you can use this Gist to | |
manage dynmaic template data in your python application. | |
The DynamicTemplateData is used to assign the key and value of the data, then NewPersonalizations is a subclass | |
of the existing Personalizations to which the DynamicTemplateData can be added. | |
This Gist takes inspiration from Sendgrid's Python SDK custom_args and CustomArgs. | |
Hopefully, other finds this useful and Sendgrid updates their Python SDK soon | |
""" | |
import sendgrid | |
from sendgrid.helpers.mail import * | |
class DynamicTemplateData(object): | |
""" Dynamic template data for your email | |
""" | |
def __init__(self, key=None, value=None): | |
"""Create a DynamicTemplateData with the given key and value.""" | |
self.key = key | |
self.value = value | |
@property | |
def key(self): | |
"""Key for this DynamicTemplateData. | |
:rtype: string | |
""" | |
return self._key | |
@key.setter | |
def key(self, value): | |
self._key = value | |
@property | |
def value(self): | |
"""Value of this DynamicTemplateData.""" | |
return self._value | |
@value.setter | |
def value(self, value): | |
self._value = value | |
def get(self): | |
""" | |
Get a JSON-ready representation of this CustomArg. | |
:returns: This DynamicTemplateData, ready for use in a request body. | |
:rtype: dict | |
""" | |
dynamic_template_data = {} | |
if self.key is not None and self.value is not None: | |
dynamic_template_data[self.key] = self.value | |
return dynamic_template_data | |
class NewPersonalization(Personalization): | |
"""A subclass of Personalization that adds dynamic_template_data | |
""" | |
def __init__(self): | |
"""Create an empty Personalization.""" | |
self._tos = [] | |
self._ccs = [] | |
self._bccs = [] | |
self._subject = None | |
self._headers = [] | |
self._substitutions = [] | |
self._custom_args = [] | |
self._send_at = None | |
self._dynamic_template_data = [] | |
@property | |
def dynamic_template_data(self): | |
"""A list of data for the dynamic data for this template. | |
:rtype: list(dict) | |
""" | |
return self._dynamic_template_data | |
@dynamic_template_data.setter | |
def dynamic_template_data(self, value): | |
self._dynamic_template_data = value | |
def add_dynamic_template_data(self, dynamic_template_data): | |
"""Add a dynamic_template_data to this Personalization. | |
:type dynamic_template_data: DynamicTemplateData | |
""" | |
self._dynamic_template_data.append(dynamic_template_data.get()) | |
def get(self): | |
""" | |
Get a JSON-ready representation of this Personalization. | |
:returns: This Personalization, ready for use in a request body. | |
:rtype: dict | |
""" | |
personalization = {} | |
if self.tos: | |
personalization["to"] = self.tos | |
if self.ccs: | |
personalization["cc"] = self.ccs | |
if self.bccs: | |
personalization["bcc"] = self.bccs | |
if self.subject is not None: | |
personalization["subject"] = self.subject | |
if self.headers: | |
headers = {} | |
for key in self.headers: | |
headers.update(key) | |
personalization["headers"] = headers | |
if self.substitutions: | |
substitutions = {} | |
for key in self.substitutions: | |
substitutions.update(key) | |
personalization["substitutions"] = substitutions | |
if self.custom_args: | |
custom_args = {} | |
for key in self.custom_args: | |
custom_args.update(key) | |
personalization["custom_args"] = custom_args | |
if self.send_at is not None: | |
personalization["send_at"] = self.send_at | |
if self.dynamic_template_data: | |
dynamic_template_data = {} | |
for key in self.dynamic_template_data: | |
dynamic_template_data.update(key) | |
personalization["dynamic_template_data"] = dynamic_template_data | |
return personalization | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have improved by adding some
super()
calls: https://gist.github.com/wojtek-fliposports/f73eafcf62fa9eb0500ab453d90f553e