o
    Mf#.                     @   s
  d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ eeZe Zdd	 Zd
d Zdd Zdd Zdd Zdd ZdZdd Zdd Zeejdddde deddeejd ddd!deddeejd"ddd#deddgZdS )$zThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
    N)AllPermissions)DataManagerException)settings)
AnnotationTask)TaskSerializerBulkc                 K   s  |d }|j }|jd}tjj| |d}|std| d| }t|j	ddd}z	|
|jj W n	 ty>   Y nw g }	|D ] }
|
|j|j|j|j| d	}t||d
}|	tdi | qCtjj|	tjd}	t||	d
 | jtjj|dd ddt|	 ddS )Nrequestsource_annotation_id)projectidzSource annotation z! not found in the current projectr   T)flat)task_idcompleted_by_idresultresult_countparent_annotation_idr
   propagated_annotation)
batch_size)id__in)tasks_queryset   zCreated z annotationsresponse_codedetail )userdatagetr   objectsfilterr   firstsetvalues_listremovetaskr   KeyErrorr   r   r   add_annotation_fieldsappendbulk_creater   
BATCH_SIZEpost_process_annotations$update_tasks_counters_and_is_labeledr   len)r
   querysetkwargsr   r   r	   annotationssource_annotationtasksdb_annotationsibodyr   r   p/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/label_studio/data_manager/actions/experimental.pypropagate_annotations   s<   r6   c                 C   sF   t jj|d }ddd|rdt|j dnd d}d	|gd
gS )Nr
   numberr	   zEnter source annotation IDz [first ID: ] typenamelabel   columnCountfields)r   r   r   r    strr   )r   r
   first_annotationfieldr   r   r5   propagate_annotations_form9   s   rF   c                 K   s  |d }|j d}|j d}|j d}|  }||vr$td| || d  }tjj| d}	tj	tj
krD|	j|dj|d}	n|	jd	|igd
jd||giigd
}	d}
d}|	D ]Q}d}|jD ]<}|d	d |kr||di |g v rg }|d | D ]}||kr|| |
d7 }
d}q|| q||d |< qd|r|jdgd |d7 }q]| j  | j| j  tjj| d}	| j|	 dd|
 d| dS )Nr   old_label_namenew_label_namecontrol_tagz6Wrong old label name, it is not from labeling config: r<   r7   )result__icontains	from_name)result__containsvaluer   Fr?   Tr   )update_fieldsr   Updated z labels in r   )r   r   get_parsed_config	Exceptionlowerr   r   r   r   	DJANGO_DBDJANGO_DB_SQLITEr   r'   savesummaryresetupdate_data_columnsr1   all%update_created_annotations_and_labels)r
   r-   r.   r   rG   rH   rI   labels
label_typer/   label_countannotation_count
annotationchangedsub
new_labelsr>   r   r   r5   rename_labelsD   sT   


rc   c                 C   sr   |  }g }g }| D ]\}}||dg 7 }|| qdddd|ddddtt|dd	d
ddgdgS )Nr[   r?   selectrI   zChoose a label control tagr<   r=   r>   optionsrG   zOld label nameinputrH   zNew label namer;   r@   )rP   itemsr   r'   listr!   )r   r
   r[   	old_namescontrol_tagskeyr>   r   r   r5   rename_labels_form|   s*   

rm   c                 K   s  ddl m}m}m}m} |d }|jd}|jd}	|jd}
| }tt	td}|	|
 v s4J ||	 |
}
|	dkrFt|||
| n7tjtjkrht|d	}|D ]}|
|j|< qUtjj|d	gd
d n|j||d	||g||
| ddd | j| g dd| ddS )Nr   )FFunc	JSONFieldValuer   
value_name
value_typerM   StringNumber
Expressionrw   r     rB   r   	jsonb_set)function)r   r   rO   z tasksr   )django.db.modelsrn   ro   rp   rq   r   r   countrC   floatkeysadd_expressionr   rS   rT   ri   onlyr   r   bulk_updateupdaterV   rX   r    )r
   r-   r.   rn   ro   rp   rq   r   rr   rs   rM   sizecastr1   r$   r   r   r5   add_data_field   s4   
	r   c                 C   s   |  dd}}||kr@|| |d   d d }| d| | || dd | |d   } || |d   d d }||ks| S )N[r9   r?   r   ,;)findreplace)paramsstartendr   r   r5   process_arrays   s   ,r   zrange(2) or sample() or random(<min_int>, <max_int>) or choices(["<value1>", "<value2>", ...], [<weight1>, <weight2>, ...]) or replace("old-string", "new-string")c                 C   s  | d\}}t|}|dd d}t|dkr"|d dkr"g n|}t|D ]\}}|dddd	||< q(t| d
}|dkrlt|dksNJ dt|d }	t|	|	| }
t|
D ]\}}||| j	|< q_n|dkrt|dkszJ dt
td||}
t|
D ]\}}||| j	|< qn|dkrt|dksJ dt|d t|d }}t|D ]}t
|||| j	|< qn|dkrdt|  k rdk sJ d J dt|dkrt|d nd }t
jt|d ||d}
t|
D ]\}}||| j	|< qn>|dkr@t|dksJ dt|d t|d }}|D ]}||j	v r=|j	| |||j	|< q)ntdt tjj|d
gdd d S )N()r:   r   r?   r   r   '"r   rangez,range(start:int) should have start argument samplezsample() doesn't have argumentsrandom   z.random(min, max) should have 2 args: min & maxchoices   z[choices(values:list, weights:list) should have 1 or 2 args: values & weights (default=None))
populationweightskr   zOreplace(old_value:str, new_value:str) should have 2 args: old value & new valuez#Undefined expression, you can use: rx   ry   )splitr   r   r,   	enumerateri   r   intr   r   r   r   randintjsonloadsr   rQ   add_data_field_examplesr   r   r   )r-   r   rM   rr   commandargsr3   argr1   r   valuesvminimummaximumr   	old_value	new_valuer$   r   r   r5   r      s^    

r   c                 C   s0   ddddddddg d	d
ddddgdgS )Nr?   rg   rr   Namer;   rd   rs   Typert   re   rM   rq   r@   r   )r   r
   r   r   r5   add_data_field_form  s   

r   zAdd Or Modify Data Fieldr?   TzConfirm that you want to add a new field in tasks. After this operation you must refresh the Data Manager page fully to see the new column! You can use the following expressions: confirm)textr<   form)entry_point
permissiontitleorderexperimentaldialogzPropagate AnnotationszConfirm that you want to copy the source annotation to all selected tasks. Note: this action can be applied only for similar source objects: images with the same width and height, texts with the same length, audios with the same durations.zRename LabelszConfirm that you want to rename a label in all annotations. Also you have to change label names in the labeling config manually.) __doc__loggingr   ujsonr   core.permissionsr   data_manager.functionsr   django.confr   tasks.modelsr   r   tasks.serializersr   	getLogger__name__loggerall_permissionsr6   rF   rc   rm   r   r   r   r   r   projects_changetasks_changeactionsr   r   r   r5   <module>   sl    
'8(
	: