Skip to content

Commit

Permalink
Merge branch 'copperstick6/sendgrid'
Browse files Browse the repository at this point in the history
  • Loading branch information
anishathalye committed Dec 27, 2019
2 parents e5bff02 + 683e022 commit 370a333
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ secret_key: null
# Optional Settings #
#####################

# can also be specified as the 'USE_SENDGRID' environment variable
# defaults to 'False'
#
# this enables sending emails via sendgrid. if you enable this, you should also
# set SENDGRID_API_KEY.
use_sendgrid: null

# can also be specified as the 'SENDGRID_API_KEY' environment variable
# defaults to 'None'
sendgrid_api_key: null

# can also be specified as the 'DATABASE_URL' or 'DB_URI' environment variable
# defaults to 'postgresql://localhost/gavel'
db_uri: null
Expand Down
1 change: 1 addition & 0 deletions gavel/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ANNOTATOR_ID = 'annotator_id'
TELEMETRY_URL = 'https://telemetry.anish.io/api/v1/submit'
SENDGRID_URL = "https://api.sendgrid.com/v3/mail/send"

# Setting
# keys
Expand Down
5 changes: 4 additions & 1 deletion gavel/controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,7 @@ def email_invite_links(annotators):
body = '\n\n'.join(utils.get_paragraphs(raw_body))
emails.append((annotator.email, settings.EMAIL_SUBJECT, body))

utils.send_emails.delay(emails)
if settings.USE_SENDGRID and settings.SENDGRID_API_KEY != None:
utils.send_sendgrid_emails(emails)
else:
utils.send_emails.delay(emails)
2 changes: 2 additions & 0 deletions gavel/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ def _list(item):
EMAIL_SUBJECT = c.get('email_subject', default=constants.DEFAULT_EMAIL_SUBJECT)
EMAIL_BODY = c.get('email_body', default=constants.DEFAULT_EMAIL_BODY)
SEND_STATS = _bool(c.get('send_stats', 'SEND_STATS', default=True))
USE_SENDGRID = _bool(c.get('use_sendgrid', 'USE_SENDGRID', default=False))
SENDGRID_API_KEY = c.get('sendgrid_api_key','SENDGRID_API_KEY', default=None)
28 changes: 28 additions & 0 deletions gavel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import email
import email.mime.multipart
import email.mime.text
import json

def gen_secret(length):
return base64.b32encode(os.urandom(length))[:length].decode('utf8').lower()
Expand Down Expand Up @@ -95,6 +96,33 @@ def send_emails(emails):
if exceptions:
raise Exception('Error sending some emails: %s' % exceptions)

def send_sendgrid_emails(emails):
exceptions = []
for e in emails:
to_address, subject, body = e
response = sendgrid_send_email(to_address, subject, body)
if not (response.status_code == requests.codes.ok or response.status_code == requests.codes.accepted):
all_errors = [error_obj['message'] for error_obj in response.json()['errors']]
error_msg = '%s (error %s)' % (to_address, str(all_errors))
exceptions.append(error_msg)
if exceptions:
raise Exception('Error sending some emails: %s' % exceptions)

def sendgrid_send_email(to_address, subject, body):
payload = {
'personalizations': [{'to': [{'email': to_address}], 'subject': subject}],
'from': {'email': settings.EMAIL_FROM},
'subject': subject,
'content': [{'type': 'text/plain', 'value': body}]
}
headers = {
'authorization': 'Bearer %s' % settings.SENDGRID_API_KEY,
'content-type': 'application/json',
}

response = requests.request('POST', constants.SENDGRID_URL, data=json.dumps(payload), headers=headers)
return response

def render_markdown(content):
return Markup(markdown.markdown(content))

Expand Down

0 comments on commit 370a333

Please sign in to comment.