Skip to content

Commit 64b824c

Browse files
committed
upgraded SendGrid API support
always forget the version number
1 parent f1eca89 commit 64b824c

File tree

5 files changed

+65
-66
lines changed

5 files changed

+65
-66
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ cache:
99
- pip
1010
install:
1111
- pip install -r requirements-test.txt -e .
12-
- pip install coveralls
1312
script: python setup.py test
1413
after_success:
1514
- coveralls

flask_sendgrid.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
from sendgrid.helpers.mail import Email, Content, Personalization
1010

1111

12+
__version__ = '0.6'
13+
__versionfull__ = __version__
14+
15+
1216
class SendGrid(SGMail):
1317
app = None
1418
api_key = None
@@ -21,36 +25,36 @@ def __init__(self, app=None, **opts):
2125
super(SGMail, self).__init__()
2226
self.from_email = None
2327
self.subject = None
24-
self.personalizations = None
25-
self.contents = None
26-
self.attachments = None
27-
self.template_id = None
28-
self.sections = None
29-
self.headers = None
30-
self.categories = None
31-
self.custom_args = None
32-
self.send_at = None
33-
self.batch_id = None
34-
self.asm = None
35-
self.ip_pool_name = None
36-
self.mail_settings = None
37-
self.tracking_settings = None
38-
self.reply_to = None
28+
self._personalizations = None
29+
self._contents = None
30+
self._attachments = None
31+
self._template_id = None
32+
self._sections = None
33+
self._headers = None
34+
self._categories = None
35+
self._custom_args = None
36+
self._send_at = None
37+
self._batch_id = None
38+
self._asm = None
39+
self._ip_pool_name = None
40+
self._mail_settings = None
41+
self._tracking_settings = None
42+
self._reply_to = None
3943

4044
def init_app(self, app):
4145
self.app = app
4246
self.api_key = app.config['SENDGRID_API_KEY']
4347
self.default_from = app.config['SENDGRID_DEFAULT_FROM']
4448
self.client = SendGridAPIClient(apikey=self.api_key).client
4549

46-
def send_email(self, to_email, subject, from_email=None, html=None, text=None, *args, **kwargs):
50+
def send_email(self, to_email, subject, from_email=None, html=None, text=None, *args, **kwargs): # noqa
4751
if not any([from_email, self.default_from]):
4852
raise ValueError("Missing from email and no default.")
4953
if not any([html, text]):
5054
raise ValueError("Missing html or text.")
5155

52-
self.set_from(Email(from_email or self.default_from))
53-
self.set_subject(subject)
56+
self.from_email = Email(from_email or self.default_from)
57+
self.subject = subject
5458

5559
personalization = Personalization()
5660

@@ -76,6 +80,3 @@ def _extract_emails(self, emails):
7680
elif type(emails[0]) is dict:
7781
for email in emails:
7882
yield Email(email['email'])
79-
80-
__version__ = '0.5.2'
81-
__versionfull__ = __version__

requirements-test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
flask
12
pyflakes==1.2.3
23
pytest==2.9.2
34
pytest-cache==1.0
@@ -7,3 +8,4 @@ pytest-pep8==1.0.6
78
pep8==1.7.0
89
coverage==4.2
910
mock==2.0.0
11+
coveralls

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ def run_tests(self):
8888
py_modules=['flask_sendgrid'],
8989
zip_safe=False,
9090
platforms='any',
91-
install_requires=[
92-
'Flask',
93-
'SendGrid'],
91+
install_requires=['SendGrid'],
9492
tests_require=get_requirements('-test'),
9593
cmdclass={'test': PyTest},
9694
classifiers=[

tests/test_extension.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_mandrill_compat_email_send(self, mock_client):
5656
html='<h2>html</h2>'
5757
)
5858

59-
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"email": "from"}, "personalizations": [{"to": [{"email": "test@example.com"}]}], "subject": "Subject"}')
59+
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"name": "from"}, "personalizations": [{"to": [{"email": "test@example.com"}]}], "subject": "Subject"}')
6060

6161
@patch('python_http_client.Client._make_request')
6262
def test_mandrill_compat_single_recipient(self, mock_client):
@@ -70,7 +70,7 @@ def test_mandrill_compat_single_recipient(self, mock_client):
7070
html='<h2>html</h2>'
7171
)
7272

73-
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"email": "from"}, "personalizations": [{"to": [{"email": "test@example.com"}]}], "subject": "Subject"}')
73+
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"name": "from"}, "personalizations": [{"to": [{"email": "test@example.com"}]}], "subject": "Subject"}')
7474

7575
@patch('python_http_client.Client._make_request')
7676
def test_mandrill_compat_multiple_recipient(self, mock_client):
@@ -84,7 +84,7 @@ def test_mandrill_compat_multiple_recipient(self, mock_client):
8484
html='<h2>html</h2>'
8585
)
8686

87-
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"email": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}, {"email": "test2@example.com"}]}], "subject": "Subject"}')
87+
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"name": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}, {"email": "test2@example.com"}]}], "subject": "Subject"}')
8888

8989
@patch('python_http_client.Client._make_request')
9090
def test_single_recipient_email_object(self, mock_client):
@@ -98,7 +98,7 @@ def test_single_recipient_email_object(self, mock_client):
9898
html='<h2>html</h2>'
9999
)
100100

101-
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"email": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}]}], "subject": "Subject"}')
101+
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"name": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}]}], "subject": "Subject"}')
102102

103103
@patch('python_http_client.Client._make_request')
104104
def test_multiple_recipient_email_object(self, mock_client):
@@ -112,7 +112,7 @@ def test_multiple_recipient_email_object(self, mock_client):
112112
html='<h2>html</h2>'
113113
)
114114

115-
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"email": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}, {"email": "test2@example.com"}]}], "subject": "Subject"}')
115+
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"content": [{"type": "text/html", "value": "<h2>html</h2>"}], "from": {"name": "from"}, "personalizations": [{"to": [{"email": "test1@example.com"}, {"email": "test2@example.com"}]}], "subject": "Subject"}')
116116

117117
@patch('python_http_client.Client._make_request')
118118
def test_hello_email(self, mock_client):
@@ -124,9 +124,8 @@ def test_hello_email(self, mock_client):
124124

125125
"""Minimum required to send an email"""
126126

127-
self.mail.set_from(Email("test@example.com"))
128-
129-
self.mail.set_subject("Hello World from the SendGrid Python Library")
127+
self.mail.from_email = Email("test@example.com")
128+
self.mail.subject = "Hello World from the SendGrid Python Library"
130129

131130
personalization = Personalization()
132131
personalization.add_to(Email("test@example.com"))
@@ -147,9 +146,9 @@ def test_kitchenSink(self, mock_client):
147146

148147
"""All settings set"""
149148

150-
self.mail.set_from(Email("test@example.com", "Example User"))
149+
self.mail.from_email = Email("test@example.com", "Example User")
151150

152-
self.mail.set_subject("Hello World from the SendGrid Python Library")
151+
self.mail.subject = "Hello World from the SendGrid Python Library"
153152

154153
personalization = Personalization()
155154
personalization.add_to(Email("test@example.com", "Example User"))
@@ -158,14 +157,14 @@ def test_kitchenSink(self, mock_client):
158157
personalization.add_cc(Email("test@example.com", "Example User"))
159158
personalization.add_bcc(Email("test@example.com"))
160159
personalization.add_bcc(Email("test@example.com"))
161-
personalization.set_subject("Hello World from the Personalized SendGrid Python Library")
160+
personalization.subject = "Hello World from the Personalized SendGrid Python Library"
162161
personalization.add_header(Header("X-Test", "test"))
163162
personalization.add_header(Header("X-Mock", "true"))
164163
personalization.add_substitution(Substitution("%name%", "Example User"))
165164
personalization.add_substitution(Substitution("%city%", "Denver"))
166165
personalization.add_custom_arg(CustomArg("user_id", "343"))
167166
personalization.add_custom_arg(CustomArg("type", "marketing"))
168-
personalization.set_send_at(1443636843)
167+
personalization.send_at = 1443636843
169168
self.mail.add_personalization(personalization)
170169

171170
personalization2 = Personalization()
@@ -175,36 +174,36 @@ def test_kitchenSink(self, mock_client):
175174
personalization2.add_cc(Email("test@example.com", "Example User"))
176175
personalization2.add_bcc(Email("test@example.com"))
177176
personalization2.add_bcc(Email("test@example.com"))
178-
personalization2.set_subject("Hello World from the Personalized SendGrid Python Library")
177+
personalization2.subject = "Hello World from the Personalized SendGrid Python Library"
179178
personalization2.add_header(Header("X-Test", "test"))
180179
personalization2.add_header(Header("X-Mock", "true"))
181180
personalization2.add_substitution(Substitution("%name%", "Example User"))
182181
personalization2.add_substitution(Substitution("%city%", "Denver"))
183182
personalization2.add_custom_arg(CustomArg("user_id", "343"))
184183
personalization2.add_custom_arg(CustomArg("type", "marketing"))
185-
personalization2.set_send_at(1443636843)
184+
personalization2.send_at = 1443636843
186185
self.mail.add_personalization(personalization2)
187186

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

191190
attachment = Attachment()
192-
attachment.set_content("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12")
193-
attachment.set_type("application/pdf")
194-
attachment.set_filename("balance_001.pdf")
195-
attachment.set_disposition("attachment")
196-
attachment.set_content_id("Balance Sheet")
191+
attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12"
192+
attachment.type = "application/pdf"
193+
attachment.filename = "balance_001.pdf"
194+
attachment.disposition = "attachment"
195+
attachment.content_id = "Balance Sheet"
197196
self.mail.add_attachment(attachment)
198197

199198
attachment2 = Attachment()
200-
attachment2.set_content("BwdW")
201-
attachment2.set_type("image/png")
202-
attachment2.set_filename("banner.png")
203-
attachment2.set_disposition("inline")
204-
attachment2.set_content_id("Banner")
199+
attachment2.content = "BwdW"
200+
attachment2.type = "image/png"
201+
attachment2.filename = "banner.png"
202+
attachment2.disposition = "inline"
203+
attachment2.content_id = "Banner"
205204
self.mail.add_attachment(attachment2)
206205

207-
self.mail.set_template_id("13b8f94f-bcae-4ec6-b752-70d6cb59f932")
206+
self.mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
208207

209208
self.mail.add_section(Section("%section1%", "Substitution Text for Section 1"))
210209
self.mail.add_section(Section("%section2%", "Substitution Text for Section 2"))
@@ -220,29 +219,29 @@ def test_kitchenSink(self, mock_client):
220219
self.mail.add_custom_arg(CustomArg("campaign", "welcome"))
221220
self.mail.add_custom_arg(CustomArg("weekday", "morning"))
222221

223-
self.mail.set_send_at(1443636842)
222+
self.mail.send_at = 1443636842
224223

225-
self.mail.set_batch_id("sendgrid_batch_id")
224+
self.mail.batch_id = "sendgrid_batch_id"
226225

227-
self.mail.set_asm(ASM(99, [4, 5, 6, 7, 8]))
226+
self.mail.asm = ASM(99, [4, 5, 6, 7, 8])
228227

229-
self.mail.set_ip_pool_name("24")
228+
self.mail.ip_pool_name = "24"
230229

231230
mail_settings = MailSettings()
232-
mail_settings.set_bcc_settings(BCCSettings(True, Email("test@example.com")))
233-
mail_settings.set_bypass_list_management(BypassListManagement(True))
234-
mail_settings.set_footer_settings(FooterSettings(True, "Footer Text", "<html><body>Footer Text</body></html>"))
235-
mail_settings.set_sandbox_mode(SandBoxMode(True))
236-
mail_settings.set_spam_check(SpamCheck(True, 1, "https://spamcatcher.sendgrid.com"))
237-
self.mail.set_mail_settings(mail_settings)
231+
mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com"))
232+
mail_settings.bypass_list_management = BypassListManagement(True)
233+
mail_settings.footer_settings = FooterSettings(True, "Footer Text", "<html><body>Footer Text</body></html>")
234+
mail_settings.sandbox_mode = SandBoxMode(True)
235+
mail_settings.spam_check = SpamCheck(True, 1, "https://spamcatcher.sendgrid.com")
236+
self.mail.mail_settings = mail_settings
238237

239238
tracking_settings = TrackingSettings()
240-
tracking_settings.set_click_tracking(ClickTracking(True, True))
241-
tracking_settings.set_open_tracking(OpenTracking(True, "Optional tag to replace with the open image in the body of the message"))
242-
tracking_settings.set_subscription_tracking(SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "<html><body>html to insert into the text/html portion of the message</body></html>", "Optional tag to replace with the open image in the body of the message"))
243-
tracking_settings.set_ganalytics(Ganalytics(True, "some source", "some medium", "some term", "some content", "some campaign"))
244-
self.mail.set_tracking_settings(tracking_settings)
239+
tracking_settings.click_tracking = ClickTracking(True, True)
240+
tracking_settings.open_tracking = OpenTracking(True, "Optional tag to replace with the open image in the body of the message")
241+
tracking_settings.subscription_tracking = SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "<html><body>html to insert into the text/html portion of the message</body></html>", "Optional tag to replace with the open image in the body of the message")
242+
tracking_settings.ganalytics = Ganalytics(True, "some source", "some medium", "some term", "some content", "some campaign")
243+
self.mail.tracking_settings = tracking_settings
245244

246-
self.mail.set_reply_to(Email("test@example.com"))
245+
self.mail.reply_to = Email("test@example.com")
247246

248247
self.assertEqual(json.dumps(self.mail.get(), sort_keys=True), '{"asm": {"group_id": 99, "groups_to_display": [4, 5, 6, 7, 8]}, "attachments": [{"content": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12", "content_id": "Balance Sheet", "disposition": "attachment", "filename": "balance_001.pdf", "type": "application/pdf"}, {"content": "BwdW", "content_id": "Banner", "disposition": "inline", "filename": "banner.png", "type": "image/png"}], "batch_id": "sendgrid_batch_id", "categories": ["May", "2016"], "content": [{"type": "text/plain", "value": "some text here"}, {"type": "text/html", "value": "<html><body>some text here</body></html>"}], "custom_args": {"campaign": "welcome", "weekday": "morning"}, "from": {"email": "test@example.com", "name": "Example User"}, "headers": {"X-Test1": "test1", "X-Test3": "test2", "X-Test4": "test4"}, "ip_pool_name": "24", "mail_settings": {"bcc": {"email": "test@example.com", "enable": true}, "bypass_list_management": {"enable": true}, "footer": {"enable": true, "html": "<html><body>Footer Text</body></html>", "text": "Footer Text"}, "sandbox_mode": {"enable": true}, "spam_check": {"enable": true, "post_to_url": "https://spamcatcher.sendgrid.com", "threshold": 1}}, "personalizations": [{"bcc": [{"email": "test@example.com"}, {"email": "test@example.com"}], "cc": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}], "custom_args": {"type": "marketing", "user_id": "343"}, "headers": {"X-Mock": "true", "X-Test": "test"}, "send_at": 1443636843, "subject": "Hello World from the Personalized SendGrid Python Library", "substitutions": {"%city%": "Denver", "%name%": "Example User"}, "to": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}]}, {"bcc": [{"email": "test@example.com"}, {"email": "test@example.com"}], "cc": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}], "custom_args": {"type": "marketing", "user_id": "343"}, "headers": {"X-Mock": "true", "X-Test": "test"}, "send_at": 1443636843, "subject": "Hello World from the Personalized SendGrid Python Library", "substitutions": {"%city%": "Denver", "%name%": "Example User"}, "to": [{"email": "test@example.com", "name": "Example User"}, {"email": "test@example.com", "name": "Example User"}]}], "reply_to": {"email": "test@example.com"}, "sections": {"%section1%": "Substitution Text for Section 1", "%section2%": "Substitution Text for Section 2"}, "send_at": 1443636842, "subject": "Hello World from the SendGrid Python Library", "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932", "tracking_settings": {"click_tracking": {"enable": true, "enable_text": true}, "ganalytics": {"enable": true, "utm_campaign": "some campaign", "utm_content": "some content", "utm_medium": "some medium", "utm_source": "some source", "utm_term": "some term"}, "open_tracking": {"enable": true, "substitution_tag": "Optional tag to replace with the open image in the body of the message"}, "subscription_tracking": {"enable": true, "html": "<html><body>html to insert into the text/html portion of the message</body></html>", "substitution_tag": "Optional tag to replace with the open image in the body of the message", "text": "text to insert into the text/plain portion of the message"}}}')

0 commit comments

Comments
 (0)