Browse Source

Natural sort for check names, fixes #136.

Apply the user's chosen sort order in emails also.
pull/140/head
Pēteris Caune 7 years ago
parent
commit
882933668a
  1. 7
      hc/accounts/models.py
  2. 9
      hc/api/transports.py
  3. 27
      hc/front/templatetags/hc_extras.py
  4. 4
      hc/front/views.py
  5. 8
      static/css/my_checks_desktop.css
  6. 2
      templates/emails/alert-body-html.html
  7. 2
      templates/emails/summary-html.html
  8. 4
      templates/front/my_checks_desktop.html
  9. 2
      templates/front/my_checks_mobile.html

7
hc/accounts/models.py

@ -141,12 +141,13 @@ class Profile(models.Model):
path = reverse("hc-unsubscribe-reports", args=[self.user.username])
unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
# Sort checks by team name, then by email, and then by creation date:
checks = checks.order_by(
"user__profile__team_name", "user__email", "created")
# Sort checks by owner. Need this because will group by owner in
# template.
checks = checks.order_by("user_id")
ctx = {
"checks": checks,
"sort": self.sort,
"now": timezone.now(),
"unsub_link": unsub_link,
"notifications_url": self.notifications_url,

9
hc/api/transports.py

@ -52,9 +52,18 @@ class Email(Transport):
headers = {"X-Bounce-Url": bounce_url}
try:
# Look up the sorting preference for this email address
p = Profile.objects.get(user__email=self.channel.value)
sort = p.sort
except Profile.DoesNotExist:
# Default sort order is by check's creation time
sort = "created"
ctx = {
"check": check,
"checks": self.checks(),
"sort": sort,
"now": timezone.now(),
"unsub_link": self.channel.get_unsub_link()
}

27
hc/front/templatetags/hc_extras.py

@ -1,3 +1,5 @@
import re
from django import template
from django.conf import settings
from django.utils.html import escape
@ -31,3 +33,28 @@ def mangle_link(s):
@register.simple_tag
def site_root():
return settings.SITE_ROOT
def naturalize_int_match(match):
return '%08d' % (int(match.group(0)),)
def natural_name_key(check):
s = check.name.lower().strip()
return re.sub(r'\d+', naturalize_int_match, s)
def last_ping_key(check):
return check.last_ping.isoformat() if check.last_ping else "9999"
@register.filter
def sortchecks(checks, key):
if key == "created":
checks.sort(key=lambda check: check.created)
elif key.endswith("name"):
checks.sort(key=natural_name_key, reverse=key.startswith("-"))
elif key.endswith("last_ping"):
checks.sort(key=last_ping_key, reverse=key.startswith("-"))
return checks

4
hc/front/views.py

@ -40,9 +40,7 @@ def my_checks(request):
request.profile.sort = request.GET["sort"]
request.profile.save()
q = Check.objects.filter(user=request.team.user)
q = q.order_by(request.profile.sort)
checks = list(q)
checks = list(Check.objects.filter(user=request.team.user))
tags, down_tags, grace_tags = set(), set(), set()
for check in checks:

8
static/css/my_checks_desktop.css

@ -52,7 +52,8 @@ table.table tr > th.th-name {
cursor: pointer;
}
#checks-table > tbody > tr > th.th-period, #checks-table > tbody > tr > th.th-last-ping {
#checks-table > tbody > tr > th.th-period,
#checks-table > tbody > tr > th.th-last-ping {
padding-left: 15px;
}
@ -80,6 +81,11 @@ table.table tr > th.th-name {
padding: 6px;
}
#checks-table .last-ping-never {
padding: 7px;
}
#checks-table tr:hover .last-ping {
border: 1px dotted #AAA;
cursor: pointer;

2
templates/emails/alert-body-html.html

@ -10,7 +10,7 @@ The check <strong>{{ check.name_then_code }}</strong>
has gone <strong>{{ check.status|upper }}</strong>.
<br /><br />
Here is a summary of all your checks:
Here is a summary of your checks:
<br />
{% include "emails/summary-html.html" %}

2
templates/emails/summary-html.html

@ -8,7 +8,7 @@
</td>
<td class="mobile-hide" style="padding: 32px 8px 8px 8px; margin: 0; font-size: 12px; color: #9BA2AB; font-family: Helvetica, Arial, sans-serif;">Last Ping</td>
</tr>
{% for check in group.list %}
{% for check in group.list|sortchecks:sort %}
<tr>
<td style="border-top: 1px solid #EDEFF2; padding: 16px 8px;">
<table cellpadding="0" cellspacing="0">

4
templates/front/my_checks_desktop.html

@ -27,7 +27,7 @@
</th>
<th></th>
</tr>
{% for check in checks %}
{% for check in checks|sortchecks:sort %}
<tr class="checks-row">
<td class="indicator-cell">
{% if check.get_status == "new" %}
@ -94,7 +94,7 @@
{% endif %}
</div>
{% else %}
Never
<div class="last-ping-never">Never</div>
{% endif %}
</td>
<td>

2
templates/front/my_checks_mobile.html

@ -1,7 +1,7 @@
{% load hc_extras humanize %}
<ul id="checks-list" class="visible-xs">
{% for check in checks %}
{% for check in checks|sortchecks:sort %}
<li>
<h2>
<span class="{% if not check.name %}unnamed{% endif %}">

Loading…
Cancel
Save