From bcc7009437dad33dbadaef42981d9d384f1cf772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 21 Oct 2021 11:42:13 +0300 Subject: [PATCH] Update channels.html to use Channel.last_notify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Channel.last_notify is the datetime of the most recent notification sent via the channel. Channel.last_error is the error message (blank if the delivery was successful). In the integrations list, "Last Notification" column, use these fields instead of looking up the most recent Notification object. This saves some db queries, and also fixes a subtle issue: if prunenotifications cleans up all notifications for a given channel, the "Last Notification" column would display "Never", which would not be correct – not any more. --- hc/api/management/commands/backfillchannels.py | 2 +- hc/api/models.py | 3 --- templates/front/channels.html | 12 +++++------- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/hc/api/management/commands/backfillchannels.py b/hc/api/management/commands/backfillchannels.py index a0ab0a45..bec56968 100644 --- a/hc/api/management/commands/backfillchannels.py +++ b/hc/api/management/commands/backfillchannels.py @@ -13,7 +13,7 @@ class Command(BaseCommand): q = Channel.objects.filter(id=channel.id) try: - n = channel.latest_notification() + n = Notification.objects.filter(channel=channel).latest() q.update(last_notify=n.created, last_error=n.error) total += 1 except Notification.DoesNotExist: diff --git a/hc/api/models.py b/hc/api/models.py index 3d63b8f3..dd308ceb 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -550,9 +550,6 @@ class Channel(models.Model): def icon_path(self): return "img/integrations/%s.png" % self.kind - def latest_notification(self): - return Notification.objects.filter(channel=self).latest() - @property def json(self): return json.loads(self.value) diff --git a/templates/front/channels.html b/templates/front/channels.html index 0b448f32..76355274 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -26,7 +26,6 @@ {% for ch in channels %} - {% with n=ch.latest_notification %} {{ ch.get_kind_display }} @@ -125,13 +124,13 @@ {% endif %} - {% if n %} - {% if n.error %} - - Failed, {{ n.created|naturaltime }} + {% if ch.last_notify %} + {% if ch.last_error %} + + Failed, {{ ch.last_notify|naturaltime }} {% else %} - Delivered, {{ n.created|naturaltime }} + Delivered, {{ ch.last_notify|naturaltime }} {% endif %} {% else %} Never @@ -168,7 +167,6 @@ {% endif %} - {% endwith %} {% endfor %} {% else %}