Browse Source

Add Channel.last_notify and a command to backfill it

master
Pēteris Caune 3 years ago
parent
commit
33fb4a36ca
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 50 additions and 1 deletions
  1. +24
    -0
      hc/api/management/commands/backfillchannels.py
  2. +18
    -0
      hc/api/migrations/0081_channel_last_notify.py
  3. +4
    -1
      hc/api/models.py
  4. +4
    -0
      hc/front/tests/test_send_test_notification.py

+ 24
- 0
hc/api/management/commands/backfillchannels.py View File

@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand
from hc.api.models import Channel, Notification
class Command(BaseCommand):
help = "Backfill Channel.last_notify and Channel.last_error"
def handle(self, *args, **options):
total = 0
for channel in Channel.objects.all():
q = Channel.objects.filter(id=channel.id)
try:
n = channel.latest_notification()
q.update(last_notify=n.created, last_error=n.error)
total += 1
except Notification.DoesNotExist:
if channel.last_error:
q.update(last_error="")
total += 1
return "Done! Updated %d channels." % total

+ 18
- 0
hc/api/migrations/0081_channel_last_notify.py View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.8 on 2021-10-21 07:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0080_fill_slug'),
]
operations = [
migrations.AddField(
model_name='channel',
name='last_notify',
field=models.DateTimeField(blank=True, null=True),
),
]

+ 4
- 1
hc/api/models.py View File

@ -412,6 +412,7 @@ class Channel(models.Model):
kind = models.CharField(max_length=20, choices=CHANNEL_KINDS)
value = models.TextField(blank=True)
email_verified = models.BooleanField(default=False)
last_notify = models.DateTimeField(null=True, blank=True)
last_error = models.CharField(max_length=200, blank=True)
checks = models.ManyToManyField(Check)
@ -540,7 +541,9 @@ class Channel(models.Model):
error = self.transport.notify(check) or ""
Notification.objects.filter(id=n.id).update(error=error)
Channel.objects.filter(id=self.id).update(last_error=error)
Channel.objects.filter(id=self.id).update(
last_notify=timezone.now(), last_error=error
)
return error


+ 4
- 0
hc/front/tests/test_send_test_notification.py View File

@ -31,6 +31,10 @@ class SendTestNotificationTestCase(BaseTestCase):
self.assertTrue("X-Status-Url" in email.extra_headers)
self.assertTrue("List-Unsubscribe" in email.extra_headers)
# It should update self.channel.last_notify
self.channel.refresh_from_db()
self.assertIsNotNone(self.channel.last_notify)
# It should create a notification
n = Notification.objects.get()
self.assertEqual(n.channel, self.channel)


Loading…
Cancel
Save