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
|
|
|
|
2021-03-29 15:33:12 +00:00
|
|
|
from bookwyrm import models, settings
|
2020-10-02 21:55:28 +00:00
|
|
|
from bookwyrm.tasks import app
|
2021-03-21 18:43:14 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2020-10-02 21:55:28 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-03-21 18:43:14 +00:00
|
|
|
def email_data():
|
|
|
|
""" fields every email needs """
|
2021-03-21 16:13:21 +00:00
|
|
|
site = models.SiteSettings.objects.get()
|
2021-03-21 18:43:14 +00:00
|
|
|
if site.logo_small:
|
2021-03-21 19:07:58 +00:00
|
|
|
logo_path = "/images/{}".format(site.logo_small.url)
|
2021-03-21 18:43:14 +00:00
|
|
|
else:
|
2021-03-21 19:07:58 +00:00
|
|
|
logo_path = "/static/images/logo-small.png"
|
2021-03-21 18:43:14 +00:00
|
|
|
|
|
|
|
return {
|
2021-03-21 16:13:21 +00:00
|
|
|
"site_name": site.name,
|
2021-03-21 18:43:14 +00:00
|
|
|
"logo": logo_path,
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"user": None,
|
2021-03-21 16:13:21 +00:00
|
|
|
}
|
2021-03-21 18:43:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def invite_email(invite_request):
|
|
|
|
""" 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-03-08 16:49:10 +00:00
|
|
|
""" generate a password reset email """
|
2021-03-21 18:43:14 +00:00
|
|
|
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):
|
|
|
|
""" 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):
|
|
|
|
""" use a task to send the email """
|
2021-03-29 15:33:12 +00:00
|
|
|
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()
|