Skip to content

Permit unicode string values with Substitution helper #335

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

Merged
merged 2 commits into from
Aug 31, 2017
Merged
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
4 changes: 2 additions & 2 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ def key(self):

@key.setter
def key(self, value):
self._key = str(value)
self._key = value

@property
def value(self):
return self._value

@value.setter
def value(self, value):
self._value = str(value)
self._value = value

def get(self):
substitution = {}
Expand Down
59 changes: 59 additions & 0 deletions test/test_mail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import json

from sendgrid.helpers.mail import (
Expand Down Expand Up @@ -406,3 +407,61 @@ def test_kitchenSink(self):
json.dumps(mail.get(), sort_keys=True),
json.dumps(expected_result, sort_keys=True)
)

def test_unicode_values_in_substitutions_helper(self):

""" Test that the Substitutions helper accepts unicode values """

self.maxDiff = None

"""Minimum required to send an email"""
mail = Mail()

mail.from_email = Email("[email protected]")

mail.subject = "Testing unicode substitutions with the SendGrid Python Library"

personalization = Personalization()
personalization.add_to(Email("[email protected]"))
personalization.add_substitution(Substitution("%city%", u"Αθήνα"))
mail.add_personalization(personalization)

mail.add_content(Content("text/plain", "some text here"))
mail.add_content(
Content(
"text/html",
"<html><body>some text here</body></html>"))

expected_result = {
"content": [
{
"type": "text/plain",
"value": "some text here"
},
{
"type": "text/html",
"value": "<html><body>some text here</body></html>"
}
],
"from": {
"email": "[email protected]"
},
"personalizations": [
{
"substitutions": {
"%city%": u"Αθήνα"
},
"to": [
{
"email": "[email protected]"
}
]
}
],
"subject": "Testing unicode substitutions with the SendGrid Python Library",
}

self.assertEqual(
json.dumps(mail.get(), sort_keys=True),
json.dumps(expected_result, sort_keys=True)
)