moviewyrm/bookwyrm/emailing.py

76 lines
2.4 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()
return {
2021-03-21 16:13:21 +00:00
"site_name": site.name,
2021-11-18 04:22:00 +00:00
"logo": site.logo_small_url,
"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-11-18 04:22:00 +00:00
def moderation_report_email(report):
"""a report was created"""
data = email_data()
data["reporter"] = report.reporter.localname or report.reporter.username
data["reportee"] = report.user.localname or report.user.username
data["report_link"] = report.remote_id
for admin in models.User.objects.filter(groups__name__in=["admin", "moderator"]):
2021-11-18 04:22:00 +00:00
data["user"] = admin.display_name
send_email.delay(admin.email, *format_email("moderation_report", data))
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-09-18 18:33:43 +00:00
subject = get_template(f"email/{email_name}/subject.html").render(data).strip()
2021-03-21 16:20:37 +00:00
html_content = (
2021-09-18 18:33:43 +00:00
get_template(f"email/{email_name}/html_content.html").render(data).strip()
2021-03-21 16:20:37 +00:00
)
text_content = (
2021-09-18 18:33:43 +00:00
get_template(f"email/{email_name}/text_content.html").render(data).strip()
2021-03-21 16:20:37 +00:00
)
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
2021-09-07 23:33:43 +00:00
@app.task(queue="high_priority")
2021-03-21 19:06:20 +00:00
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(
2022-01-06 01:35:42 +00:00
subject, text_content, settings.EMAIL_SENDER, [recipient]
)
2021-03-21 15:54:57 +00:00
email.attach_alternative(html_content, "text/html")
email.send()