Browse Source

Fix grouping and sorting in the text version of the report/nag emails

Fixes: #679
pull/680/head
Pēteris Caune 6 months ago
parent
commit
65cef0b271
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
  1. 1
      CHANGELOG.md
  2. 23
      hc/api/tests/test_sendreports.py
  3. 17
      hc/front/templatetags/hc_extras.py
  4. 7
      templates/emails/report-body-text.html
  5. 21
      templates/emails/summary-text.html

1
CHANGELOG.md

@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
### Bug Fixes
- Fix the display of ignored pings with non-zero exit status
- Fix a race condition in the "Change Email" flow
- Fix grouping and sorting in the text version of the report/nag emails (#679)
## v2.2.1 - 2022-06-13

23
hc/api/tests/test_sendreports.py

@ -7,6 +7,26 @@ from hc.api.management.commands.sendreports import Command
from hc.api.models import Check
from hc.test import BaseTestCase
NAG_TEXT = """Hello,
This is a hourly reminder sent by Mychecks.
One check is currently DOWN.
Alices Project
==============
Status Name Last Ping
------ ---------------------------------------- ----------------------
DOWN Foo now
--
Cheers,
Mychecks
"""
class SendReportsTestCase(BaseTestCase):
def setUp(self):
@ -28,6 +48,7 @@ class SendReportsTestCase(BaseTestCase):
# And it needs at least one check that has been pinged.
self.check = Check(project=self.project, last_ping=now())
self.check.name = "Foo"
self.check.status = "down"
self.check.save()
@ -114,6 +135,8 @@ class SendReportsTestCase(BaseTestCase):
self.assertNotIn(str(self.check.code), email.body)
self.assertNotIn(str(self.check.code), html)
self.assertEqual(email.body, NAG_TEXT)
def test_it_obeys_next_nag_date(self):
self.profile.next_nag_date = now() + td(days=1)
self.profile.save()

17
hc/front/templatetags/hc_extras.py

@ -34,7 +34,7 @@ def site_name():
@register.simple_tag
def absolute_site_logo_url():
""" Return absolute URL to site's logo.
"""Return absolute URL to site's logo.
Uses settings.SITE_LOGO_URL if set, uses
/static/img/logo.png as fallback.
@ -111,7 +111,7 @@ def not_down_key(check):
@register.filter
def sortchecks(checks, key):
"""Sort the list of checks in-place by given key, then by status=down. """
"""Sort the list of checks in-place by given key, then by status=down."""
if key == "created":
checks.sort(key=lambda check: check.created)
@ -137,7 +137,7 @@ def num_down_title(num_down):
@register.filter
def down_title(check):
""" Prepare title tag for the Details page.
"""Prepare title tag for the Details page.
If the check is down, return "DOWN - Name - site_name".
Otherwise, return "Name - site_name".
@ -153,7 +153,7 @@ def down_title(check):
@register.filter
def break_underscore(s):
""" Add zero-width-space characters after underscores. """
"""Add zero-width-space characters after underscores."""
if len(s) > 30:
s = s.replace("_", "_\u200b")
@ -163,7 +163,7 @@ def break_underscore(s):
@register.filter
def fix_asterisks(s):
""" Prepend asterisks with "Combining Grapheme Joiner" characters. """
"""Prepend asterisks with "Combining Grapheme Joiner" characters."""
return s.replace("*", "\u034f*")
@ -220,7 +220,7 @@ FORMATTED_PING_ENDPOINT = (
@register.filter
def format_ping_endpoint(ping_url):
""" Wrap the ping endpoint in span tags for styling with CSS. """
"""Wrap the ping endpoint in span tags for styling with CSS."""
assert ping_url.startswith(settings.PING_ENDPOINT)
tail = ping_url[len(settings.PING_ENDPOINT) :]
@ -230,3 +230,8 @@ def format_ping_endpoint(ping_url):
@register.filter
def mask_key(key):
return key[:4] + "*" * len(key[4:])
@register.filter
def underline(s):
return "=" * len(str(s))

7
templates/emails/report-body-text.html

@ -1,14 +1,11 @@
{% load hc_extras %}
Hello,
{% load hc_extras %}Hello,
{% if nag %}This is a {% if nag_period == 3600 %}hourly {% endif %}{% if nag_period == 86400 %}daily {% endif %}reminder sent by {% site_name %}.
{% if num_down == 1%}One check is currently DOWN.{% else %}{{ num_down }} checks are currently DOWN.{% endif %}{% else %}This is a {{ monthly_or_weekly }} report sent by {% site_name %}.{% endif %}
{% include 'emails/summary-text.html' %}
--
Cheers,
{% site_name %}

21
templates/emails/summary-text.html

@ -1,5 +1,18 @@
{% load humanize hc_extras %}
Status | Name | Last Ping
--------+------------------------------------------+-----------------------{% for check in checks %}
{{ check.get_status|ljust:"6" }} | {{ check.name|default:'unnamed'|ljust:"40" }} | {% if check.last_ping %}{{ check.last_ping|naturaltime }}{% else %}Never{% endif %}{% endfor %}
{% load humanize hc_extras linemode %}{% linemode %}
{% regroup checks by project as groups %}
{% for group in groups %}
{% line %}{% endline %}
{% line %}{% endline %}
{% line %}{{ group.grouper|safe }}{% endline %}
{% line %}{{ group.grouper|underline }}{% endline %}
{% line %}{% endline %}
{% line %}Status Name Last Ping{% endline %}
{% line %}------ ---------------------------------------- ----------------------{% endline %}
{% for check in group.list|sortchecks:sort %}
{% with check.get_status as status %}
{% line %}{% if status == "down" %}{{ status|upper|ljust:"6" }}{% else %}{{ status|ljust:"6" }}{% endif %} {{ check.name_then_code|safe|ljust:"40" }} {% if check.last_ping %}{{ check.last_ping|naturaltime }}{% else %}Never{% endif %}{% endline %}
{% endwith %}
{% endfor %}
{% endfor %}
{% endlinemode %}
Loading…
Cancel
Save