@@ -85,8 +85,8 @@ def run(self):
8585 Sends all the batch commands to the Wikidata API. This method should not fail.
8686 Sets the batch status to BLOCKED when a command fails.
8787 """
88- # Ignore when not INITIAL
89- if not self .is_initial :
88+ # Ignore when not INITIAL or RUNNING
89+ if not self .is_initial_or_running :
9090 return
9191
9292 self .start ()
@@ -146,10 +146,13 @@ def finish(self):
146146 self .save ()
147147
148148 def stop (self ):
149- logger .debug (f"[{ self } ] stop..." )
150- self .message = f"Batch stopped processing by owner at { datetime .now ()} "
151- self .status = self .STATUS_STOPPED
152- self .save ()
149+ if not self .is_done :
150+ logger .debug (f"[{ self } ] stop..." )
151+ self .message = f"Batch stopped processing by owner at { datetime .now ()} "
152+ self .status = self .STATUS_STOPPED
153+ self .save ()
154+ else :
155+ logger .debug (f"[{ self } ] user tried to stop but batch is done." )
153156
154157 def restart (self ):
155158 if self .is_stopped :
@@ -386,7 +389,7 @@ class Error(models.TextChoices):
386389 )
387390
388391 def __str__ (self ):
389- return f"Batch #{ self .batch .pk } Command #{ self .pk } "
392+ return f"Batch #{ self .batch .pk } Command #{ self .pk } ## { self . index } "
390393
391394 # -----------------
392395 # Status-changing methods
@@ -528,13 +531,21 @@ def parser_value_to_api_value(self, parser_value):
528531
529532 @property
530533 def statement_api_value (self ):
534+ self .update_quantity_units_if_needed ()
535+ return self .parser_value_to_api_value (self .json ["value" ])
536+
537+ def update_quantity_units_if_needed (self ):
538+ # TODO: maybe we can add this elsewhere,
539+ # because `statement_api_value` above is called a lot
531540 value = self .json ["value" ]
532- if value ["type" ] == "quantity" and value ["value" ]["unit" ] != "1" :
533- base = self .batch .wikibase_url ()
541+ base = self .batch .wikibase_url ()
542+ if (value ["type" ] == "quantity"
543+ and value ["value" ]["unit" ] != "1"
544+ and base not in value ["value" ]["unit" ]
545+ ):
534546 unit_id = value ["value" ]["unit" ]
535547 full_unit = f"{ base } /entity/Q{ unit_id } "
536548 value ["value" ]["unit" ] = full_unit
537- return self .parser_value_to_api_value (value )
538549
539550 def update_statement (self , st ):
540551 st .setdefault ("property" , {"id" : self .prop })
@@ -787,7 +798,7 @@ def update_combining_state(self, client: Client):
787798 commands = commands ,
788799 entity = entity ,
789800 )
790- logger .debug (f"[{ self } ] combined. final entity= { entity } " )
801+ logger .debug (f"[{ self } ] combined with next " )
791802
792803 @property
793804 def final_combining_state (self ):
0 commit comments