
    	]jR                        d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ ddlmZmZ  G d	 d
e	j                        Z G d de	j"                        Z G d de      Z G d de      Z G d de	j*                        Zy)    )settings)transaction)Project)FlexFieldsModelSerializer)serializers)ValidationError   )Label	LabelLinkc                       e Zd Zd Zd Zy)LabelListSerializerc                 X    t        t        d |D                    dkD  rt        d      |S )Nc              3   &   K   | ]	  }|d      yw)projectN ).0items     Q/root/env/lib/python3.12/site-packages/label_studio/labels_manager/serializers.py	<genexpr>z/LabelListSerializer.validate.<locals>.<genexpr>   s     5t4	?5s   r	   zACreating labels for different projects in one request not allowed)lensetr   )selfitemss     r   validatezLabelListSerializer.validate   s+    s5u556:!"eff    c                    ddl m} t        j                         5  |D cg c]  }|d   	 }}t        j
                  j                  | j                  d   j                  j                  |      j                         }|D ci c]  }|j                  | }}g }g }	g }
|D ]}  }|j                  d      }|j                  d      }|d   |v r	||d      }nt	        di |}|
j                  |       |	j                  |       |j                  t        ||              |
rt        j                   t        j"                  k(  r)i }|
D ]!  }|j%                          |||j                  <   # n7t        j
                  j'                  |
      D ci c]  }|j                  | }}g }g }t)        |	      D ]u  \  }}|j*                  |j                     }||   d   |_        ||   d   |_        |j                  |       |j                  t1        di |||   d   ||   d   d
       w t0        j
                  j'                  |d      }|	d   j,                  }|D cg c]  }|j*                   }}t0        j
                  j                  ||      j                         }|r9 || j                  d   j                  j                  |d   j,                  d|       d	d	d	       |S c c}w c c}w c c}w c c}w # 1 sw Y   S xY w)zlBulk creation objects of Label model with related LabelLink
        reusing already existing labels
        r   )emit_webhooks_for_instancetitlerequest)organization	title__inr   	from_name)r   r"   N)labelr   r"   T)ignore_conflicts)label_id__inr   LABEL_LINK_CREATEDr   )webhooks.utilsr   r   atomicr
   objectsfiltercontextuseractive_organizationallr   popappenddictr   	DJANGO_DBDJANGO_DB_SQLITEsavebulk_create	enumerateidr   r"   r   )r   validated_datar   r   titlesexisting_labelsr#   existing_labels_maplabels_datalabelslabels_creater   r"   created_labelslinksresultindex	label_idss                     r   createzLabelListSerializer.create   s    	>! <	0>?d7m?F?#mm22!\\)499MMY_ 3 ce  DS"S%5;;#5"S"S KFM& 	O((9- HH[1	=$77/W>E!MDME!((/e$""49#MN	O %%)B)BB%'N!. <

6;u{{3< GLmmF_F_`mFn%oUekk5&8%oN%o EF )& 1 u88#*5;;7E +E 29 ="-e"4["Ae$ %*'25'9)'D)4U);K)H  %%11%$1OE Qi''G/56e6I6%%,,)W,UYY[E*LL+00DDeAhFVFVXlnsu<	| y @ #T0 &p2 7o<	| sC   K;K'AK;K,C7K;K1!B=K;K61A,K;'K;;LN)__name__
__module____qualname__r   rD   r   r   r   r   r      s    
Dr   r   c                       e Zd Z ej                  dd      Z ej                  dd      Z ej                  ej                  j                               Z
 ej                         Z G d d      Zy)LabelCreateSerializerFT)required	read_only)querysetc                       e Zd ZeZeZdZy)LabelCreateSerializer.Meta__all__N)rE   rF   rG   r
   modelr   list_serializer_classfieldsr   r   r   MetarN   ^   s     3r   rS   N)rE   rF   rG   r   PrimaryKeyRelatedField
created_byr    r   r)   r.   r   	CharFieldr"   rS   r   r   r   rI   rI   X   sd    333UdSJ5;55uPTUL0k00'//:M:M:OPG%%%'I r   rI   c                   F    e Zd Z ej                  d      Z G d d      Zy)LabelLinkSerializerT)rK   c                   (    e Zd ZeZdZdddddgifiZy)LabelLinkSerializer.MetarO   r#   z*labels_manager.serializers.LabelSerializeromitr@   projectsN)rE   rF   rG   r   rP   rR   expandable_fieldsr   r   r   rS   rZ   g   s*    $'SV\_fhr^sUt&uvr   rS   N)rE   rF   rG   r   IntegerFieldannotations_countrS   r   r   r   rX   rX   d   s!    0004@w wr   rX   c                   H    e Zd Z ej                  dd      Z G d d      Zy)LabelSerializerT)manyrK   c                   $    e Zd ZeZdZddddifiZy)LabelSerializer.MetarO   r@   z.labels_manager.serializers.LabelLinkSerializerrb   TN)rE   rF   rG   r
   rP   rR   r]   r   r   r   rS   rd   p   s$    $'WZ`bfYg&hir   rS   N)rE   rF   rG   r   rT   r@   rS   r   r   r   ra   ra   m   s"    .K..DDIEj jr   ra   c                       e Zd Z ej                  ej                  j                         dd      Z ej                         Z
 ej                         Zy)LabelBulkUpdateSerializerFN)rL   rJ   default)rE   rF   rG   r   rT   r   r)   r.   r   	JSONField	old_label	new_labelr   r   r   rf   rf   v   sG    0k00'//:M:M:OZ_imnG%%%'I%%%'Ir   rf   N)django.confr   	django.dbr   projects.modelsr   rest_flex_fieldsr   rest_frameworkr   rest_framework.exceptionsr   modelsr
   r   ListSerializerr   ModelSerializerrI   rX   ra   
Serializerrf   r   r   r   <module>ru      sq      ! # 6 & 5 $J+44 JZ	K77 	w3 wj/ j( 6 6 (r   