
    	]j]                     &   d Z ddlZddlZddlmZ ddlm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 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mZmZmZ ddl m!Z! ddl"m#Z#  G d dejH                        Z% G d dejH                        Z& G d dejH                        Z' G d dejH                        Z( eddddddd       G d dejR                               Z* edd d!d"ddd       G d# d$ejR                               Z+ edd%d&d'       G d( d)ejX                               Z- G d* d+e      Z. edd,d-dd.dd/dd0dd1d2d3d4d5d2d3d6d5d7d8d       G d9 d:ejR                               Z/ edd;d<dd=dd>dd?dd1d@dAdd2dBdddCdddDdd"dEdd"dFdd2d3d4d5d2d3d6d5dG	d8d       G dH dIejR                               Z0 G dJ dKe      Z1 G dL dMejd                        Z3 G dN dOejd                        Z4 G dP dQejd                        Z5y)RzThis 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)CurrentContext)flag_set)FilterFilterGroupView)settings)transaction)extend_schema_field)FSMStateField)Project)serializers)Task)AnnotationDraftSerializerAnnotationSerializerAnnotationStubSerializerPredictionSerializerTaskSerializer)User)round_floatsc                   ,    e Zd Z G d d      Zd Zd Zy)ChildFilterSerializerc                       e Zd ZeZdZy)ChildFilterSerializer.Meta__all__N__name__
__module____qualname__r   modelfields     O/root/env/lib/python3.12/site-packages/label_studio/data_manager/serializers.pyMetar          r"   r$   c                 l    | j                   }|j                  || j                        }|j                  S )N)instancecontext)parent	__class__r(   data)selfvaluer)   
serializers       r#   to_representationz'ChildFilterSerializer.to_representation!   s.    %%udll%K
r"   c                     | j                   j                  } ||| j                        }|j                  d       |j                  S )a  Allow ChildFilterSerializer to be writable.

        We instantiate the *parent* serializer class (which in this case is
        ``FilterSerializer``) to validate the nested payload. The validated
        data produced by that serializer is returned so that the enclosing
        serializer (``FilterSerializer``) can include it in its own
        ``validated_data`` structure.
        )r+   r(   T)raise_exception)r)   r*   r(   is_validvalidated_data)r,   r+   
parent_clsr.   s       r#   to_internal_valuez'ChildFilterSerializer.to_internal_value&   s@     [[**
T4<<@
D1(((r"   N)r   r   r   r$   r/   r5   r!   r"   r#   r   r      s     
)r"   r   c                   B    e Zd Z ed      Z G d d      ZdedefdZy)	FilterSerializerFrequiredc                       e Zd ZeZdZy)FilterSerializer.Metar   Nr   r!   r"   r#   r$   r;   9   r%   r"   r$   columnreturnc                 N   |}d}d}|j                  |      st        j                  d| d| d      |t        |      d }|j                  |      r|t        |      d }|j                  d      r|S |t        j
                  v r|S d|v rt        j                  d	| d
      |S )a  
        Ensure that the passed filter expression starts with 'filter:tasks:' and contains
        no foreign key traversals. This means either the filter expression contains no '__'
        substrings, or that it's the task.data json field that's accessed.

        Users depending on foreign key traversals in views can allowlist them via the
        DATA_MANAGER_FILTER_ALLOWLIST setting in the env.

        Edit with care. The validations below are critical for security.
        zfilter:tasks:-zFilter "z" should start with ""Nzdata.__zU"__" is not generally allowed in filters. Consider asking your administrator to add "zb" to DATA_MANAGER_FILTER_ALLOWLIST, but note that some filter expressions may pose a security risk)
startswithr   ValidationErrorlenr   DATA_MANAGER_FILTER_ALLOWLIST)r,   r<   column_copyrequired_prefixoptional_prefixs        r#   validate_columnz FilterSerializer.validate_column=   s      *%%o6--@UVeUffg.hii!#o"6"89!!/2%c/&:&<=K!!'*M (@@@M ;--ghsgt us s 
 r"   N)r   r   r   r   child_filterr$   strrI   r!   r"   r#   r7   r7   6   s)    (%8L )c )c )r"   r7   c                   8    e Zd Z ed      Zd Z G d d      Zy)FilterGroupSerializerT)manyc                     d }|j                   j                  d      j                  d      j                  d      }|j                  |D cg c]
  } ||       c}dS c c}w )Nc                    | j                   | j                  | j                  | j                  d}| j                  j                         }|r9|d   }|j                   |j                  |j                  |j                  d}||d<   |S )z)Build hierarchical filter representation.)filteroperatortyper-   r   rJ   )r<   rR   rS   r-   childrenall)
filter_objitemchild_filterschild
child_items        r#   _build_filter_treezCFilterGroupSerializer.to_representation.<locals>._build_filter_treem   s     %++&//"#))	D '//335M%a(#ll %!JJ"[[	
 (2^$Kr"   T)parent__isnullrT   index)conjunctionitems)filtersrQ   prefetch_relatedorder_byr^   )r,   r'   r[   rootsfs        r#   r/   z'FilterGroupSerializer.to_representationl   sa    	0   ''t'<MMjYbbcjk'33^c>dYZ?QRS?T>dee>ds   A!c                       e Zd ZeZdZy)FilterGroupSerializer.Metar   N)r   r   r   r   r   r    r!   r"   r#   r$   rf      s    r"   r$   N)r   r   r   r7   r`   r/   r$   r!   r"   r#   rM   rM   i   s    D)Gf: r"   rM   c                   j     e Zd Z ed      Z G d d      Z fdZ fdZed        Z	d Z
d	 Z xZS )
ViewSerializerFr8   c                       e Zd ZeZdZy)ViewSerializer.Metar   N)r   r   r   r   r   r    r!   r"   r#   r$   rj      s    r"   r$   c                 8   |j                  di       }|j                  di       }|j                  d      }d|vr:|r8|g d|d<   d|v r,fd|d   D ]  }|d   d   j                   |             ! |j                  di       }||d<   t        |   |      S )	a  
        map old filters structure to models
        "filters": {  ===> FilterGroup model
            "conjunction": "or",
            "items":[  ===> "filters" in FilterGroup
                 {  ==> Filter model
                   "filter":"filter:tasks:data.image", ==> column
                    "operator":"contains",
                    "type":"Image",
                    "value": <string: "XXX" | int: 123 | dict | list>
                 },
                  {
                    "filter":"filter:tasks:data.image",
                    "operator":"equal",
                    "type":"Image",
                    "value": <string: "XXX" | int: 123 | dict | list>
                 }
              ]
           }
        }
        r+   r`   r^   filter_group)r^   r`   r_   c                     | j                  dd      | j                  dd      | j                  dd      | j                  di       d}| j                  d      x}r |      |d<   |S )zAConvert a single filter JSON object into internal representation.rQ    rR   rS   r-   )r<   rR   rS   r-   rJ   )get)
src_filterfilter_payloadrJ   _convert_filters      r#   rr   z9ViewSerializer.to_internal_value.<locals>._convert_filter   so     #-..2">$.NN:r$B *vr :!+!<	&N (2~~n'EE|E9H9V~6))r"   ordering)ro   popappendsuperr5   )	r,   r+   _datar`   r^   rd   rs   rr   r*   s	          @r#   r5   z ViewSerializer.to_internal_value   s    , $))Ir*kk-0%+3>2#ND '!*  !) OA(3::?1;MNO 99Z,#Zw(..r"   c                     t         |   |      }|j                  di       }|r||d   d<   |j                  di       }|r||d   d<   |j                  di       }|r||d   d<   |S )Nrl   r+   r`   selected_itemsselectedItemsrs   )rv   r/   rt   )r,   r'   resultr`   ry   rs   r*   s         r#   r/   z ViewSerializer.to_representation   s}    *84 **^R0(/F6N9%$4b9.<F6N?+::j"-)1F6N:&r"   c                 P     d fd	t        |      D ]  \  }} ||        y)uR  Create Filter objects inside the provided ``filter_group``.

        * For **root** filters (``parent`` is ``None``) we enumerate the
          ``index`` so that the UI can preserve left-to-right order.
        * For **child** filters we leave ``index`` as ``None`` – they are not
          shown in the top-level ordering bar.
        Nc                     | j                  dd       }|| j                  dd        ||| d<   t        j                  j                  dd|i| }j                  j                  |       |r ||       y y )NrJ   r)   r]   )r)   r!   )rt   r   objectscreater`   add)r+   r)   r]   rJ   obj_create_recursiverl   s        r#   r   z9ViewSerializer._create_filters.<locals>._create_recursive   s}    88ND9L
 !4( ~ %W ..''>v>>C  $$S)!,s; r"   )r]   )NN)	enumerate)rl   filters_datar]   r+   r   s   `   @r#   _create_filterszViewSerializer._create_filters   s,    	<* %\2 	1KE4d%0	1r"   c                    t        j                         5  |j                  dd       }|r|j                  dg       }t        j                  j
                  di |}| j                  ||       |j                  |d<   t        j                  j                  |d         j                         |d<    | j                  j                  j                  j
                  di |}|cd d d        S # 1 sw Y   y xY w)	Nrl   r`   rl   r   filter_group_idproject)r   orderr!   )r	   atomicrt   r   r~   r   r   idr   rQ   countr$   r   )r,   r3   filter_group_datar   rl   views         r#   r   zViewSerializer.create   s    ! 	 . 2 2>4 H 044YC*2299N<MN$$,\$Z4@OO01*.,,*=*=nU^F_*=*`*f*f*hw'1499??**11CNCD	 	 	s   CC,,C5c                    t        j                         5  |j                  dd       }|r|j                  dg       }|j                  }|9t	        j
                  j                  di |}||_        |j                  dg       |j                  d      }|r&|j                  |k7  r||_	        |j                          |j                  j                          | j                  ||       |j                  dd       }|r&||j                  k7  r||_        |j                          |d   |j                  k7  r|d   |_        |j                          |cd d d        S # 1 sw Y   y xY w)	Nrl   r`   )update_fieldsr^   r   rs   r+   r!   )r	   r   rt   rl   r   r~   r   savero   r^   r`   clearr   rs   r+   )r,   r'   r3   r   r   rl   r^   rs   s           r#   updatezViewSerializer.update  sD   ! 	 . 2 2>4 H 044YC'44'#.#6#6#=#=#R@Q#RL,8H)MM0@MA/33MB<#;#;{#J/:L, %%'$$**,$$,\$Z%))*d;HH(9(99$,!f%6 .v 69	 	 	s   D;EE#)r   r   r   rM   rl   r$   r5   r/   staticmethodr   r   r   __classcell__r*   s   @r#   rh   rh      sA    (%8L 5/n" 1 1B r"   rh   arrayzUser IDszUser IDs who updated this taskobject)rS   title)rS   r   descriptionr_   c                       e Zd Zy)UpdatedByDMFieldSerializerNr   r   r   r!   r"   r#   r   r   /       	r"   r   zAnnotators IDsz&Annotators IDs who annotated this taskintegerc                       e Zd Zy)AnnotatorsDMFieldSerializerNr   r!   r"   r#   r   r   <  r   r"   r   zUser detailsz+User details who completed this annotation.)rS   r   r   c                       e Zd Zy)(CompletedByDMSerializerWithGenericSchemaNr   r!   r"   r#   r   r   I  s     	r"   r   c                   P    e Zd Z edej
                  j                               Zy)AnnotationsDMFieldSerializerF)r9   querysetN)r   r   r   r   r   r~   rU   completed_byr!   r"   r#   r   r   U  s    ;UUYUaUaUeUeUghLr"   r   zAnnotation draftszDrafts for this taskzDraft objectzDraft resultzDraft result item)rS   r   r_   stringz	date-timezCreation time)rS   formatr   zLast update time)r{   
created_at
updated_at)rS   r   
propertiesc                       e Zd Zy) AnnotationDraftDMFieldSerializerNr   r!   r"   r#   r   r   Y  s    @ 	r"   r   PredictionszPredictions for this taskzPrediction objectzPrediction resultzPrediction result itemnumberzPrediction scorezModel versionzML Backend instancezModel Run instancez!Task ID related to the predictionz$Project ID related to the prediction)	r{   scoremodel_versionr   	model_runtaskr   r   r   c                       e Zd Zy)PredictionsDMFieldSerializerNr   r!   r"   r#   r   r   |  s    p 	r"   r   c                   V    e Zd ZdZ edd      Z ej                  dd      Z e	dd      Z
 edd      Z ej                  d      Z ej                  d      Z ej                  d      Z ej                  d      Z ej$                  d      Z ej                  d      Z ej                  d      Z ej,                  d      Z ej                  d      Z ej                  d      Z ej                  d      Z ej                  d      Z ej,                  d      Z ej:                  d      Z edd      Z  e!d      Z"dZ# G d d	      Z$ fd
Z%ddZ&d Z'd Z(d Z)d Z*e+d        Z,e+d        Z-e+d        Z.e+d        Z/d Z0d Z1d Z2d Z3d Z4 xZ5S )DataManagerTaskSerializerz4Data Manager Task Serializer with FSM state support.FT)r9   	read_onlyr8   )r   i  c                   (    e Zd ZeZdZdZdeddifiZy)DataManagerTaskSerializer.Metadata_manager_task_serializer)precomputed_agreement
allow_skipannotationsrN   TN)	r   r   r   r   r   ref_nameexcluder   expandable_fieldsr!   r"   r#   r$   r     s&    19*-AFD>,RSr"   r$   c                 b   t         t        |   |      }| j                  j	                  d      s|j                  dd       | j                  j	                  d      s|j                  dd       t        j                         }t        d|      rt        d|      s|j                  dd       |S )z=Dynamically manage including of some fields in the API resultr   Npredictions*fflag_feat_fit_568_finite_state_managementuser#fflag_feat_fit_710_fsm_state_fieldsstate)	rv   r   r/   r(   ro   rt   r   get_userr   )r,   r   retr   r*   s       r#   r/   z+DataManagerTaskSerializer.to_representation  s    -tFsK||.GGM4(||.GGM4(&&(AM>TJGGGT"
r"   c                 8   t        ||      rt        ||      yt        ||      }t        |t              r9|}|rt	        t        |j                  d                  }dj                  |      }nit        |t              rt        |      }nM|D cg c]  }||	 }}|rt	        t        |            }t        |      }t        j                  |d      dd }|d | j                   j                  dd      j                  d	d
      j                  dd      S c c}w )Nrn   ,F)ensure_ascii   z,"z, "z],[z] [r@   )hasattrgetattr
isinstancerK   listsetsplitjoinintr   jsondumpsCHAR_LIMITSreplace)r,   r   fielduniquer{   outputrs          r#   _pretty_resultsz)DataManagerTaskSerializer._pretty_results  s    tU#wtU';'Cu%fc"Fc&,,s"345&)$[F!'9A1=a9F9c&k*!&)FZZU;AbAF((()11$>FFueT\\]`bdee :s   DDc                 &    | j                  |d      S )Nannotations_resultsr   r,   r   s     r#   get_annotations_resultsz1DataManagerTaskSerializer.get_annotations_results      ##D*?@@r"   c                 &    | j                  |d      S )Npredictions_resultsr   r   s     r#   get_predictions_resultsz1DataManagerTaskSerializer.get_predictions_results  r   r"   c                 H    t        |j                  dg d      j                  S )NT)rN   defaultr   )r   r   r+   r   s     r#   get_predictionsz)DataManagerTaskSerializer.get_predictions  s     #D$4$44W[\aaar"   c                 6   | j                   j                  d      sg S |j                  j                         }| j                   j                  d      r%t	        |dd| j                   dg      j
                  S t        |dd| j                         j
                  S )zReturn annotations for the task.

        If annotations_stub=True is in context (via feature flag
        fflag_fix_all_fit_720_lazy_load_annotations), returns lightweight
        annotation stubs without result data for improved performance.
        r   annotations_stubTr   )rN   r   r(   expand)rN   r   r(   )r(   ro   r   rU   r   r+   r   )r,   r   r   s      r#   get_annotationsz)DataManagerTaskSerializer.get_annotations  s     ||.I&&**, <<./+&' d ,LL	

 $	r"   c                     t        | d      r9| j                  }|r)t        j                  j	                  | j                        S d S y )Nfile_upload_field)r   r   ospathbasename)r   file_uploads     r#   get_file_uploadz)DataManagerTaskSerializer.get_file_upload$  s>    4,-00K?J277##D$:$:;TPTTr"   c                 "    | j                         S N)get_storage_filename)r   s    r#   r   z.DataManagerTaskSerializer.get_storage_filename+  s    ((**r"   c                 <    | j                   rd| j                   igS g S )Nuser_id)updated_by_id)r   s    r#   get_updated_byz(DataManagerTaskSerializer.get_updated_by/  s#    363D3DC--./L"Lr"   c                 0   t        | d      sg S | j                  }|sg S t        |t              r'|j	                  d      D cg c]  }t        |       }}t        t        |            }|D cg c]  }||	 }}t        | d      r|r|S g S c c}w c c}w )N
annotatorsr   )r   r  r   rK   r   r   r   r   )r   r  vas       r#   get_annotatorsz(DataManagerTaskSerializer.get_annotators3  s    sL)I^^
Ij#&*4*:*:3*?@Q#a&@J@#j/*
!+=Aq}a=
=$S,7JzNBN	 A >s   B.B6Bc                 *    | j                  |dd      S )Nannotations_idsTr   r   r   s     r#   get_annotations_idsz-DataManagerTaskSerializer.get_annotations_idsB  s    ##D*;D#IIr"   c                 *    | j                  |dd      S )Npredictions_model_versionsTr  r   r   s     r#   get_predictions_model_versionsz8DataManagerTaskSerializer.get_predictions_model_versionsE  s    ##D*Ft#TTr"   c                     t         S r   )r   )r,   s    r#   get_drafts_serializerz/DataManagerTaskSerializer.get_drafts_serializerH  s    ((r"   c                 &    |j                  |      S )zGet all user's draftr   )rQ   )r,   r   draftss      r#   get_drafts_querysetz-DataManagerTaskSerializer.get_drafts_querysetK  s    }}$}''r"   c                 z   t        |t              r| j                  j                  d      sg S |j                  }d| j                  v rDt        | j                  d   d      r+| j                  d   j                  }| j                  ||      }| j                         } ||ddd| j                        j                  S )z'Return drafts only for the current userr  requestr   T)rN   r   r   r(   )
r   r   r(   ro   r  r   r   r  r  r+   )r,   r   r  r   serializer_classs        r#   
get_draftsz$DataManagerTaskSerializer.get_draftsO  s     $%T\\-=-=h-GI$i1H&)Q<<	*//D--dF;F557TT4Y]YeYefkkkr"   )F)6r   r   r   __doc__r   r   r   SerializerMethodFieldr   r   r  r   r  IntegerFieldinner_idcancelled_annotationstotal_annotationstotal_predictionsDateTimeFieldcompleted_atr   r   
FloatFieldpredictions_scorer   storage_filenamer  r  avg_lead_timeBooleanFielddraft_existsr   
updated_byr   r   r   r$   r/   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r  r   r   s   @r#   r   r     s   >.NK3+33UdSK-uMF,etLJ'{''7H4K44eD000%@000%@,;,,e<L;+;;UK;+;;UK...>3+33UCK8{88%H7k77GO!B!B!BE!R*K**E:M+;++U;L+UdKJD)EKT T f,AAb:   + + M M O OJU)(lr"   r   c                       e Zd Z ej                         Z ej                   ej                         d      Z ej                   ej                         d      Z	d Z
y)SelectedItemsSerializerF)rY   r9   c                    |d   du r&|j                  d      rt        j                  d      |d   du r&|j                  d      rt        j                  d      | j                  j                  d      }| j                  j                  d	      }|rJ|rH|j                  d
v r:|j
                  j                  d      }|r||d   k7  rt        j                  d      |S )NrU   Tincludedz#included not allowed with all==trueFexcludedz$excluded not allowed with all==falser   r  )PATCHDELETEz1changing all value possible only with POST method)ro   r   rC   r(   methodry   )r,   r+   r   r  	all_values        r#   validatez SelectedItemsSerializer.validatec  s    ;$488J#7--.STT;%DHHZ$8--.TUU||',,""9-G2E E++//6IY$u+5!112effr"   N)r   r   r   r   r#  rU   	ListFieldr  r)  r*  r/  r!   r"   r#   r'  r'  ^  sY    
"+
"
"
$C${$$+C;+C+C+EPUVH${$$+C;+C+C+EPUVHr"   r'  c                   b    e Zd Z ej                  ej                  j                               Zy)ViewResetSerializer)r   N)	r   r   r   r   PrimaryKeyRelatedFieldr   r~   rU   r   r!   r"   r#   r2  r2  s  s!    0k00'//:M:M:OPGr"   r2  c                   v    e Zd Z ej                         Z ej                   ej                         dd      Zy)ViewOrderSerializerFz(A list of view IDs in the desired order.)rY   allow_empty	help_textN)r   r   r   r   r  r   r0  idsr!   r"   r#   r5  r5  w  s7    &k&&(G
+

&k&&(eGqCr"   r5  )6r  r   ujsonr   core.current_requestr   core.feature_flagsr   data_manager.modelsr   r   r   django.confr   	django.dbr	   drf_spectacular.utilsr
   fsm.serializer_fieldsr   projects.modelsr   rest_frameworkr   tasks.modelsr   tasks.serializersr   r   r   r   r   users.modelsr   label_studio.core.utils.commonr   ModelSerializerr   r7   rM   rh   r  r   r   r3  r   r   r   r   r   
Serializerr'  r2  r5  r!   r"   r#   <module>rI     s   l 	  / ' 9 9   ! 5 / # &    7)K77 )40{22 0f"K77 "J^[00 ^B 7"Z8		!B!B 		
 !?#j9		+"C"C 		
 D	{/Q/Q 		
i#7 i $-# $+ (!4 %), %)/
	>	{'H'H 	?>	 2( $0 (!9 %/
 %,"
 %2
 %1
 &@
 &C
 %), %)/M+/
	46n	;#D#D 	o6n	dl dlNk44 *Q+00 Q+00 r"   