
    	]je                         d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZ  edddd	
       G d de
             Z eddd	dd       G d de
             Zy)z
FSM Transitions for Annotation model.

This module defines declarative transitions for the Annotation entity.
Annotation transitions can update related task states via post_transition_hooks.
    )AnyDictOptional)register_state_transition)AnnotationStateChoices)ModelChangeTransitionStateModelTypeTransitionContext
annotationannotation_createdTF)triggers_on_createtriggers_on_updatec                   f    e Zd ZdZd
dee   defdZdedefdZdede	ee
f   fdZdededdfd	Zy)AnnotationCreatedTransitionz
    Transition when an annotation is created.

    This is the default transition for newly created annotations.

    Trigger: Automatically on creation only (triggers_on_create=True, triggers_on_update=False)
    Ncontextreturnc                 "    t         j                  S Nr   CREATEDselfr   s     Q/root/env/lib/python3.12/site-packages/label_studio/fsm/annotation_transitions.pyget_target_statez,AnnotationCreatedTransition.get_target_state       %---    c                      y)z/Return detailed reason for annotation creation.zAnnotation created r   s     r   
get_reasonz&AnnotationCreatedTransition.get_reason       #r   c                 x    |j                   }|j                  |j                  |j                  |j                  dS )z)Execute annotation submission transition.)task_id
project_idcompleted_by_id	lead_time)entityr"   r#   r$   r%   r   r   r   s      r   
transitionz&AnnotationCreatedTransition.transition    s;    ^^
 "))$//)99#--	
 	
r   state_recordc                 b   ddl m} ddlm} ddlm} ddlm} |j                  }|j                  }|j                  }	|j                  |      }
|
 |||j                  |j                        }
|
|j                  k7  r|j                  |d|j                  	        ||	|j                  
       y)a  
        Post-transition hook for annotation creation.

        Updates task state to COMPLETED when annotation is created.
        Then updates project state based on task completion status.
        Handles "cold start" scenarios where task may not have state record yet.
        r   )&update_project_state_after_task_change)TaskStateChoices)StateManager)get_or_initialize_stateN)userinferred_statetask_completed)r&   transition_namer/   )r/   )fsm.project_transitionsr+   fsm.state_choicesr,   fsm.state_managerr-   	fsm.utilsr.   r&   taskprojectget_current_state_valuecurrent_user	COMPLETEDexecute_transition)r   r   r)   r+   r,   r-   r.   r   r7   r8   current_task_states              r   post_transition_hookz0AnnotationCreatedTransition.post_transition_hook+   s     	S625^^
$$ *AA$G% "97//@P@Z@Z"
 !1!;!;;++4IY`g`t`t+u 	/wW=Q=QRr   r   __name__
__module____qualname____doc__r   r
   strr   r   r   r   r(   r	   r>   r   r   r   r   r      sp    .1B(C .s .$"3 $ $	
"3 	
S#X 	
 S,=  S^  S`d  Sr   r   annotation_updated)r   r   force_state_recordc                   f    e Zd ZdZd
dee   defdZdedefdZdede	ee
f   fdZdededdfd	Zy)AnnotationUpdatedTransitionz
    Transition when an annotation is updated.

    Updates keep the annotation in CREATED state but create audit trail records.

    Trigger: On update (triggers_on_create=False, triggers_on_update=True, force_state_record=True)
    Nr   r   c                 "    t         j                  S r   r   r   s     r   r   z,AnnotationUpdatedTransition.get_target_stateZ   r   r   c                      y)z-Return detailed reason for annotation update.zAnnotation updatedr   r   s     r   r   z&AnnotationUpdatedTransition.get_reason]   r    r   c                     |j                   }|j                  |j                  t        |dd      | j                  r%t        | j                  j                               dS g dS )z%Execute annotation update transition.updated_by_idN)r"   r#   rL   changed_fields)r&   r"   r#   getattrrM   listkeysr'   s      r   r(   z&AnnotationUpdatedTransition.transitiona   se    ^^
 "))$//$Z$GBFBUBUd4#6#6#;#;#=>	
 	
 \^	
 	
r   r)   c                      y)z,Post-transition hook for annotation updates.Nr   )r   r   r)   s      r   r>   z0AnnotationUpdatedTransition.post_transition_hookl   s    r   r   r?   r   r   r   rH   rH   N   sl    .1B(C .s .$"3 $ $	
"3 	
S#X 	
,= ^ `d r   rH   N)rC   typingr   r   r   fsm.registryr   r4   r   fsm.transitionsr   r	   r
   r   rH   r   r   r   <module>rU      s|    ' & 2 4 T T <)=RVkpq;S"7 ;S r;S| &5UYnr"7 r   