Browse Source

Fix email template to use SITE_LOGO_URL (with img/logo.png fallback)

Fixes: #550
pull/555/head
Pēteris Caune 3 years ago
parent
commit
484c0befbc
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 17 additions and 5 deletions
  1. +11
    -2
      hc/accounts/tests/test_login.py
  2. +5
    -2
      hc/lib/emails.py
  3. +1
    -1
      templates/emails/base.html

+ 11
- 2
hc/accounts/tests/test_login.py View File

@ -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": "[email protected]"}
@ -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": "[email protected]"})
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": "[email protected]"}


+ 5
- 2
hc/lib/emails.py View File

@ -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


+ 1
- 1
templates/emails/base.html View File

@ -105,7 +105,7 @@
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" class="wrapper">
<tr>
<td align="center" valign="top" style="padding: 15px 0;" class="logo">
<img alt="Logo" src="{% site_root %}/static/img/[email protected]" width="200" height="50" style="display: block; font-family: Helvetica, Arial, sans-serif; color: #ffffff; font-size: 16px;" border="0">
<img alt="{% site_name %}" src="{{ site_logo_url }}" height="50" style="display: block; font-family: Helvetica, Arial, sans-serif; color: #ffffff; font-size: 16px;" border="0">
</td>
</tr>
</table>


Loading…
Cancel
Save