
    	]j?                        d Z ddlZddlmZmZ ddlmZ ddlmZ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mZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"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- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3  eddd       G d de-jh                               Z5 G d d      Z6 e	dg       G d de+             Z7 G d de7      Z8 G d de-jr                        Z: G d de-jv                        Z< G d  d!e-jr                        Z= G d" d#e-jr                        Z> G d$ d%e-jr                        Z? G d& d'e-jv                        Z@ G d( d)e-jv                        ZA G d* d+e-jv                        ZBy),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)SAFE_HTML_ATTRIBUTESSAFE_HTML_TAGS)Q)extend_schema_fieldextend_schema_serializer)FSMStateField)LabelInterface)BrushLabelsTagBrushTag
ChoicesTagDateTimeTagEllipseLabelsTag
EllipseTagHyperTextLabelsTagKeyPointLabelsTagKeyPointTag	LabelsTag	NumberTagParagraphLabelsTagPolygonLabelsTag
PolygonTag	RatingTagRectangleLabelsTagRectangleTagTaxonomyTagTextAreaTagTimeSeriesLabelsTagVideoRectangleTag)ProjectProjectImportProjectOnboardingProjectReimportProjectSummary)FlexFieldsModelSerializer)serializers)SerializerMethodField)Task)UserSimpleSerializerobjectT)typeadditionalPropertiesc                       e Zd ZdZy)OpenApiObjectJSONFieldz
    A JSON field that is always rendered as a generic OpenAPI object.

    drf-spectacular may otherwise produce a schema with only metadata (e.g. nullable/readOnly/description)
    and omit `type`/`$ref`, which breaks some OpenAPI doc renderers.
    N)__name__
__module____qualname____doc__     K/root/env/lib/python3.12/site-packages/label_studio/projects/serializers.pyr-   r-   (   s    r3   r-   c                       e Zd ZdZd Zy)CreatedByFromContextTc                 8    |j                   j                  d      S )N
created_by)contextget)selfserializer_fields     r4   __call__zCreatedByFromContext.__call__5   s    ''++L99r3   N)r.   r/   r0   requires_contextr=   r2   r3   r4   r6   r6   2   s    :r3   r6   show_ground_truth_first)deprecate_fieldsc                       e Zd ZdZ ej
                  ddd      Z ej
                  ddd      Z ej
                  ddd      Z ej
                  ddd      Z	 ej
                  ddd	      Z
 ej
                  ddd
      Z ej
                  ddd      Z e e       d      Z eddd      Z eddd      Z eddd      Z eddd      Z ej&                  ddd      Z ej
                  ddd      Z ej&                         Z ej&                         Z ed      Zed        Zedefd       Z edefd       Z!ed        Z"defdZ# fdZ$d Z% G d d       Z&d! Z'd" Z( fd#Z)de*fd$Z+de*fd%Z, xZ-S )&ProjectSerializerzSerializer get numbers from project queryset annotation,
    make sure, that you use correct one(Project.objects.with_counts())
    NTzTotal task number in project)default	read_only	help_textzaTotal annotations number in project including skipped_annotations_number and ground_truth_number.z|Total predictions number in project including skipped_annotations_number, ground_truth_number, and useful_annotation_number.zUseful annotation number in project not including skipped_annotations_number and ground_truth_number. Total annotations = annotation_number + skipped_annotations_number + ground_truth_numberz%Honeypot annotation number in projectz5Skipped by collaborators annotation number in projectzTasks with annotations countzProject owner)rC   rE   Fz;Dict of weights for each control tag in metric calculation.)required
allow_nullrE   z%JSON-formatted labeling configurationzCStart model training after any annotations are submitted or updatedz,Flag to detect is project ready for labelingz3Flag to detect is project ready for bulk annotationzFinished tasks)rD   c                     	 | j                   d   j                  j                  S # t        $ r$ t	        t        | j                   d               cY S w xY w)Nrequest
user_cache)r9   useridKeyErrornextiter)r;   s    r4   user_idzProjectSerializer.user_idv   sJ    	:<<	*//222 	:T\\,7899	:s   "% *AAreturnc                 :    t        | j                               dkD  S )Nr   )lenget_parsed_configprojects    r4   get_config_has_control_tagsz-ProjectSerializer.get_config_has_control_tags}   s    7,,./!33r3   c                 V   t        | j                        }t        t        t        t
        t        t        t        t        t        t        t        t        t        t        t         g}|D ]  }|j#                  |      s y t$        t&        t(        t*        t,        t.        g}|D ]  }|j#                  |      }|D ]  }|j0                  j3                  dd      j5                         dk(  }|j0                  j3                  dd      j5                         dk(  }|s|r  y|t$        t&        fv ssd|j0                  v s  y  |j#                  t&              }	|	D ]S  }|j0                  j3                  dd      j5                         dk(  }
|
r y|j0                  j3                  dd       }|S y y	)
NF	perRegionfalsetrueperItemvaluelabelingapiUrlT)r	   label_configr   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   find_tags_by_classr   r   r   r   r   r   attrr:   lower)rV   lidisallowed_tags	tag_classallowed_tags_for_checkstagstag
per_regionper_itemtaxonomy_tagsr^   api_urls               r4   'get_config_suitable_for_bulk_annotationz9ProjectSerializer.get_config_suitable_for_bulk_annotation   s   G001 
& ) 	I$$Y/	
 $.{KT]_j"k0 
	%I((3D % XX\\+w?EEG6Q
88<<	7;AACvM [ 99#((*$%
	% --k:  	Cxx||J8>>@FJHhhll8T2G"	 r3   c                 "    | j                         S N)rT   rU   s    r4   get_parsed_label_configz)ProjectSerializer.get_parsed_label_config   s    ((**r3   c                 "    |j                   rdS dS )NTF)!min_annotations_to_start_training)r;   instances     r4   'get_start_training_on_annotation_updatez9ProjectSerializer.get_start_training_on_annotation_update   s    AAtLuLr3   c                     |}t         |   |      }d|v rt        |d         |d<   d|v r&t        j                  |d   t
        t              |d<   |S )N#start_training_on_annotation_updaters   expert_instruction)rh   
attributes)superto_internal_valueintbleachcleanr   r   )r;   datainitial_data	__class__s      r4   r{   z#ProjectSerializer.to_internal_value   sf    w(.0L@8;LIn<o8pD45</)/12Th*D%& r3   c                     |j                  d      r t        |      dk(  r	 t        |dd  d       |S t	        j
                  d      # t        $ r Y  w xY w)N#         z!Color must be in "#RRGGBB" format)
startswithrS   r|   
ValueErrorr%   ValidationErrorr;   r]   s     r4   validate_colorz ProjectSerializer.validate_color   s\    C SZ1_E!"Ir" ))*MNN  s   A 	AAc                   0    e Zd ZeZddiddiddidZg dZy)ProjectSerializer.MetarF   F)membershipstitler8   ).rL   r   descriptionr`   rx   show_instructionshow_skip_buttonenable_empty_annotationshow_annotation_historyorganizationcolormaximum_annotationsis_publishedmodel_versionis_draftr8   
created_atrs   rw   show_collab_predictionsnum_tasks_with_annotationstask_numberuseful_annotation_numberground_truth_numberskipped_annotations_numbertotal_annotations_numbertotal_predictions_numbersamplingr?   annotator_evaluation_enabledshow_overlap_firstoverlap_cohort_percentagetask_data_logintask_data_passwordcontrol_weightsparsed_label_config"evaluate_predictions_automaticallyconfig_has_control_tags
skip_queue#reveal_preannotations_interactively	pinned_atfinished_task_numberqueue_total
queue_done#config_suitable_for_bulk_annotationstateN)r.   r/   r0   r   modelextra_kwargsfieldsr2   r3   r4   Metar      s-    &. %(%u-

/
r3   r   c                     | j                   t        j                  |       |S | j                   j                  |       |S rp   )rt   r   validate_label_configvalidate_configr   s     r4   r   z'ProjectSerializer.validate_label_config  s:    == ))%0  MM))%0r3   c                     | j                   }||j                  |k7  rp|dk7  rk|j                  j                  |      j	                  |j
                  j                  ||            j                         r|S t        j                  d      |S )zCustom model_version validation )r   )rV   r   zJModel version doesn't exist either as live model or as static predictions.)	rt   r   ml_backendsfilterunionpredictionsexistsr%   r   )r;   r]   ps      r4   validate_model_versionz(ProjectSerializer.validate_model_version  s    MM =Q__5%2+ }}##%#066q}}7K7KTUej7K7klssu!11`  r3   c                 X    |j                  d      du rd|_        t        |   ||      S )Nr   Fr   )r:   r   rz   update)r;   rt   validated_datar   s      r4   r   zProjectSerializer.update-  s0    78EA%'H"w~h77r3   c                     |j                   j                  t        d      t        | j                         z  t        | j                        z        j	                         }|j                         S )NF)
is_labeled)annotations__completed_by_id)tasksr   r   rP   distinctcount)r;   rV   remains      r4   get_queue_totalz!ProjectSerializer.get_queue_total3  sZ    %%1$,,#O"OOT\\:;
 (* 	 ||~r3   c                     || j                   d}|j                  |j                  j                  k(  rd|d<   t	        j
                  j                  di |}|j                         j                         }|S )N)rV   r   Fannotations__was_cancelledr2   )	rP   r   	SkipQueueREQUEUE_FOR_MEr'   objectsr   r   r   )r;   rV   tasks_filteralready_done_tasksresults        r4   get_queue_donez ProjectSerializer.get_queue_done:  sn    ,0LL

 !2!2!A!AA9>L56!\\00@<@#,,.446r3   ).r.   r/   r0   r1   r%   IntegerFieldr   r   r   r   r   r   r   r(   r6   r8   r-   r   r   r&   rw   r   r   r   r   r   r   r   propertyrP   staticmethodboolrW   rn   rq   ru   r{   r   r   r   r   r   r|   r   r   __classcell__)r   s   @r4   rB   rB   9   sE    ++**44SqrK7{77>   8{77$   8{77;  3+220W ":!9!90g" ":!9!90N" &.B.DP_`J,43pO 10W +@1v+' 40^ +L+*K*K0e+' 4;33DD\lm3+335K2224JD)E: : 4 4 4 4D 4 4l + +M4 MO6
 6
p&8#  r3   rB   c                        e Zd Z G d d      Zy)ProjectCountsSerializerc                       e Zd ZeZg dZy)ProjectCountsSerializer.Meta)	rL   r   r   r   r   r   r   r   r   N)r.   r/   r0   r   r   r   r2   r3   r4   r   r   J  s    

r3   r   Nr.   r/   r0   r   r2   r3   r4   r   r   I  s    
 
r3   r   c                        e Zd Z G d d      Zy)ProjectOnboardingSerializerc                       e Zd ZeZdZy) ProjectOnboardingSerializer.Meta__all__N)r.   r/   r0   r!   r   r   r2   r3   r4   r   r   Z  s    !r3   r   Nr   r2   r3   r4   r   r   Y       r3   r   c                   t    e Zd Z ej                  ej                  j                  j                        Zd Z	y)ProjectLabelConfigSerializer)rE   c                 0    t        j                  |       |S rp   )r   r   )r;   configs     r4   r   z2ProjectLabelConfigSerializer.validate_label_configb  s    %%f-r3   N)
r.   r/   r0   r%   	CharFieldr   r`   fieldrE   r   r2   r3   r4   r   r   _  s+    (;((73G3G3M3M3W3WXLr3   r   c                        e Zd Z G d d      Zy)ProjectSummarySerializerc                       e Zd ZeZdZy)ProjectSummarySerializer.Metar   N)r.   r/   r0   r#   r   r   r2   r3   r4   r   r   h  s    r3   r   Nr   r2   r3   r4   r   r   g  r   r3   r   c                        e Zd Z G d d      Zy)ProjectImportSerializerc                       e Zd ZeZg dZy)ProjectImportSerializer.Meta)rL   rV   preannotated_from_fieldscommit_to_projectreturn_task_idsstatusurlerrorr   
updated_atfinished_at
task_countannotation_countprediction_countdurationfile_upload_idscould_be_tasks_listfound_formatsdata_columnsr   task_idsN)r.   r/   r0   r    r   r   r2   r3   r4   r   r   n  s    
r3   r   Nr   r2   r3   r4   r   r   m  s    
 
r3   r   c                        e Zd Z G d d      Zy)ProjectReimportSerializerc                       e Zd ZeZg dZy)ProjectReimportSerializer.Meta)rL   rV   r   r   r   r  r  r  r  files_as_tasks_listr  r  N)r.   r/   r0   r"   r   r   r2   r3   r4   r   r    s    
r3   r   Nr   r2   r3   r4   r
  r
    s    
 
r3   r
  c                   r    e Zd Z ej                         Z ej                         Z ej                         Z	y)%ProjectModelVersionExtendedSerializerN)
r.   r/   r0   r%   r   r   r   r   DateTimeFieldlatestr2   r3   r4   r  r    s2    )K))+M$K$$&E&[&&(Fr3   r  c                       e Zd Z ej                  dd      Z ej                  dd      Z ej                  dd      Zy)#ProjectModelVersionParamsSerializerF)rF   rC   N)	r.   r/   r0   r%   BooleanFieldextendedinclude_live_modelsr   limitr2   r3   r4   r  r    s?    '{''FH2+22E5Q$K$$eTBEr3   r  c                       e Zd Z ej                  dd      Z ej                  ddd      Z ej                  ddd      Zd	 Zd
 Z	y)GetFieldsSerializerFaY  Comma-separated list of count fields to include in the response to optimize performance. Available fields: task_number, finished_task_number, total_predictions_number, total_annotations_number, num_tasks_with_annotations, useful_annotation_number, ground_truth_number, skipped_annotations_number. If not specified, all count fields are included.)rF   rE   allzFilter projects by pinned status. Use 'pinned_only' to return only pinned projects, 'exclude_pinned' to return only non-pinned projects, or 'all' to return all projects.)rF   rC   rE   Nz-Search term for project title and descriptionc                 ,    ||j                  d      }|S )N,)splitr   s     r4   validate_includez$GetFieldsSerializer.validate_include  s    KK$Er3   c                     |dv r|S y )N)r  pinned_onlyexclude_pinnedr2   r   s     r4   validate_filterz#GetFieldsSerializer.validate_filter  s    <<L =r3   )
r.   r/   r0   r%   r   includer   searchr  r"  r2   r3   r4   r  r    sc    #k##pG #[""d	F #[""0_F
r3   r  )Cr1   r}   	constantsr   r   django.db.modelsr   drf_spectacular.utilsr   r   fsm.serializer_fieldsr    label_studio_sdk.label_interfacer	   -label_studio_sdk.label_interface.control_tagsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   projects.modelsr   r    r!   r"   r#   rest_flex_fieldsr$   rest_frameworkr%   rest_framework.serializersr&   tasks.modelsr'   users.serializersr(   	JSONFieldr-   r6   rB   r   ModelSerializerr   
Serializerr   r   r   r
  r  r  r  r2   r3   r4   <module>r4     sQ    :  O / ;     . g f 6 & <  2 hEF[22  G: : ,E+FGL1 L HL^
/ 
 +"="= ;#9#9 {:: 
k99 
8
 ; ; 
&)K,B,B )C+*@*@ C+00 r3   