moviewyrm/bookwyrm/emailing.py

75 lines
2.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" send emails """
2021-03-21 15:54:57 +00:00
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
2020-10-02 21:55:28 +00:00
from bookwyrm import models, settings
2020-10-02 21:55:28 +00:00
from bookwyrm.tasks import app
from bookwyrm.settings import DOMAIN
2020-10-02 21:55:28 +00:00
2021-03-08 16:49:10 +00:00
def email_data():
2021-04-26 16:15:42 +00:00
"""fields every email needs"""
2021-03-21 16:13:21 +00:00
site = models.SiteSettings.objects.get()
if site.logo_small:
2021-03-21 19:07:58 +00:00
logo_path = "/images/{}".format(site.logo_small.url)
else:
2021-03-21 19:07:58 +00:00
logo_path = "/static/images/logo-small.png"
return {
2021-03-21 16:13:21 +00:00
"site_name": site.name,
"logo": logo_path,
"domain": DOMAIN,
"user": None,
2021-03-21 16:13:21 +00:00
}
2021-08-06 22:38:37 +00:00
def email_confirmation_email(user):
"""newly registered users confirm email address"""
data = email_data()
data["confirmation_code"] = user.confirmation_code
data["confirmation_link"] = user.confirmation_link
2021-08-07 00:23:44 +00:00
send_email.delay(user.email, *format_email("confirm", data))
2021-08-06 22:38:37 +00:00
2021-08-06 23:24:57 +00:00
def invite_email(invite_request):
2021-04-26 16:15:42 +00:00
"""send out an invite code"""
data = email_data()
data["invite_link"] = invite_request.invite.link
2021-03-21 19:06:20 +00:00
send_email.delay(invite_request.email, *format_email("invite", data))
2021-03-21 16:13:21 +00:00
2020-10-02 21:55:28 +00:00
def password_reset_email(reset_code):
2021-04-26 16:15:42 +00:00
"""generate a password reset email"""
data = email_data()
data["reset_link"] = reset_code.link
2021-03-21 19:06:20 +00:00
data["user"] = reset_code.user.display_name
send_email.delay(reset_code.user.email, *format_email("password_reset", data))
2020-10-02 21:55:28 +00:00
2021-03-21 19:07:58 +00:00
2021-03-21 19:06:20 +00:00
def format_email(email_name, data):
2021-04-26 16:15:42 +00:00
"""render the email templates"""
2021-03-21 16:20:37 +00:00
subject = (
2021-03-21 19:06:20 +00:00
get_template("email/{}/subject.html".format(email_name)).render(data).strip()
2021-03-21 16:20:37 +00:00
)
html_content = (
2021-03-21 19:06:20 +00:00
get_template("email/{}/html_content.html".format(email_name))
2021-03-21 16:20:37 +00:00
.render(data)
.strip()
)
text_content = (
2021-03-21 19:06:20 +00:00
get_template("email/{}/text_content.html".format(email_name))
2021-03-21 16:20:37 +00:00
.render(data)
.strip()
)
2021-03-21 19:06:20 +00:00
return (subject, html_content, text_content)
2021-03-21 15:54:57 +00:00
2021-03-21 19:06:20 +00:00
@app.task
def send_email(recipient, subject, html_content, text_content):
2021-04-26 16:15:42 +00:00
"""use a task to send the email"""
email = EmailMultiAlternatives(
subject, text_content, settings.DEFAULT_FROM_EMAIL, [recipient]
)
2021-03-21 15:54:57 +00:00
email.attach_alternative(html_content, "text/html")
email.send()