From 484c0befbc1ffeae03f994dbb3567cf42c197644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 13 Aug 2021 14:21:20 +0300 Subject: [PATCH] Fix email template to use SITE_LOGO_URL (with img/logo.png fallback) Fixes: #550 --- hc/accounts/tests/test_login.py | 13 +++++++++++-- hc/lib/emails.py | 7 +++++-- templates/emails/base.html | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/hc/accounts/tests/test_login.py b/hc/accounts/tests/test_login.py index b7c27a35..26af718d 100644 --- a/hc/accounts/tests/test_login.py +++ b/hc/accounts/tests/test_login.py @@ -21,6 +21,7 @@ class LoginTestCase(BaseTestCase): r = self.client.get("/accounts/login/") self.assertRedirects(r, self.checks_url) + @override_settings(SITE_ROOT="http://testserver") def test_it_sends_link(self): form = {"identity": "alice@example.org"} @@ -29,8 +30,16 @@ class LoginTestCase(BaseTestCase): # And email should have been sent self.assertEqual(len(mail.outbox), 1) - subject = "Log in to %s" % settings.SITE_NAME - self.assertEqual(mail.outbox[0].subject, subject) + message = mail.outbox[0] + self.assertEqual(message.subject, f"Log in to {settings.SITE_NAME}") + html = message.alternatives[0][0] + self.assertIn("http://testserver/static/img/logo.png", html) + + @override_settings(SITE_LOGO_URL="https://example.org/logo.svg") + def test_it_uses_custom_logo(self): + self.client.post("/accounts/login/", {"identity": "alice@example.org"}) + html = mail.outbox[0].alternatives[0][0] + self.assertIn("https://example.org/logo.svg", html) def test_it_sends_link_with_next(self): form = {"identity": "alice@example.org"} diff --git a/hc/lib/emails.py b/hc/lib/emails.py index 8b998572..aba08231 100644 --- a/hc/lib/emails.py +++ b/hc/lib/emails.py @@ -3,6 +3,7 @@ from threading import Thread import time from django.conf import settings +from django.templatetags.static import static from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string as render @@ -33,7 +34,10 @@ class EmailThread(Thread): def make_message(name, to, ctx, headers={}): - ctx["SITE_ROOT"] = settings.SITE_ROOT + ctx["site_logo_url"] = settings.SITE_LOGO_URL or static("img/logo.png") + if ctx["site_logo_url"].startswith("/"): + # If it's a relative URL, prepend SITE_ROOT + ctx["site_logo_url"] = settings.SITE_ROOT + ctx["site_logo_url"] subject = render("emails/%s-subject.html" % name, ctx).strip() body = render("emails/%s-body-text.html" % name, ctx) @@ -41,7 +45,6 @@ def make_message(name, to, ctx, headers={}): msg = EmailMultiAlternatives(subject, body, to=(to,), headers=headers) msg.attach_alternative(html, "text/html") - return msg diff --git a/templates/emails/base.html b/templates/emails/base.html index 9672af6c..168460c6 100644 --- a/templates/emails/base.html +++ b/templates/emails/base.html @@ -105,7 +105,7 @@