diff --git a/hc/api/models.py b/hc/api/models.py index 1fe467d1..6e10e6ff 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -12,23 +12,6 @@ STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New")) DEFAULT_TIMEOUT = td(days=1) DEFAULT_GRACE = td(hours=1) -DURATION_CHOICES = ( - ("1 minute", td(minutes=1)), - ("2 minutes", td(minutes=2)), - ("5 minutes", td(minutes=5)), - ("10 minutes", td(minutes=10)), - ("15 minutes", td(minutes=15)), - ("30 minutes", td(minutes=30)), - ("1 hour", td(hours=1)), - ("3 hours", td(hours=3)), - ("6 hours", td(hours=6)), - ("12 hours", td(hours=12)), - ("1 day", td(days=1)), - ("2 days", td(days=2)), - ("3 days", td(days=3)), - ("1 week", td(weeks=1)) -) - class Check(models.Model): name = models.CharField(max_length=100, blank=True) @@ -46,7 +29,6 @@ class Check(models.Model): def send_alert(self): ctx = { - "timeout_choices": DURATION_CHOICES, "check": self, "checks": self.user.check_set.order_by("created"), "now": timezone.now() diff --git a/hc/front/templatetags/__init__.py b/hc/front/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py new file mode 100644 index 00000000..ee7fc13f --- /dev/null +++ b/hc/front/templatetags/hc_extras.py @@ -0,0 +1,26 @@ +from django import template + +register = template.Library() + + +@register.filter +def hc_duration(td): + total = int(td.total_seconds() / 60) + total, m = divmod(total, 60) + total, h = divmod(total, 24) + w, d = divmod(total, 7) + + result = "" + if w: + result += "1 week " if w == 1 else "%d weeks " % w + + if d: + result += "1 day " if d == 1 else "%d days " % d + + if h: + result += "1 hour " if h == 1 else "%d hours " % h + + if m: + result += "1 minute " if m == 1 else "%d minutes " % m + + return result diff --git a/hc/front/views.py b/hc/front/views.py index ba86cc66..db0a7f8b 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -5,7 +5,7 @@ from django.http import HttpResponseForbidden from django.shortcuts import redirect, render from django.utils import timezone -from hc.api.models import Check, DURATION_CHOICES +from hc.api.models import Check from hc.front.forms import TimeoutForm @@ -43,8 +43,7 @@ def _my_checks(request): ctx = { "checks": checks, - "now": timezone.now(), - "duration_choices": DURATION_CHOICES + "now": timezone.now() } return render(request, "front/my_checks.html", ctx) @@ -121,11 +120,9 @@ def email_preview(request, code): if check.user != request.user: return HttpResponseForbidden() - from hc.api.models import TIMEOUT_CHOICES ctx = { "check": check, "checks": check.user.check_set.all(), - "timeout_choices": TIMEOUT_CHOICES, "now": timezone.now() } diff --git a/templates/emails/alert/body.html b/templates/emails/alert/body.html index ade3aa80..2821fcc5 100644 --- a/templates/emails/alert/body.html +++ b/templates/emails/alert/body.html @@ -1,4 +1,4 @@ -{% load humanize %} +{% load humanize hc_extras %}