Browse Source

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.
pull/555/head
Pēteris Caune 3 years ago
parent
commit
642d436ae9
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
13 changed files with 29 additions and 445 deletions
  1. +1
    -0
      hc/accounts/tests/test_login.py
  2. +2
    -0
      hc/api/tests/test_notify_slack.py
  3. +17
    -0
      hc/front/templatetags/hc_extras.py
  4. +0
    -6
      hc/lib/emails.py
  5. +0
    -221
      stuff/logo-full.svg
  6. +0
    -209
      stuff/logo.svg
  7. +2
    -2
      templates/accounts/login.html
  8. +1
    -1
      templates/emails/base.html
  9. +1
    -1
      templates/emails/login-body-html.html
  10. +1
    -1
      templates/emails/login-body-text.html
  11. +2
    -2
      templates/front/welcome.html
  12. +1
    -1
      templates/integrations/add_msteams.html
  13. +1
    -1
      templates/integrations/slack_message.json

+ 1
- 0
hc/accounts/tests/test_login.py View File

@ -34,6 +34,7 @@ class LoginTestCase(BaseTestCase):
self.assertEqual(message.subject, f"Log in to {settings.SITE_NAME}") self.assertEqual(message.subject, f"Log in to {settings.SITE_NAME}")
html = message.alternatives[0][0] html = message.alternatives[0][0]
self.assertIn("http://testserver/static/img/logo.png", html) 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") @override_settings(SITE_LOGO_URL="https://example.org/logo.svg")
def test_it_uses_custom_logo(self): def test_it_uses_custom_logo(self):


+ 2
- 0
hc/api/tests/test_notify_slack.py View File

@ -26,6 +26,7 @@ class NotifySlackTestCase(BaseTestCase):
self.channel.save() self.channel.save()
self.channel.checks.add(self.check) self.channel.checks.add(self.check)
@override_settings(SITE_ROOT="http://testserver")
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_slack(self, mock_post): def test_slack(self, mock_post):
self._setup_data("123") self._setup_data("123")
@ -43,6 +44,7 @@ class NotifySlackTestCase(BaseTestCase):
# The payload should not contain check's code # The payload should not contain check's code
serialized = json.dumps(payload) serialized = json.dumps(payload)
self.assertNotIn(str(self.check.code), serialized) self.assertNotIn(str(self.check.code), serialized)
self.assertIn("http://testserver/static/img/logo.png", serialized)
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_slack_with_complex_value(self, mock_post): def test_slack_with_complex_value(self, mock_post):


+ 17
- 0
hc/front/templatetags/hc_extras.py View File

@ -2,6 +2,7 @@ import re
from django import template from django import template
from django.conf import settings from django.conf import settings
from django.templatetags.static import static
from django.utils.html import escape from django.utils.html import escape
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.timezone import now from django.utils.timezone import now
@ -31,6 +32,22 @@ def site_name():
return settings.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 @register.filter
def mangle_link(s): def mangle_link(s):
return mark_safe(escape(s).replace(".", "<span>.</span>")) return mark_safe(escape(s).replace(".", "<span>.</span>"))


+ 0
- 6
hc/lib/emails.py View File

@ -3,7 +3,6 @@ from threading import Thread
import time import time
from django.conf import settings from django.conf import settings
from django.templatetags.static import static
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string as render from django.template.loader import render_to_string as render
@ -34,11 +33,6 @@ class EmailThread(Thread):
def make_message(name, to, ctx, headers={}): 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() subject = render("emails/%s-subject.html" % name, ctx).strip()
body = render("emails/%s-body-text.html" % name, ctx) body = render("emails/%s-body-text.html" % name, ctx)
html = render("emails/%s-body-html.html" % name, ctx) html = render("emails/%s-body-html.html" % name, ctx)


+ 0
- 221
stuff/logo-full.svg
File diff suppressed because it is too large
View File


+ 0
- 209
stuff/logo.svg
File diff suppressed because it is too large
View File


+ 2
- 2
templates/accounts/login.html View File

@ -1,12 +1,12 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load compress static %}
{% load compress hc_extras static %}
{% block title %}Sign In - {{ site_name }}{% endblock %} {% block title %}Sign In - {{ site_name }}{% endblock %}
{% block description %}{% endblock %} {% block description %}{% endblock %}
{% block head %} {% block head %}
<link rel="canonical" href="{{ site_root }}{% url 'hc-login' %}" />
<link rel="canonical" href="{% site_root %}{% url 'hc-login' %}" />
{% endblock %} {% endblock %}
{% block content %} {% block content %}


+ 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"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" class="wrapper">
<tr> <tr>
<td align="center" valign="top" style="padding: 15px 0;" class="logo"> <td align="center" valign="top" style="padding: 15px 0;" class="logo">
<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">
<img alt="{% site_name %}" src="{% absolute_site_logo_url %}" height="50" style="display: block; font-family: Helvetica, Arial, sans-serif; color: #ffffff; font-size: 16px;" border="0">
</td> </td>
</tr> </tr>
</table> </table>


+ 1
- 1
templates/emails/login-body-html.html View File

@ -34,6 +34,6 @@ The {% site_name %} Team
<br /><br /> <br /><br />
<strong>P.S.</strong> Need help getting started? Check out our <strong>P.S.</strong> Need help getting started? Check out our
<a href="{% site_root %}/docs/">help documentation</a>.
<a href="{% site_root %}{% url 'hc-docs' %}">help documentation</a>.
Or, just reply to this email with any questions or issues you have. Or, just reply to this email with any questions or issues you have.
{% endblock %} {% endblock %}

+ 1
- 1
templates/emails/login-body-text.html View File

@ -14,6 +14,6 @@ Thanks,
The {% site_name %} Team The {% site_name %} Team
P.S. Need help getting started? Check out our help documentation 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. Or, just reply to this email with any questions or issues you have.
{% endblock %} {% endblock %}

+ 2
- 2
templates/front/welcome.html View File

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load compress humanize i18n static %}
{% load compress hc_extras i18n static %}
{% block description %} {% block description %}
<meta name="description" content="{% blocktrans %}Cron Monitoring. Monitor nightly backups, weekly reports, cron jobs and background tasks. Receive alerts when your tasks don't run on time.{% endblocktrans %}"> <meta name="description" content="{% blocktrans %}Cron Monitoring. Monitor nightly backups, weekly reports, cron jobs and background tasks. Receive alerts when your tasks don't run on time.{% endblocktrans %}">
@ -7,7 +7,7 @@
{% block head %} {% block head %}
<link rel="canonical" href="{{ site_root }}" />
<link rel="canonical" href="{% site_root %}" />
{% endblock %} {% endblock %}
{% block containers %} {% block containers %}


+ 1
- 1
templates/integrations/add_msteams.html View File

@ -60,7 +60,7 @@
</p> </p>
<p> <p>
Optionally, upload an icon Optionally, upload an icon
(feel free to use <a href="{% static 'img/[email protected]' %}">this one</a>).
(feel free to use <a href="{% absolute_site_logo_url %}">this one</a>).
</p> </p>
<p> <p>
Click on <strong>Create</strong>. Click on <strong>Create</strong>.


+ 1
- 1
templates/integrations/slack_message.json View File

@ -1,7 +1,7 @@
{% load hc_extras humanize %} {% load hc_extras humanize %}
{ {
"username": "{% site_name %}", "username": "{% site_name %}",
"icon_url": "{% site_root %}/static/img/[email protected]",
"icon_url": "{% absolute_site_logo_url %}",
"attachments": [{ "attachments": [{
{% if check.status == "up" %} {% if check.status == "up" %}
"color": "good", "color": "good",


Loading…
Cancel
Save