66 get_failure_hook_name ,
77 get_creation_hook_name ,
88)
9- from jsonfield import JSONField
10- from django .db .models import UUIDField , Count
9+ from django .db .models import JSONField , UUIDField , Count , TextChoices
1110import datetime
1211import logging
1312import uuid
@@ -74,27 +73,19 @@ def to_process(self, queue_name):
7473
7574
7675class Job (models .Model ):
77- class STATES :
76+ class STATES ( TextChoices ) :
7877 NEW = "NEW"
7978 READY = "READY"
8079 PROCESSING = "PROCESSING"
8180 FAILED = "FAILED"
8281 COMPLETE = "COMPLETE"
8382
84- STATE_CHOICES = [
85- (STATES .NEW , "NEW" ),
86- (STATES .READY , "READY" ),
87- (STATES .PROCESSING , "PROCESSING" ),
88- (STATES .FAILED , "FAILED" ),
89- (STATES .COMPLETE , "COMPLETE" ),
90- ]
91-
9283 id = UUIDField (primary_key = True , default = uuid .uuid4 , editable = False )
9384 created = models .DateTimeField (auto_now_add = True , db_index = True )
9485 modified = models .DateTimeField (auto_now = True )
9586 name = models .CharField (max_length = 100 )
9687 state = models .CharField (
97- max_length = 20 , choices = STATE_CHOICES , default = STATES .NEW , db_index = True
88+ max_length = 20 , choices = STATES . choices , default = STATES .NEW , db_index = True
9889 )
9990 next_task = models .CharField (max_length = 100 , blank = True )
10091 workspace = JSONField (null = True )
@@ -107,9 +98,7 @@ class Meta:
10798 objects = JobManager ()
10899
109100 def save (self , * args , ** kwargs ):
110- is_new = not Job .objects .filter (pk = self .pk ).exists ()
111-
112- if is_new :
101+ if self ._state .adding :
113102 self .next_task = get_next_task_name (self .name )
114103 self .workspace = self .workspace or {}
115104
@@ -121,7 +110,7 @@ def save(self, *args, **kwargs):
121110 )
122111 return # cancel the save
123112
124- return super (Job , self ).save (* args , ** kwargs )
113+ return super ().save (* args , ** kwargs )
125114
126115 def update_next_task (self ):
127116 self .next_task = get_next_task_name (self .name , self .next_task ) or ""
0 commit comments