From 69c8435a5bc61e5d455dbfefdefa66839ae34ffd Mon Sep 17 00:00:00 2001 From: adriant Date: Wed, 25 Feb 2015 16:33:00 +1300 Subject: [PATCH] basic email templating Change-Id: I6be5ac18ee9760a39b026a71106970c31f4e0ac5 --- api_v1/templates/token.txt | 15 +++++++++++++++ api_v1/views.py | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 api_v1/templates/token.txt diff --git a/api_v1/templates/token.txt b/api_v1/templates/token.txt new file mode 100644 index 0000000..298c972 --- /dev/null +++ b/api_v1/templates/token.txt @@ -0,0 +1,15 @@ +Hello, + +Your registrations with our service is almost done. + +The action you have requested is: +{% for action in actions %} +- {{ action }} +{% endfor %} + +You token to complete this action is: +{{ token }} + + +Thank you for using our service. + diff --git a/api_v1/views.py b/api_v1/views.py index 847d3ac..f455616 100644 --- a/api_v1/views.py +++ b/api_v1/views.py @@ -24,6 +24,7 @@ from django.core.mail import send_mail from smtplib import SMTPException from django.conf import settings +from django.template import loader, Context @decorator @@ -82,10 +83,12 @@ def create_token(registration): def email_token(registration, token): emails = set() + actions = [] for action in registration.actions: act = action.get_action() if act.need_token: emails.add(act.token_email()) + actions.append(unicode(act)) if len(emails) > 1: notes = { @@ -97,8 +100,12 @@ def email_token(registration, token): # TODO(adriant): raise some error? # and surround calls to this function with try/except + context = {'actions': actions, 'token': token.token} + + email_template = loader.get_template("token.txt") + try: - message = "your token is: %s" % token.token + message = email_template.render(Context(context)) send_mail( 'Your token', message, 'no-reply@example.com', [emails.pop()], fail_silently=False)