From 642d436ae94d6f2e1137d52802f75ddc56972789 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Fri, 13 Aug 2021 14:57:15 +0300
Subject: [PATCH] Add absolute_site_logo_url template tag
This commit adds a {% absolute_site_logo_url %} template tag.
The tag emits an absolute url pointing to either
SITE_LOGO_URL or to the fallback picture.
The tag is used in base email template, in slack message
template, and in "Add MS Teams" page.
This commit also fixes a couple instances where absolute URLs
were constructed like so:
{% site_root %}/docs/
This would result in incorrect links if Healthchecks is not
running at webserver's root. The correct way is:
{% site_root %}{% url 'hc-docs' %}
Finally, this commit removes stuff/logo.svg and
stuff/logo-full.svg. Selfhosted sites should not use the
official Healthchecks.io logos, so no point keeping them around
there.
---
hc/accounts/tests/test_login.py | 1 +
hc/api/tests/test_notify_slack.py | 2 +
hc/front/templatetags/hc_extras.py | 17 ++
hc/lib/emails.py | 6 -
stuff/logo-full.svg | 221 ----------------------
stuff/logo.svg | 209 --------------------
templates/accounts/login.html | 4 +-
templates/emails/base.html | 2 +-
templates/emails/login-body-html.html | 2 +-
templates/emails/login-body-text.html | 2 +-
templates/front/welcome.html | 4 +-
templates/integrations/add_msteams.html | 2 +-
templates/integrations/slack_message.json | 2 +-
13 files changed, 29 insertions(+), 445 deletions(-)
delete mode 100644 stuff/logo-full.svg
delete mode 100644 stuff/logo.svg
diff --git a/hc/accounts/tests/test_login.py b/hc/accounts/tests/test_login.py
index 26af718d..ea7407ca 100644
--- a/hc/accounts/tests/test_login.py
+++ b/hc/accounts/tests/test_login.py
@@ -34,6 +34,7 @@ class LoginTestCase(BaseTestCase):
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)
+ self.assertIn("http://testserver/docs/", html)
@override_settings(SITE_LOGO_URL="https://example.org/logo.svg")
def test_it_uses_custom_logo(self):
diff --git a/hc/api/tests/test_notify_slack.py b/hc/api/tests/test_notify_slack.py
index a3f611f1..fe898f88 100644
--- a/hc/api/tests/test_notify_slack.py
+++ b/hc/api/tests/test_notify_slack.py
@@ -26,6 +26,7 @@ class NotifySlackTestCase(BaseTestCase):
self.channel.save()
self.channel.checks.add(self.check)
+ @override_settings(SITE_ROOT="http://testserver")
@patch("hc.api.transports.requests.request")
def test_slack(self, mock_post):
self._setup_data("123")
@@ -43,6 +44,7 @@ class NotifySlackTestCase(BaseTestCase):
# The payload should not contain check's code
serialized = json.dumps(payload)
self.assertNotIn(str(self.check.code), serialized)
+ self.assertIn("http://testserver/static/img/logo.png", serialized)
@patch("hc.api.transports.requests.request")
def test_slack_with_complex_value(self, mock_post):
diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py
index 145f825c..9de2207e 100644
--- a/hc/front/templatetags/hc_extras.py
+++ b/hc/front/templatetags/hc_extras.py
@@ -2,6 +2,7 @@ import re
from django import template
from django.conf import settings
+from django.templatetags.static import static
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.timezone import now
@@ -31,6 +32,22 @@ def site_name():
return settings.SITE_NAME
+@register.simple_tag
+def absolute_site_logo_url():
+ """ Return absolute URL to site's logo.
+
+ Uses settings.SITE_LOGO_URL if set, uses
+ /static/img/logo.png as fallback.
+
+ """
+
+ url = settings.SITE_LOGO_URL or static("img/logo.png")
+ if url.startswith("/"):
+ url = settings.SITE_ROOT + url
+
+ return url
+
+
@register.filter
def mangle_link(s):
return mark_safe(escape(s).replace(".", "."))
diff --git a/hc/lib/emails.py b/hc/lib/emails.py
index aba08231..fcbd0da2 100644
--- a/hc/lib/emails.py
+++ b/hc/lib/emails.py
@@ -3,7 +3,6 @@ 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
@@ -34,11 +33,6 @@ class EmailThread(Thread):
def make_message(name, to, ctx, headers={}):
- 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)
html = render("emails/%s-body-html.html" % name, ctx)
diff --git a/stuff/logo-full.svg b/stuff/logo-full.svg
deleted file mode 100644
index de89b62e..00000000
--- a/stuff/logo-full.svg
+++ /dev/null
@@ -1,221 +0,0 @@
-
-
-
-
diff --git a/stuff/logo.svg b/stuff/logo.svg
deleted file mode 100644
index 578644c3..00000000
--- a/stuff/logo.svg
+++ /dev/null
@@ -1,209 +0,0 @@
-
-
-
-
diff --git a/templates/accounts/login.html b/templates/accounts/login.html
index de6720dc..a71b8f0f 100644
--- a/templates/accounts/login.html
+++ b/templates/accounts/login.html
@@ -1,12 +1,12 @@
{% extends "base.html" %}
-{% load compress static %}
+{% load compress hc_extras static %}
{% block title %}Sign In - {{ site_name }}{% endblock %}
{% block description %}{% endblock %}
{% block head %}
-
+
{% endblock %}
{% block content %}
diff --git a/templates/emails/base.html b/templates/emails/base.html
index 168460c6..7b6721c7 100644
--- a/templates/emails/base.html
+++ b/templates/emails/base.html
@@ -105,7 +105,7 @@
-
+
|
diff --git a/templates/emails/login-body-html.html b/templates/emails/login-body-html.html
index 2c71739a..14bc3b38 100644
--- a/templates/emails/login-body-html.html
+++ b/templates/emails/login-body-html.html
@@ -34,6 +34,6 @@ The {% site_name %} Team
P.S. Need help getting started? Check out our
-help documentation.
+help documentation.
Or, just reply to this email with any questions or issues you have.
{% endblock %}
diff --git a/templates/emails/login-body-text.html b/templates/emails/login-body-text.html
index f17e23e1..a6e53e39 100644
--- a/templates/emails/login-body-text.html
+++ b/templates/emails/login-body-text.html
@@ -14,6 +14,6 @@ Thanks,
The {% site_name %} Team
P.S. Need help getting started? Check out our help documentation
-at {% site_root %}/docs/
+at {% site_root %}{% url 'hc-docs' %}
Or, just reply to this email with any questions or issues you have.
{% endblock %}
diff --git a/templates/front/welcome.html b/templates/front/welcome.html
index 9f1e1b8f..0e27e362 100644
--- a/templates/front/welcome.html
+++ b/templates/front/welcome.html
@@ -1,5 +1,5 @@
{% extends "base.html" %}
-{% load compress humanize i18n static %}
+{% load compress hc_extras i18n static %}
{% block description %}
@@ -7,7 +7,7 @@
{% block head %}
-
+
{% endblock %}
{% block containers %}
diff --git a/templates/integrations/add_msteams.html b/templates/integrations/add_msteams.html
index d3b6cf65..f3274538 100644
--- a/templates/integrations/add_msteams.html
+++ b/templates/integrations/add_msteams.html
@@ -60,7 +60,7 @@
Optionally, upload an icon
- (feel free to use this one).
+ (feel free to use this one).
Click on Create.
diff --git a/templates/integrations/slack_message.json b/templates/integrations/slack_message.json
index 1490a17d..677a5de5 100644
--- a/templates/integrations/slack_message.json
+++ b/templates/integrations/slack_message.json
@@ -1,7 +1,7 @@
{% load hc_extras humanize %}
{
"username": "{% site_name %}",
- "icon_url": "{% site_root %}/static/img/logo@2x.png",
+ "icon_url": "{% absolute_site_logo_url %}",
"attachments": [{
{% if check.status == "up" %}
"color": "good",