From e24350e7b68759bb724cd16ed59c9e0d0bc568bc Mon Sep 17 00:00:00 2001 From: rup-narayan-rajbanshi Date: Thu, 11 Jul 2024 16:44:13 +0545 Subject: [PATCH] Change logic to save status for Surge Alerts. --- .../commands/update_surge_alert_status.py | 12 ++++++------ notifications/models.py | 18 +++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/notifications/management/commands/update_surge_alert_status.py b/notifications/management/commands/update_surge_alert_status.py index 0fe04dbcb1..aee2bc8bdb 100644 --- a/notifications/management/commands/update_surge_alert_status.py +++ b/notifications/management/commands/update_surge_alert_status.py @@ -15,9 +15,9 @@ class Command(BaseCommand): """ Updating the Surge Alert Status according: - If the alert status is marked as stood_down, then the status is Stood Down. - If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed. - Otherwise, it is displayed as Open. + If molnix_status is active, status should Open, + If molnix_status is archived, status should be Closed, + If molnix_status is unfilled, status should be Stood Down, """ help = "Update surge alert status" @@ -29,9 +29,9 @@ def handle(self, *args, **options): logger.info("Updating Surge alerts status") SurgeAlert.objects.update( status=models.Case( - models.When(is_stood_down=True, then=models.Value(SurgeAlertStatus.STOOD_DOWN)), - models.When(closes__lt=now, then=models.Value(SurgeAlertStatus.CLOSED)), - models.When(closes__gte=now, then=models.Value(SurgeAlertStatus.OPEN)), + models.When(molnix_status="active", then=models.Value(SurgeAlertStatus.OPEN)), + models.When(molnix_status="archived", then=models.Value(SurgeAlertStatus.CLOSED)), + models.When(molnix_status="unfilled", then=models.Value(SurgeAlertStatus.STOOD_DOWN)), default=models.F("status"), output_field=models.IntegerField(), ) diff --git a/notifications/models.py b/notifications/models.py index dcd82950ed..1dc42c7b69 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -74,20 +74,16 @@ class Meta: def save(self, *args, **kwargs): """ - If the alert status is marked as stood_down, then the status is Stood Down. - If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed. - Otherwise, it is displayed as Open. + A molnix_status of active should be shown as Open + A molnix_status of archived should be shown as Closed + A molnix_status of unfilled should be shown as Stood Down """ - # On save, if `created` is not set, make it the current time - if (not self.id and not self.created_at) or (self.created_at > timezone.now()): - self.created_at = timezone.now() - self.is_stood_down = self.molnix_status == "unfilled" - if self.is_stood_down: - self.status = SurgeAlertStatus.STOOD_DOWN - elif self.closes and self.closes < timezone.now(): + if self.molnix_status == 'active': + self.status = SurgeAlertStatus.OPEN + if self.molnix_status == 'archived': self.status = SurgeAlertStatus.CLOSED else: - self.status = SurgeAlertStatus.OPEN + self.status = SurgeAlertStatus.STOOD_DOWN return super(SurgeAlert, self).save(*args, **kwargs) def __str__(self):