@@ -49,45 +49,42 @@ class SurgeAlert(models.Model):
4949 # ID in Molnix system, if parsed from Molnix.
5050 molnix_id = models .IntegerField (blank = True , null = True )
5151
52- # Status field from Molnix - `unfilled` denotes Stood-Down
53- molnix_status = models .CharField (blank = True , null = True , max_length = 32 )
54-
55- # It depends on molnix_status. Check "save" method below.
56- is_stood_down = models .BooleanField (verbose_name = _ ("is stood down?" ), default = False )
5752 opens = models .DateTimeField (blank = True , null = True )
5853 closes = models .DateTimeField (blank = True , null = True )
5954 start = models .DateTimeField (blank = True , null = True )
6055 end = models .DateTimeField (blank = True , null = True )
6156 molnix_tags = models .ManyToManyField (MolnixTag , blank = True )
6257
63- # Set to inactive when position is no longer in Molnix
64- is_active = models .BooleanField (default = True )
65-
6658 # Don't set `auto_now_add` so we can modify it on save
6759 created_at = models .DateTimeField (verbose_name = _ ("created at" ))
68- status = models .IntegerField (choices = SurgeAlertStatus .choices , verbose_name = _ ("alert status" ), default = SurgeAlertStatus .OPEN )
60+ molnix_status = models .IntegerField (
61+ choices = SurgeAlertStatus .choices , verbose_name = _ ("alert status" ), default = SurgeAlertStatus .OPEN
62+ )
6963
7064 class Meta :
7165 ordering = ["-created_at" ]
7266 verbose_name = _ ("Surge Alert" )
7367 verbose_name_plural = _ ("Surge Alerts" )
7468
75- def save (self , * args , ** kwargs ):
69+ @staticmethod
70+ def parse_molnix_status (status_raw : str ) -> SurgeAlertStatus :
7671 """
77- If the alert status is marked as stood_down, then the status is Stood Down.
78- If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed.
79- Otherwise, it is displayed as Open.
72+ A position_status of active should be shown as Open
73+ A position_status of archived should be shown as Closed
74+ A position_status of unfilled should be shown as Stood Down
75+ If the position_status is non other than active, archived, unfilled then show Closed.
8076 """
81- # On save, if `created` is not set, make it the current time
77+ molnix_status_dict = {
78+ "active" : SurgeAlertStatus .OPEN ,
79+ "unfilled" : SurgeAlertStatus .STOOD_DOWN ,
80+ "archived" : SurgeAlertStatus .CLOSED ,
81+ }
82+
83+ return molnix_status_dict .get (status_raw .lower (), SurgeAlertStatus .CLOSED )
84+
85+ def save (self , * args , ** kwargs ):
8286 if (not self .id and not self .created_at ) or (self .created_at > timezone .now ()):
8387 self .created_at = timezone .now ()
84- self .is_stood_down = self .molnix_status == "unfilled"
85- if self .is_stood_down :
86- self .status = SurgeAlertStatus .STOOD_DOWN
87- elif self .closes and self .closes < timezone .now ():
88- self .status = SurgeAlertStatus .CLOSED
89- else :
90- self .status = SurgeAlertStatus .OPEN
9188 return super (SurgeAlert , self ).save (* args , ** kwargs )
9289
9390 def __str__ (self ):
0 commit comments