From c76ad0a3122f481e7aedfb5f5759868598c73d61 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 21 Mar 2021 12:06:20 -0700 Subject: [PATCH] Celery can't render tempaltes --- bookwyrm/emailing.py | 23 +++++++++++++---------- bookwyrm/views/password.py | 6 ++++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/bookwyrm/emailing.py b/bookwyrm/emailing.py index 4025aa22..e58423e8 100644 --- a/bookwyrm/emailing.py +++ b/bookwyrm/emailing.py @@ -27,34 +27,37 @@ def invite_email(invite_request): """ send out an invite code """ data = email_data() data["invite_link"] = invite_request.invite.link - send_email.delay(invite_request.email, "invite", data) + send_email.delay(invite_request.email, *format_email("invite", data)) def password_reset_email(reset_code): """ generate a password reset email """ data = email_data() data["reset_link"] = reset_code.link - data["user"] = reset_code.user.diplay_name - send_email.delay(reset_code.user.email, "password_reset", data) + data["user"] = reset_code.user.display_name + send_email.delay(reset_code.user.email, *format_email("password_reset", data)) - -@app.task -def send_email(recipient, message_name, data): - """ use a task to send the email """ +def format_email(email_name, data): + """ render the email templates """ subject = ( - get_template("email/{}/subject.html".format(message_name)).render(data).strip() + get_template("email/{}/subject.html".format(email_name)).render(data).strip() ) html_content = ( - get_template("email/{}/html_content.html".format(message_name)) + get_template("email/{}/html_content.html".format(email_name)) .render(data) .strip() ) text_content = ( - get_template("email/{}/text_content.html".format(message_name)) + get_template("email/{}/text_content.html".format(email_name)) .render(data) .strip() ) + return (subject, html_content, text_content) + +@app.task +def send_email(recipient, subject, html_content, text_content): + """ use a task to send the email """ email = EmailMultiAlternatives(subject, text_content, None, [recipient]) email.attach_alternative(html_content, "text/html") email.send() diff --git a/bookwyrm/views/password.py b/bookwyrm/views/password.py index e853d16b..2926b9d7 100644 --- a/bookwyrm/views/password.py +++ b/bookwyrm/views/password.py @@ -5,6 +5,7 @@ from django.core.exceptions import PermissionDenied from django.shortcuts import redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator +from django.utils.translation import gettext as _ from django.views import View from bookwyrm import models @@ -28,7 +29,8 @@ class PasswordResetRequest(View): try: user = models.User.objects.get(email=email) except models.User.DoesNotExist: - return redirect("/password-reset") + data = {"error": _("No user with that email address was found.")} + return TemplateResponse(request, "password_reset_request.html", data) # remove any existing password reset cods for this user models.PasswordReset.objects.filter(user=user).all().delete() @@ -36,7 +38,7 @@ class PasswordResetRequest(View): # create a new reset code code = models.PasswordReset.objects.create(user=user) password_reset_email(code) - data = {"message": "Password reset link sent to %s" % email} + data = {"message": _("A password reset link sent to %s" % email)} return TemplateResponse(request, "password_reset_request.html", data)