forked from mirrors/bookwyrm
Celery can't render tempaltes
This commit is contained in:
parent
d8a20175b6
commit
c76ad0a312
2 changed files with 17 additions and 12 deletions
|
@ -27,34 +27,37 @@ def invite_email(invite_request):
|
||||||
""" send out an invite code """
|
""" send out an invite code """
|
||||||
data = email_data()
|
data = email_data()
|
||||||
data["invite_link"] = invite_request.invite.link
|
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):
|
def password_reset_email(reset_code):
|
||||||
""" generate a password reset email """
|
""" generate a password reset email """
|
||||||
data = email_data()
|
data = email_data()
|
||||||
data["reset_link"] = reset_code.link
|
data["reset_link"] = reset_code.link
|
||||||
data["user"] = reset_code.user.diplay_name
|
data["user"] = reset_code.user.display_name
|
||||||
send_email.delay(reset_code.user.email, "password_reset", data)
|
send_email.delay(reset_code.user.email, *format_email("password_reset", data))
|
||||||
|
|
||||||
|
def format_email(email_name, data):
|
||||||
@app.task
|
""" render the email templates """
|
||||||
def send_email(recipient, message_name, data):
|
|
||||||
""" use a task to send the email """
|
|
||||||
subject = (
|
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 = (
|
html_content = (
|
||||||
get_template("email/{}/html_content.html".format(message_name))
|
get_template("email/{}/html_content.html".format(email_name))
|
||||||
.render(data)
|
.render(data)
|
||||||
.strip()
|
.strip()
|
||||||
)
|
)
|
||||||
text_content = (
|
text_content = (
|
||||||
get_template("email/{}/text_content.html".format(message_name))
|
get_template("email/{}/text_content.html".format(email_name))
|
||||||
.render(data)
|
.render(data)
|
||||||
.strip()
|
.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 = EmailMultiAlternatives(subject, text_content, None, [recipient])
|
||||||
email.attach_alternative(html_content, "text/html")
|
email.attach_alternative(html_content, "text/html")
|
||||||
email.send()
|
email.send()
|
||||||
|
|
|
@ -5,6 +5,7 @@ from django.core.exceptions import PermissionDenied
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
from django.views import View
|
from django.views import View
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
|
@ -28,7 +29,8 @@ class PasswordResetRequest(View):
|
||||||
try:
|
try:
|
||||||
user = models.User.objects.get(email=email)
|
user = models.User.objects.get(email=email)
|
||||||
except models.User.DoesNotExist:
|
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
|
# remove any existing password reset cods for this user
|
||||||
models.PasswordReset.objects.filter(user=user).all().delete()
|
models.PasswordReset.objects.filter(user=user).all().delete()
|
||||||
|
@ -36,7 +38,7 @@ class PasswordResetRequest(View):
|
||||||
# create a new reset code
|
# create a new reset code
|
||||||
code = models.PasswordReset.objects.create(user=user)
|
code = models.PasswordReset.objects.create(user=user)
|
||||||
password_reset_email(code)
|
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)
|
return TemplateResponse(request, "password_reset_request.html", data)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue