o
    "iL*                    @   sL  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
m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 d dlmZmZmZ d d	lmZ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-Z.d dl/Z.d dl0T d dl1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZe d dlfmgZg d dlhmiZi d dljmkZkmlZlmmZmmnZnmoZompZpmqZqmrZr G dd de$ZsdS )    N)defaultdict)AnyDictListOptionalUnion)
exceptions)
validators)models)gettext_lazy)permissions	renderersserializers)_UnvalidatedFieldempty)CreateAPIViewGenericAPIViewListCreateAPIView)ListModelMixin)ViewInspector)get_pk_description)api_settings)get_field_info)APIView)*)add_trace_messageerrorget_overridehas_overridewarn)OpenApiAuthenticationExtensionOpenApiFilterExtensionOpenApiSerializerExtensionOpenApiSerializerFieldExtension))ComponentRegistryResolvedComponentUnableToProceedErrorappend_metaassert_basic_serializerbuild_array_typebuild_basic_typebuild_choice_fieldbuild_examples_listbuild_generic_typebuild_listed_example_valuebuild_media_type_objectbuild_mocked_viewbuild_object_typebuild_parameter_typebuild_serializer_contextfilter_supported_argumentsfollow_field_sourcefollow_model_field_lookupforce_instanceget_docget_list_serializerget_managerget_type_hintsget_view_modelis_basic_serializeris_basic_typeis_fieldis_higher_order_type_hintis_list_serializeris_list_serializer_customizedis_patched_serializeris_serializeris_trivial_string_variation!modify_media_types_for_versioningresolve_django_path_parameterresolve_regex_path_parameterresolve_type_hintsafe_ref!sanitize_specification_extensionswhitelisted)spectacular_settings)OpenApiTypes)	DirectionOpenApiCallbackOpenApiExampleOpenApiParameterOpenApiRequestOpenApiResponse_SchemaType_SerializerTypec                   @   sN  e Zd ZddddddZdeded	ed
ededee fddZde	fddZ
ddee de	fddZde	fddZdeeeef  fddZdddZdee fddZdee fddZdefd d!Zdee fd"d#Zdeeeef  fd$d%Zdeeeeef ef  fd&d'Zdee fd(d)Zdee fd*d+Zdee fd,d-Zdee fd.d/Zdefd0d1Zd2d3 Zdee  fd4d5Z!defd6d7Z"de	fd8d9Z#dee fd:d;Z$d<d= Z%dee& fd>d?Z'd@dA Z(dBdC Z)dDdE Z*ddGdHZ+dIe&dJeddfdKdLZ,ddMdNZ-dOdP Z.dQdR Z/dSdT Z0dUdV Z1dWdX Z2dYdZ Z3d[edefd\d]Z4dee& fd^d_Z5d`edee& fdadbZ6dcdd Z7dee8 fdedfZ9ddgdhZ:ddidjZ;ddkdlZ<ddne=defdodpZ>dne=dee fdqdrZ?ddsdtZ@ddefdudvZAdne=de&fdwdxZBdeCjDdne=defdydzZEddefd{d|ZF	Fddedne=deGfd}d~ZHdS )
AutoSchemaretrievecreateupdatepartial_updatedestroy)getpostputpatchdeletepath
path_regexpath_prefixmethodregistryreturnc                 C   s  || _ || _|| _|| _| | _|  rd S d|  i}|  }|r'||d< | 	 }|r1||d< | 
 }	|	r;|	|d< |  }
|
rE|
|d< |  }|rO||d< |  }|rY||d< |  }|rc||d< |  }|rm||d	< |  |d
< |  }|r|t| |  }|r||d< |S )NoperationIddescriptionsummaryexternalDocs
parameterstagsrequestBodysecurity
deprecated	responses	callbacks)rf   rb   rc   rd   upperre   is_excludedget_operation_idget_descriptionget_summary_get_external_docs_get_parametersget_tags_get_request_bodyget_authis_deprecated_get_response_bodiesget_extensionsrZ   rK   _get_callbacks)selfrb   rc   rd   re   rf   	operationri   rj   external_docsrl   rm   request_bodyauthrp   
extensionsrr    r   \/var/www/html/crm_dreinet/venv_linux/lib/python3.10/site-packages/drf_spectacular/openapi.pyget_operation=   sP   
zAutoSchema.get_operationc                 C      dS $ override this for custom behaviour Fr   r   r   r   r   rt   |      zAutoSchema.is_excludedN
serializerc                 C   s   |du r|   }t|tr|rdd | D }|t| }t|r$dS t|r*dS t| jdr6| jj	dkS | j
dkr=dS t| jtrEdS t| jtr]| jjpR| jj}|t| jv r]dS dS )	z
        partially heuristic approach to determine if a view yields an object or a
        list of objects. used for operationId naming, array building and pagination.
        defaults to False if all introspection fail.
        Nc                 S   s   i | ]	\}}t ||qS r   str).0codesr   r   r   
<dictcomp>   s    z,AutoSchema._is_list_view.<locals>.<dictcomp>TFactionlistGET)get_response_serializers
isinstancedictitemsminrA   r>   hasattrviewr   re   r   r   lookup_url_kwarglookup_fielduritemplate	variablesrb   )r   r   r   r   r   r   _is_list_view   s(   
zAutoSchema._is_list_viewc                 C   s<   | j dkrdS t| jdd dkrdS t| jttfrdS dS )NPOSTFr   rY   T)re   getattrr   r   r   r   r   r   r   r   _is_create_operation   s   
zAutoSchema._is_create_operationc                 C      g S r   r   r   r   r   r   get_override_parameters   r   z"AutoSchema.get_override_parametersrequestc           	      C   s  i }|   D ]}t|tr|jrqt|jrt|j}n"t|jr*| |j|j	}nt|jt
r4|j}n
td|j d q|jrTt|jrKt|}n	td|j d |jr`d ||j|jf< qt|j||j|j|j|j|j|j|j|j|j|jt|j|jd||j|jf< qt|rt|}| |d}|d   D ]8\}}z|j!"|}W n t#y   d }Y nw t|||$dd tj%t&|d	d
t'||"dg v d||tj%f< qqtd| d q|S )Nz unsupported type for parameter "z". Skipping.zparameter "z^" has many=True and is not a basic type. many=True only makes sense for basic types. Ignoring.nameschemalocationrequiredri   enumpatternrp   styleexplodedefaultallow_blankexamplesr   r   
propertiesri   r   Tr   )r   r   ri   r   r   r   z'could not resolve parameter annotation z. Skipping.)(r   r   rR   responser>   typer*   r=   resolve_serializerrefr   r   r   manyr)   excluder   r2   r   ri   r   r   rp   r   r   r   r   r,   r   r   r7   _map_serializerr   fieldsr]   	ExceptionpopQUERYr   bool)	r   	directionresult	parameterr   mappedproperty_nameproperty_schemafieldr   r   r   _process_override_parameters   sr   






z'AutoSchema._process_override_parametersc                 C   sD   g }|  d}tjr t|dkr |ttjttjt	j
|d |S )Nformat   )r   r   r   r   )map_renderersr   URL_FORMAT_OVERRIDElenappendr2   r*   rN   STRrR   r   )r   rl   formatsr   r   r   _get_format_parameters   s   
z!AutoSchema._get_format_parametersc           	         s@  dd }|     fddt| jD }i || |||  ||  ||  }  D ]\}}|d u rD||v rC||= q4|||< q4t	t
}|  D ]}|jr^||j | qQ| D ]\}}||v r~|| di  || d t| qcttjrt| tjdS tjrt| dd dS t
| S )	Nc                 S   s   dd | D S )Nc                 S   s   i | ]}|d  |d f|qS )r   inr   r   pr   r   r   r      s    zCAutoSchema._get_parameters.<locals>.dict_helper.<locals>.<dictcomp>r   )rl   r   r   r   dict_helper   s   z/AutoSchema._get_parameters.<locals>.dict_helperc                    s   g | ]
}|d f vr|qS )rb   r   )r   voverride_parametersr   r   
<listcomp>  s    z.AutoSchema._get_parameters.<locals>.<listcomp>r   )keyc                 S   s   | d S )Nr   r   )r   r   r   r   <lambda>#  s    z,AutoSchema._get_parameters.<locals>.<lambda>)r   r   r   rb   _resolve_path_parameters_get_filter_parameters_get_pagination_parametersr   r   r   r   get_examplesparameter_onlyr   
setdefaultrZ   r,   callablerM   SORT_OPERATION_PARAMETERSsortedvalues)	r   r   path_variablesrl   r   r   examples_by_keyexampler   r   r   r   ry      sF   






zAutoSchema._get_parametersc                 C   s:   t | jt | jd| j d}t| jj}t|}|p|S )r   r   N)r   r   re   lowerr8   	__class__)r   action_or_methodview_doc
action_docr   r   r   rv   '  s   zAutoSchema.get_descriptionc                 C   r   r   Nr   r   r   r   r   rw   .  r   zAutoSchema.get_summaryc                 C   s   |   }t|trd|iS |S )Nurl)get_external_docsr   r   )r   r   r   r   r   rx   2  s   
zAutoSchema._get_external_docsc                 C   r   r   r   r   r   r   r   r   9  r   zAutoSchema.get_external_docsc           
   
   C   s@  g }| j  D ]g}t|tjdsqt|}|s"td|j d q|	| }|dur;t
|tr6|| n|| t
|jtrM|jg|| g}}n	|j|| }}t||D ]\}}| jt|tj|j|d q[qtjrx|tj dd | j  D }	tj|	v r|i  |S tj|	v r| jtjv r|i  |S )a#  
        Obtains authentication classes and permissions from view. If authentication
        is known, resolve security requirement for endpoint and security definition for
        the component section.
        For custom authentication subclass ``OpenApiAuthenticationExtension``.
        Tz could not resolve authenticator z~. There was no OpenApiAuthenticationExtension registered for that class. Try creating one by subclassing it. Ignoring for now.N)r   r   objectr   c                 S   s   g | ]}|j qS r   )r   r   r   r   r   r   l  s    z'AutoSchema.get_auth.<locals>.<listcomp>)r   get_authenticatorsrL   rM   AUTHENTICATION_WHITELISTr    	get_matchr   r   get_security_requirementr   r   r   extendr   r   get_security_definitionziprf   register_on_missingr%   SECURITY_SCHEMASECURITYget_permissionsr   AllowAnyIsAuthenticatedOrReadOnlyre   SAFE_METHODS)
r   authsauthenticatorschemesecurity_requirementsnamesdefinitionsr   
definitionpermsr   r   r   r|   =  sJ   







zAutoSchema.get_authc                 C      |   S r   _get_serializerr   r   r   r   get_request_serializers     z!AutoSchema.get_request_serializerc                 C   r  r   r  r   r   r   r   r   w  r  z#AutoSchema.get_response_serializersc                 C   s   |   }|dd S )r   Nr   )_tokenize_path)r   tokenized_pathr   r   r   rz   {  s   zAutoSchema.get_tagsc                 C   s   i S Nr   r   r   r   r   r     s   zAutoSchema.get_extensionsc                 C   s  i }|   D ]}t|jtr|j}nd|ji}i }| D ]e\}}t|tr-||| < qt||j|| jd}i }|j	
 }	|	rD|	|d< |j	 }
|
rO|
|d< |j	d}|r[||d< |j	 }|rf||d< |j	d|d	< |j	 }|r||t| ||| < q|j|i||j< q|S )
a  
        Creates a mocked view for every callback. The given extend_schema decorator then
        specifies the expectations on the receiving end of the callback. Effectively
        simulates a sub-schema from the opposing perspective via a virtual view definition.
        r^   )re   rb   extend_schema_decoratorrf   ri   rj   r   rn   rp   r   rq   )get_callbacksr   	decoratorr   r   r   r0   rb   rf   r   rv   rw   r{   r}   r~   r   rZ   rK   r   )r   r   callbackmethods
path_itemsre   r  mocked_viewr   ri   rj   r   rp   r   r   r   r   r     sH   





zAutoSchema._get_callbacksc                 C   r   r   r   r   r   r   r   r    r   zAutoSchema.get_callbacksc                 C   s   |   }dd |D }| jdkr|  rd}n| j| j  }|s&|d td| jr2|d t	j
dkr?d	|g| S t	j
d
krLd	||g S J d)r   c                 S   s   g | ]}| d dqS )-_)replacer   tr   r   r   r         z/AutoSchema.get_operation_id.<locals>.<listcomp>r   r   rootz<drf_format_suffix\w*:\w+>	formattedPREr  r   FzBInvalid value for OPERATION_ID_METHOD_POSITION. Allowed: PRE, POST)r  re   r   method_mappingr   r   researchrc   rM   OPERATION_ID_METHOD_POSITIONjoin)r   r  r   r   r   r   ru     s   



zAutoSchema.get_operation_idc                 C   r   r   r   r   r   r   r   r}     r   zAutoSchema.is_deprecatedc                 C   sL   t j| jd| jt jd}t jdd|d}|ddd}dd |D S )N )r   replstringflagsz\{[\w\-]+\})r   r*  r+  /c                 S   s   g | ]}|r|qS r   r   r  r   r   r   r         z-AutoSchema._tokenize_path.<locals>.<listcomp>)r%  subrd   rb   
IGNORECASErstriplstripsplit)r   rb   r  r   r   r   r    s   zAutoSchema._tokenize_pathc           
      C   sF  t | jdd}g }|D ]}ttj}d}t| j|| d}|s&t| j|}|r-|d }ng|d u r=t	d| d| d nWz<t
| jd	d |krOt
| jd
|}n|dr^|d d  d}n|}t||}	| j|	d d}d|vrx|	jrxt||	}W n tjy   t	d| d|j d|j d Y nw |t|tj||d q|S )NF)emit_warningsr)  r   r   z)could not derive type of path parameter "zv" because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:zP>) or annotating the parameter type with @extend_schema. Defaulting to "string".r   r   _pk_idr   ri   z" because model ".ze" contained no such field. Consider annotating parameter with @extend_schema. Defaulting to "string".)r   r   ri   r   )r<   r   r*   rN   r   rG   rc   r   rH   r   r   endswithr6   _map_model_fieldprimary_keyr   django_exceptions
FieldError
__module____name__r   r2   rR   PATH)
r   r   modelrl   variabler   ri   resolved_parametermodel_field_namemodel_fieldr   r   r   r     sZ   





z#AutoSchema._resolve_path_parametersc                 C   s   |   sg S t| jdg S )r   filter_backends)r   r   r   r   r   r   r   get_filter_backends   s   zAutoSchema.get_filter_backendsc                 C   sH   g }|   D ]}t| }|r||| 7 }q|| | j7 }q|S r  )rH  r!   r   get_schema_operation_parametersr   )r   rl   filter_backendfilter_extensionr   r   r   r   &  s   z!AutoSchema._get_filter_parametersc                 C   s@   |   sg S |  }|sg S t|}|r|| S || jS r  )r   _get_paginatorr!   r   rI  r   )r   	paginatorrK  r   r   r   r   0  s   

z%AutoSchema._get_pagination_parametersc                 C   s  t |tjsJ zt j|jt|j|jdd\}}|di |}|j|_	W n   d }Y |rGt |tj
rG|jsAt|j |_| ||S t |tjr`|jjsZt|j |j_| ||S |rqt |tjtjfsq| ||S t |tjr~| |j|S ttdrt |tjrttjS t |tjrttjS tt| rtt| }tjj !|}|st"d|  d ttj#S | | |S t$d| d ttj#S )	Nr   )
field_nameinfomodel_classnested_depth	JSONFieldzmodel field "zY" has no mapping in ModelSerializer. It may be a deprecated field. Defaulting to "string"zcould not resolve model field "z}". Failed to resolve through serializer_field_mapping, get_internal_type(), or any override mechanism. Defaulting to "string"r   )%r   r
   Fieldr   ModelSerializerbuild_fieldr   r   rB  rN  PrimaryKeyRelatedFieldquerysetr:   related_modelnone_map_serializer_fieldManyRelatedFieldchild_relationReadOnlyField
ModelField
ForeignKeyr;  target_fieldr   rR  r*   rN   ANYBinaryFieldBYTEget_internal_typer   serializer_field_mappingr]   r   r   r   )r   rF  r   	field_clsfield_kwargsr   internal_typer   r   r   r;  >  sR   





zAutoSchema._map_model_fieldFc                    s	  |  ||}t|drYt|d}t|rt|}|d u rd S nt|r(t|}nt|tr0|}n| 	t
||}t|d}|rTt|tj||d}| j| t|j|S t||S t|}	|	r|s|	| |}|d u rnd S |	 rt|	 tj||d}| j| t|j|S t||S t|r| ||}|rt||S d S t|r| ||}|rt|j|S d S t|tjr| 	|j|}|dd  |d|dkr|dd  tt||S t|tjtj frt|tj }
|
st!|dd ur| 	|j"|}t||S t!|dd d ur+|
r$|j#j$}|j%g}t&||t'( d}nX|j#j$j)j*}nQt|j+tjrA|j+j+j,j$}|j+j-.d	}n)t/|j+d
rT|j+j,j$}|j-.d	}nt0d|j1j2 d|j3 d ttt4j5|S |
rs|6|j% t&||t'( d}t7|r| 8|}nt|t'j9r| :||}n	J d|j3 d|dd  t||S t|tj;rttt4j5|S t|tj<rttt4j=|S t|tj>rttt4j=|S t|tj?rttt@||S t|tjArt@|}d|v rd|v r|d d |d |d< t||S t|tjBrdt|jCtDr%tttt4jE|S tF|jCr?| |jC|}|r=tt|j|S d S | 	|jC|}| G|jC| tH|j3|dr]|dd  tt||S t|tjIrsttt4jJ|S t|tjKrttt4jL|S t|tjMrttt4jN|S t|tjOrttt4jP|S t|tjQrttt4j=|S t|tjRrttt4jS|S t|tjTrttt4j5|S t|tjUrd|jVW krtt4jX}nd|jVW krtt4jY}ntt4j5}t||S t|tjZrJt!|dt[j\r&i tt4j5ddi |j]r%d|j] d|j^ d d< ntt4j_ |j]r?d|j] } `|| ddd | a|  t |S t|tjbratt4jc | a|  t |S t|tjdrtt4je | a|  tf fdd d!D sd" d< t |S t|tjgrthjir|d#krtt4jj nt!|d$t[jk}t|rt4j=nt4j5 t |S t|tjlrt!|j+|jmd }|d u rtnd%|j3 d&|jm d' ttt4j5|S t| 8||S t/td(rtjotjpf}ntjof}t||rttt4jq|S t|tjrrttt4jE|S t|tjstjtfrAtt4ju t|jCtDs<| 	|jC| d)< | G|jC d)  t |S t|tjvrPttt4j5|S t|tjwrt|j+rnt!t!|j+j+d
d d*d }|j+jx}nt!t!|j+d
d d*d }|jx}|d u rt0d+|j3 d, ttt4j5|S t&||}t/|d-r|jyjzj2d.krtt{}n!t7|r| 8|}nt|t'j9r| :||}nJ d/| d0t||S t|tj|r| :|j}|}t||S t0d1| d2 ttt4j5|S )3Nr   field_component_namer   r   r   r   readOnlyri   pk_fieldrW  )r   r9  Metaz*Could not derive type for under-specified z "z". The serializer has no associated model (Meta class) and this particular field has no type without a model association. Consider changing the field or adding a Meta class. Defaulting to string.FzField "z3" must point to either a property or a model field.z

titleipv4ipv6coerce_to_stringr   decimalz^-?\d{0,z}(?:\.\d{0,z})?$r   
   T)maximumminimumexclusiveMaximumexclusiveMinimumc                 3   s4    | ]}d t  |d  kodkn  V  qdS )i   r   iN)intr]   )r   r   contentr   r   	<genexpr>M  s   2 z3AutoSchema._map_serializer_field.<locals>.<genexpr>rt  ru  int64r   use_urlzSerializerMethodField "z" is missing required method "z". defaulting to "string".NullBooleanFieldadditionalPropertiesrB  z)Could not derive type for ReadOnlyField "z" because the serializer class has no associated model (Meta class). Consider using some other field like CharField(read_only=True) instead. defaulting to string._partialmethod_get_FIELD_displayzReadOnlyField target "z!" must be property or model fieldz$could not resolve serializer field "z". Defaulting to "string")~_get_serializer_field_metar   r   r>   r*   r@   rI   r   r   rZ  r7   r%   SCHEMArf   r   r'   r   r#   r   map_serializer_fieldget_namerA   _unwrap_list_serializerrD   r   r   r[  r\  r   r]   r)   rV  SlugRelatedFieldr   rl  rW  rB  
slug_fieldr5   r
   	TextField_metapkparentrm  sourcer3  r   r   r   r@  rN  rN   r   r   r   _map_response_type_hintrS  r;  StringRelatedFieldHyperlinkedIdentityFieldURIHyperlinkedRelatedFieldMultipleChoiceFieldr+   ChoiceField	ListFieldchildr   ra  r=   _insert_field_validatorsrE   	DateFieldDATEDateTimeFieldDATETIME	TimeFieldTIME
EmailFieldEMAILURLField	UUIDFieldUUIDDurationFieldIPAddressFieldprotocolr   IP4IP6DecimalFieldr   COERCE_DECIMAL_TO_STRINGmax_whole_digitsdecimal_placesDECIMALrZ   _insert_min_max
FloatFieldDOUBLEIntegerFieldINTall	FileFieldrM   COMPONENT_SPLIT_REQUESTBINARYUPLOADED_FILES_USE_URLSerializerMethodFieldmethod_namer   BooleanFieldr  BOOLrR  	DictFieldHStoreFieldOBJECT	CharFieldr]  source_attrsr  funcr   r^  rF  )r   r   r   bypass_extensionsmetaoverrider   ri  	componentserializer_field_extensionis_slugrB  r  rF  valuer~  re   boolean_field_classestargetr   ry  r   rZ  w  s  


































z AutoSchema._map_serializer_fieldr   rz  c                 C   sP   |j d ur|j |d< d|v r|d= |jd ur$|j|d< d|v r&|d= d S d S d S )Nrt  rv  ru  rw  )	max_value	min_value)r   r   rz  r   r   r   r    s   




zAutoSchema._insert_min_maxc                 C   s`   t |}t|}|r|s|| |}n| ||}t|di }|r)|t| | |||S )Nr   )	r7   r"   r   map_serializer_map_basic_serializerr   rZ   rK   _postprocess_serializer_schema)r   r   r   r  serializer_extensionr   r   r   r   r   r     s   
zAutoSchema._map_serializerc                 C   s   t js|S |dg }|dg }t|D ]4}|dkr0|| dr0|d |= ||v r0|| |dkrI|| drI|d |= ||v rI|| qd|v rS|sS|d= |S )z
        postprocess generated schema for component splitting, if enabled.
        does only apply to direct component schemas and not intermediate schemas
        like components composed of sub-component via e.g. oneOf.
        r   r   r   rk  r   	writeOnly)rM   r  r]   r   remove)r   r   r   r   r   r   	prop_namer   r   r   r    s"   



z)AutoSchema._postprocess_serializer_schemac                 C   s0  t |tjsi S i }|jrd|d< |jrd|d< |jrd|d< t |tjr>|js>tj	r4|dkr3d|d< n
tj
r>|js>d|d< |jd ur{|jtkr{t|js{t |tjtjtjtjfr^|j}nz||j}W n   |j}Y t |trwt|}||d< |jrt|j|jst|j|d	< |jrt|j|d
< |S )NTrk  r  nullabler   r   	minLengthr   rn  ri   )r   r   rS  	read_only
write_only
allow_nullr  r   rM   r  ENFORCE_NON_BLANK_FIELDSr   r   r   r^  r  rV  r  to_representationsetr   labelrE   rN  r   	help_text)r   r   r   r  r   r   r   r   r    sN   
z%AutoSchema._get_serializer_field_metac                 C   s   t | t|}|js|jt| j t }i }|j D ]L}t	|t
jr'q|jt|dg v r1q| ||}|d u r<q|jpG|doGtj }|rP||j | || |jt|dg v rcd|d< t|||j< qt||rrg }t||t|jdS )Nexclude_fieldsrk  deprecate_fieldsTrp   )r   r   ri   )r(   r7   contextrZ   r3   r   r  r   r   r   r   HiddenFieldrN  r   rZ  r   r]   rM   COMPONENT_NO_READ_ONLY_REQUIREDaddr  rJ   rC   r1   r8   r   )r   r   r   r   r   r   r   add_to_requiredr   r   r   r    s<   
z AutoSchema._map_basic_serializerc                 C   sv  | d}t|trdd |D d }dddd}|jD ]}|d	krt|tjr3d
|vr2d|d
< qt|tjrBd
|vrAd|d
< qt|tjrkd|vrj|jj	dd
 }|dd}|dddd}||d< qt|tjrz||dt|j qt|tjr||dt|j qt|tjr|jrd|vrdddd |jD  |d< q|dv rt|tjr||dt|j qt|tjr||dt|j qt|tjr|jrd|j|jpd  }||dt|dd ||dt| dd q|d krt|tjr||d!t|j qt|tjr||d"t|j q|d#kr8t|tjr)||d$t|j qt|tjr8||d%t|j qd S )&Nr   c                 S   s   g | ]}|d kr|qS )nullr   r  r   r   r   r   9  r   z7AutoSchema._insert_field_validators.<locals>.<listcomp>r   F)	exclusivec                S   s   t |r| }| |}|d ur|||}n|}|| |< |dv r>d|  }|r5||kr3d| |< d S d S || v r@| |= d S d S d S )Nr|  r  T)r   r]   rn  )r   r   functionr  r  current_value	new_valueexclusive_keyr   r   r   update_constraint;  s"   

z>AutoSchema._insert_field_validators.<locals>.update_constraintr+  r   emailurir   asciibackslashreplacez\xz\u00z\Z$z\A^	maxLengthr  z(?:%s)$|c                 S   s   g | ]}t |qS r   )r%  escape)r   extnr   r   r   r   `      )integernumberrt  ru  rs  TarraymaxItemsminItemsr   maxPropertiesminProperties)r]   r   r   r	   EmailValidatorURLValidatorRegexValidatorregexr   encodedecoder  MaxLengthValidatorr   limit_valueMinLengthValidatormaxFileExtensionValidatorallowed_extensionsr(  MaxValueValidatorMinValueValidatorDecimalValidator
max_digitsr  )r   r   r   schema_typer  r   r   r  r   r   r   r  4  sh   



z#AutoSchema._insert_field_validatorsc                 C   s   t |dpt|d}t|st|r| t|dS t|tr#|S zt	|}W n t
y@   td|j d ttj Y S w tt|tjrK|jn|}|rT||d< |S )Nr   rg   r   z*unable to resolve type hint for function "zL". Consider using a type hint or @extend_schema_field. Defaulting to string.ri   )r   r;   r]   rD   r?   rZ  r7   r   r   rI   r&   r   r@  r*   rN   r   r8   	functoolspartialr  )r   re   hintr   ri   r   r   r   r  u  s&   
z"AutoSchema._map_response_type_hintc                 C   s   t | jdd }|r| S d S )Npagination_class)r   r   )r   r  r   r   r   rL    s   zAutoSchema._get_paginatorserializer_namec                 C   s   d| dS )N	Paginatedr   r   )r   r  r   r   r   get_paginated_name  s   zAutoSchema.get_paginated_namec                 C   s   t tdd | j D S )Nc                 S   s   g | ]}t |tjr|jqS r   )rL   rM   PARSER_WHITELIST
media_typer   r   r   r   r     s    
z*AutoSchema.map_parsers.<locals>.<listcomp>)r   r   fromkeysr   get_parsersr   r   r   r   map_parsers  s   
zAutoSchema.map_parsers	attributec                    s8    dv sJ dd t t fdd| j D S )N)r  r   c                 S   s$   t jd urt| t jS t| tj S r  )rM   RENDERER_WHITELISTrL   r   r   BrowsableAPIRenderer)rr   r   r   use_renderer  s   
z.AutoSchema.map_renderers.<locals>.use_rendererc                    s2   g | ]}|rt | rt| d d qS );r   )r   r   r3  )r   r  r  r  r   r   r     s    z,AutoSchema.map_renderers.<locals>.<listcomp>)r   r   r  r   get_renderers)r   r  r   r!  r   r     s
   
zAutoSchema.map_renderersc              
   C   s   | j }t|}zVt|tr#|jjtjkr| |dW S |j|dW S t|trWtt	|dd r7|j|dW S tt	|dd rG| |dW S t
|drP|jW S td W d S td W d S  tyx } ztd| d W Y d }~d S d }~ww )	N)r  get_serializerget_serializer_classserializer_classzunable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method). Ignoring view for now.zOEncountered unknown view base class. Please report this issue. Ignoring for nowzexception raised while getting serializer. Hint: Is get_serializer_class() returning None or is get_queryset() not working without a request? Ignoring the view for now. (Exception: ))r   r3   r   r   r   r#  r$  r   r   r   r   r%  r   r   )r   r   r  excr   r   r   r    s4   



zAutoSchema._get_serializerc                 C   r   r   r   r   r   r   r   r     r   zAutoSchema.get_examplesc                 C   sL  dd |   D }|s!t|rt|jdg }n
t|r!t|dg }|p$g }g }|| D ]v}|dkr5|jr5q+|dkr=|jr=q+|jsI||v rF|nd}	n|j}	|rS||	krSq+|js`||v r]|fnd}
nt	t
t|j}
|ro||
vroq+| |rt|dd	ur|dksd
|  krdk sn tjrt|}t|j|  ||_|| q+t|S )zP Handles examples for request/response. purposefully ignores parameter examples c                 S   s   g | ]}|j s|qS r   )r   )r   er   r   r   r     r  z,AutoSchema._get_examples.<locals>.<listcomp>r   r   r   zapplication/json)200201r   Fr)  300)r   rA   r   r  rD   response_onlyrequest_onlyr  status_codestuplemapr   r   rM    ENABLE_LIST_MECHANICS_ON_NON_2XXcopyr.   r  rL  r   r,   )r   r   r   r  status_codeextrasr   filtered_examplesr   example_media_typeexample_status_codesr   r   r   _get_examples  sB   
zAutoSchema._get_examplesc                 C   s
  | j dvrd S |  }d}g }t|tr| }n
t|  t|}|D ]J\}}t|t	r:|j
|j|j}}}	nd\}	}|	rM|dkrM|dsMtd | |||d |}| ||\}
}|
d uro|||
||	f ||M }q%|std S ddd	 |D i}|r||d
< |S )N)PUTPATCHr   TNNz!application/x-www-form-urlencoded	multipartzxEncodings object on media types other than "application/x-www-form-urlencoded" or "multipart/*" have undefined behavior.rz  c                 S   s"   i | ]\}}}}|t |||qS r   )r/   )r   r  r   r   encodingr   r   r   r   3  s    
z0AutoSchema._get_request_body.<locals>.<dictcomp>r   )re   r  r   r   r   r   r  	itertoolsrepeatrS   r   r   r=  
startswithr   r8  _get_request_for_media_typer   )r   r   request_serializerrequest_body_requiredrz  media_types_iterr  r   r   r=  r   partial_request_body_requiredr   r   r   r   r{   	  sF   



zAutoSchema._get_request_bodyc                    s6  t |}t|r| ||}t|}||fS t|rX| jdkr't|}d|_| ||}|s1dS |j	}dd |j
di  D  |j
dg }t fdd	|D }||fS t|rft|}d
}||fS t|rtt|}d
}||fS t|tr|}d
}||fS td| j d| j d t }d|d< d
}||fS )Nr:  T)NFc                 S   s   g | ]\}}| d r|qS )rk  )r]   )r   r   r   r   r   r   r   O  s
    z:AutoSchema._get_request_for_media_type.<locals>.<listcomp>r   r   c                 3   s    | ]}| vV  qd S r  r   )r   reqreadonly_propsr   r   r{  S  s    z9AutoSchema._get_request_for_media_type.<locals>.<genexpr>Fz#could not resolve request body for  zN. Defaulting to generic free-form object. (Maybe annotate a Serializer class?)zUnspecified request bodyri   )r7   rA   r  r   rD   re   r2  r  r   r   r   r]   r   anyr>   r*   r@   rI   r   r   r   rb   r-   )r   r   r   r   rC  r  required_propsr   rG  r   rA  <  sL   %


	z&AutoSchema._get_request_for_media_typer   r   c           	      C   sN  |   }t|st|st|st|tr:| jdkr"ddtdiiS |  r0d| j	|d|diS d| j	|d|diS t|t
ri }| D ]:\}}t|tr\t|d |d	d  }}nt|d }}| 	||||}||v r{|| d
 |d
  qE|||< qE|S td| d| j d| j d ttj}td|d< d| j	|d|diS )NDELETE204ri   No response bodyr*  r8  r)  r   r   rz  could not resolve "" for rI  l. Expected either a serializer or some supported override mechanism. Defaulting to generic free-form object.Unspecified response body)r   rD   r>   r@   r   rT   re   r  r   _get_response_for_coder   r   r/  r   rZ   r   rb   r*   rN   r  )	r   r   response_serializersrq   r   r   media_typescontent_responser   r   r   r   r~   h  s>   




zAutoSchema._get_response_bodiesc                 C   sd   t |r
| ||S t|r| ||}|r|jS d S t|r.| |j|}|r,t|S d S J d)NFzSerializer is of unknown type.)	r?   rZ  r=   r   r   rA   r  r  r)   )r   r   r   r  r   r   r   r   r    s   z"AutoSchema._unwrap_list_serializerc           	         s  t trjjj}ndg }t }|r&d|ini }s6i |d|p3tdiS trP	j
 sOi |d|pLtdiS nStrl }|shi |d|petdiS |jn7trutn.tr~tn%t trd ntd dj dj d ttjtd	d< rtd
durd  krdk sn tjrtrttrԈ t nt!" }|rtrtrtj
rt#$%dt#j&|'(trj
ndd}j)*| |jn|r|'|s'+d}t,j-|}i | fdd|D |dS )Nr)  headersri   rN  rO  rP  rI  rQ  rR  r   Fr)  r+  r   rj  r  c                    s&   i | ]}|t  |qS r   )r/   r8  )r   r  r   r   r   r   r   r3  r   r   r     s    z5AutoSchema._get_response_for_code.<locals>.<dictcomp>)rz  ri   ).r   rT   r   ri   r   r7   _get_response_headers_for_coder  rA   r  r  rD   r   r   r>   r*   r@   rI   r   r   re   rb   rN   r  r   r   rM   r1  rB   r"   r   r9   r   r)   rL  r%   r  _get_serializer_namer  get_paginated_response_schemaget_serializer_identityrf   r   r   rF   r   )	r   r   r3  rU  r   ri   rW  r  rM  r   rX  r   rS    s   







z!AutoSchema._get_response_for_codec                 C   s  i }|   D ]}t|tsq|jsqt|jtr#|dd |jD vr#qt|jr.t|j}nt|jr<| 	|j|j
}n|j}|sKtd|j d q|jtjtjfvr\td|j  t|j||j|j|j|j|j|j|j|j|j|jt|j|jd}|d= |d= |||j< q|S )	Nc                 S   s   g | ]}t |qS r   r   )r   r   r   r   r   r      r.  z=AutoSchema._get_response_headers_for_code.<locals>.<listcomp>zresponse parameter z requires non-empty schemaz:incompatible location type ignored for response parameter r   r   r   )r   r   rR   r   r   r>   r   r*   rD   r   r   r   r   r   HEADERCOOKIEr2   r   ri   r   r   rp   r   r   r   r   r,   r   r   )r   r3  r   r   r   r   parameter_typer   r   r   rY    sP   



z)AutoSchema._get_response_headers_for_codec                 C   s*   t |}|r|| |}|d ur|S |S r  )r"   r   get_identity)r   r   r   r  identityr   r   r   r\  (  s   
z"AutoSchema.get_serializer_identityc                 C   s   |j jS r   )r   r@  )r   r   r   r   r   r   get_serializer_name1  r  zAutoSchema.get_serializer_namec                 C   s   t |}|r|s|jdi t|j| |d}nd }|r|}n-t|dr*t|d}n"tt|dd dd d ur;|jj}nt	|rF| 
|j|S | ||}|sPJ |dr[|d d }t||rdd| }|dkrotjro|d	 }td
|s}td| d |S )N)auto_schemar   component_namerm  ref_name
SerializeriPatchedr   Requestz	^[\w.-]+$zComponent name "z" contains illegal characters. Only "A-Z a-z 0-9 - . _" are allowed. Furthermore, "-" and "." are discoursed due to potential tooling issues. This likely leads to an invalid schema.r   )r"   r   r  r4   r   r   r   rm  re  rA   rZ  r  rb  r:  rC   rM   r  r%  matchr   )r   r   r   r  r  custom_namer   r   r   r   rZ  5  s6   






zAutoSchema._get_serializer_namec                 C   s   t | t|}t|jd t| |||tj| ||d}|| jv r0| j| W  d    S | j	| | 
||||_|j pU|jddkoU|jd oUd|jv}|rh| j|= td d W  d    S |W  d    S 1 stw   Y  d S )N)r   r   r   r   r   r   r  )r(   r7   r   r   r%   rZ  r  r\  rf   registerr   r   r]   )r   r   r   r  r  discard_componentr   r   r   r   `  s2   

	$zAutoSchema.resolve_serializerr  )r   )Fr;  )r   )Nr   )Ir@  r?  __qualname__r$  r   r$   r   rU   r   r   rt   rV   r   r   r   r   rR   r   r   r   ry   rv   rw   r   rx   r   r|   r  r   rz   r   r   rP   r  ru   r}   r  r   r   rH  r   r   r;  rZ  r  r   r  r  r  r  r  rL  r  r  r   r  rQ   r   r8  r{   rA  rO   r~   r  rS  rY  r\  r   rf  rb  rZ  r%   r   r   r   r   r   rW   4   s    
?!	
C*"6:4

9  /

0,A"

6
3,&
]1	,rW   )tr2  r  r>  r%  collectionsr   typingr   r   r   r   r   r   django.corer   r=  r	   	django.dbr
   django.utils.translationr   r  rest_frameworkr   r   r   rest_framework.fieldsr   r   rest_framework.genericsr   r   r   rest_framework.mixinsr   !rest_framework.schemas.inspectorsr   rest_framework.schemas.utilsr   rest_framework.settingsr   rest_framework.utils.model_metar   rest_framework.viewsr   drf_spectacular.authenticationdrf_spectaculardrf_spectacular.serializersdrf_spectacular.contribdrf_spectacular.drainager   r   r   r   r   drf_spectacular.extensionsr    r!   r"   r#   drf_spectacular.plumbingr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   drf_spectacular.settingsrM   drf_spectacular.typesrN   drf_spectacular.utilsrO   rP   rQ   rR   rS   rT   rU   rV   rW   r   r   r   r   <module>   s<    (