a
    xds                     @   s  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	 d dl
mZmZmZ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mZ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& ddl'm(Z(m)Z) G dd de$Z*G dd de&Z+dS )    N)OrderedDict)Decimal)
attrgetter)urljoin)DecimalValidatorEmailValidatorMaxLengthValidatorMaxValueValidatorMinLengthValidatorMinValueValidatorRegexValidatorURLValidator)models)	force_str)RemovedInDRF314Warning
exceptions	renderersserializers)uritemplate)_UnvalidatedFieldempty)api_settings   )BaseSchemaGenerator)ViewInspector)get_pk_descriptionis_list_viewc                   @   s&   e Zd Zdd Zdd Zd	ddZdS )
SchemaGeneratorc                 C   s.   | j pd| jpdd}| jd ur*| j|d< |S )N )titleversiondescription)r   r    r!   )selfinfo r$   V/var/www/html/Ranjet/env/lib/python3.9/site-packages/rest_framework/schemas/openapi.pyget_info   s    

zSchemaGenerator.get_infoc                 C   s   i }|D ]t}|| D ]f}d|| | vr*q|| | d }||v rlt dj|| d || d |||d ||d||< qqd S )NoperationIdzYou have a duplicated operationId in your OpenAPI schema: {operation_id}
	Route: {route1}, Method: {method1}
	Route: {route2}, Method: {method2}
	An operationId has to be unique across your schema. Your schema may not work in other tools.routemethod)Zroute1Zmethod1Zroute2Zmethod2operation_id)r(   r)   )warningswarnformat)r"   pathsidsr(   r)   r*   r$   r$   r%   check_duplicate_operation_id)   s&    

z,SchemaGenerator.check_duplicate_operation_idNFc                 C   s   |    i }i }| |rdn|\}}|D ]\}}}	| |||	sDq*|	j||}
|	j||}| D ]0}||vrvqh|| || krqhtd	| qh|
| |dr|dd }t| jpd|}||i  |
|| | < q*| | d|  |d}t|dkrd|i|d	< |S )
z,
        Generate a OpenAPI schema.
        Nz@Schema component "{}" has been overriden with a different value./r   z3.0.2)Zopenapir#   r.   r   Zschemas
components)Z_initialise_endpointsZ_get_paths_and_endpointsZhas_view_permissionsschemaget_operationget_componentskeysr+   r,   r-   update
startswithr   url
setdefaultlowerr0   r&   len)r"   requestpublicZcomponents_schemasr.   _Zview_endpointspathr)   view	operationr2   kr3   r$   r$   r%   
get_schemaC   s<    


zSchemaGenerator.get_schema)NF)__name__
__module____qualname__r&   r0   rD   r$   r$   r$   r%   r      s   r   c                       sN  e Zd ZdP fdd	Zg Zg Zdddddd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)  Z*S )Q
AutoSchemaNc                    s>   |rt dd |D std|| _|| _|| _t   dS )a  
        :param operation_id_base: user-defined name in operationId. If empty, it will be deducted from the Model/Serializer/View name.
        :param component_name: user-defined component's name. If empty, it will be deducted from the Serializer's class name.
        c                 s   s   | ]}t |tV  qd S N
isinstancestr).0tagr$   r$   r%   	<genexpr>~       z&AutoSchema.__init__.<locals>.<genexpr>z'tags must be a list or tuple of string.N)all
ValueError_tagsoperation_id_basecomponent_namesuper__init__)r"   tagsrT   rU   	__class__r$   r%   rW   y   s    zAutoSchema.__init__retrievecreater7   ZpartialUpdatedestroy)getpostputpatchdeletec                 C   s   i }|  |||d< | |||d< g }|| ||7 }|| ||7 }|| ||7 }||d< | ||}|rx||d< | |||d< | |||d< |S )Nr'   r!   
parametersZrequestBody	responsesrX   )get_operation_idget_descriptionget_path_parametersget_pagination_parametersget_filter_parametersget_request_bodyget_responsesget_tags)r"   r@   r)   rB   rc   request_bodyr$   r$   r%   r4      s    zAutoSchema.get_operationc                 C   sP   | j dur| j S |jj}tdtj}|d|}|dkrLtd|jj|S )z
        Compute the component's name from the serializer.
        Raise an exception if the serializer's class name is "Serializer" (case-insensitive).
        N
serializerr   z"{}" is an invalid class name for schema generation. Serializer's class name should be unique and explicit. e.g. "ItemSerializer")	rU   rZ   rE   recompile
IGNORECASEsub	Exceptionr-   )r"   rn   rU   patternr$   r$   r%   get_component_name   s    
zAutoSchema.get_component_namec                 C   sH   |  dkri S | ||}t|tjs,i S | |}| |}||iS )zN
        Return components with their properties from the serializer.
        rb   )r;   get_serializerrK   r   
Serializerru   map_serializer)r"   r@   r)   rn   rU   contentr$   r$   r%   r5      s    

zAutoSchema.get_componentsc                 C   s.   | d}|d ddd |dd  D  S )Nr?   r   r   c                 s   s   | ]}|  V  qd S rI   )r   )rM   xr$   r$   r%   rO      rP   z,AutoSchema._to_camel_case.<locals>.<genexpr>r   )splitjoin)r"   Z	snake_strr2   r$   r$   r%   _to_camel_case   s    
zAutoSchema._to_camel_casec                 C   s   t t | jdddd}| jdur(| j}n|dur8|j}n| ||durp| ||jj}|dr|dd }nX| jjj}|dr|dd }n|dr|dd	 }|| r|dt|  }|d
kr|ds|d7 }|S )za
        Compute the base part for operation ID from the model, serializer or view name.
        querysetNmodelrw   iZAPIViewiZViewlists)	getattrrA   rT   rE   rv   rZ   endswithr   r<   )r"   r@   r)   actionr   namer$   r$   r%   get_operation_id_base   s&    




z AutoSchema.get_operation_id_basec                 C   s`   t | jd| }t||| jr&d}n$|| jvr<| |}n| j|  }| |||}|| S )z^
        Compute an operation ID from the view type and get_operation_id_base method.
        r   r   )r   rA   r;   r   method_mappingr}   r   )r"   r@   r)   method_namer   r   r$   r$   r%   re      s    
zAutoSchema.get_operation_idc           	   	   C   s   t sJ dtt| jdddd}g }t |D ]}d}|durz|j|}W n tyf   d}Y n0 |dur|jrt|j}n|dur|j	rt
||}|dd|dd	id
}|| q0|S )zL
        Return a list of parameters from templated path variables.
        z;`uritemplate` must be installed for OpenAPI schema support.r~   Nr   r   r@   Ttypestring)r   inrequiredr!   r3   )r   r   rA   	variables_meta	get_fieldrs   	help_textr   Zprimary_keyr   append)	r"   r@   r)   r   rc   variabler!   model_fieldZ	parameterr$   r$   r%   rg     s.    

	zAutoSchema.get_path_parametersc                 C   s8   |  ||sg S g }| jjD ]}|| | j7 }q|S rI   )allows_filtersrA   filter_backendsget_schema_operation_parameters)r"   r@   r)   rc   Zfilter_backendr$   r$   r%   ri   ,  s    z AutoSchema.get_filter_parametersc                 C   s:   t | jdddu rdS t| jdr.| jjdv S | dv S )z
        Determine whether to include filter Fields in schema.

        Default implementation looks for ModelViewSet or GenericAPIView
        actions/methods that cause filtering on the default implementation.
        r   NFr   )r   r[   r7   Zpartial_updater]   )r^   r`   ra   rb   )r   rA   hasattrr   r;   r"   r@   r)   r$   r$   r%   r   4  s
    zAutoSchema.allows_filtersc                 C   s0   | j }t|||sg S |  }|s&g S ||S rI   )rA   r   get_paginatorr   )r"   r@   r)   rA   	paginatorr$   r$   r%   rh   A  s    z$AutoSchema.get_pagination_parametersc                 C   s   t t|j}tdd |D r(d}nLtdd |D r@d}n4tdd |D rXd}ntdd |D rpd	}nd }d
|i}|r||d< |S )Nc                 s   s   | ]}t |tV  qd S rI   )rK   boolrM   choicer$   r$   r%   rO   O  rP   z-AutoSchema.map_choicefield.<locals>.<genexpr>booleanc                 s   s   | ]}t |tV  qd S rI   )rK   intr   r$   r$   r%   rO   Q  rP   integerc                 s   s   | ]}t |tttfV  qd S rI   )rK   r   floatr   r   r$   r$   r%   rO   S  rP   numberc                 s   s   | ]}t |tV  qd S rI   rJ   r   r$   r$   r%   rO   V  rP   r   enumr   )r   r   fromkeyschoicesrQ   )r"   fieldr   r   mappingr$   r$   r%   map_choicefieldM  s    
zAutoSchema.map_choicefieldc                 C   s
  t |tjrd| |jdS t |tjr@| |}d|d< |S t |tjr^d| |jdS t |tj	rt
|jdd }|d ur|jj}t |tjrddiS t |tjrd| |dS t |tjr| |S t |tjrdi d}t |jts| |j|d< |S t |tjr dd	d
S t |tjr8ddd
S t |tjrPddd
S t |tjrhddd
S t |tjrddd
S t |tjrddi}|jdkr|j|d< |S t |tjrDt
|dtjrddd
}nddi}|jrt d|jd d  d |d< |j!r4t"|j!d d |d< |d  |d< | #|| |S t |tj$rjddi}| #|| |S t |tj%rddi}| #|| t"|&dddkst"|&dddkrd|d< |S t |tj'rddd
S tj(d tj)dtj*dtj+di}d|&|j,diS )!Narrayr   itemsobjectr   r   r   r   r   date)r   r-   z	date-timeemailuriuuidZbothr-   coerce_to_stringdecimalr   .r   01
multipleOf9maximumminimumr   iint64binaryr   )-rK   r   ZListSerializerrx   childrw   ZManyRelatedField	map_fieldZchild_relationZPrimaryKeyRelatedFieldr   r~   r   pkr   Z	AutoFieldZMultipleChoiceFieldr   ZChoiceField	ListFieldr   Z	DateFieldZDateTimeFieldZ
EmailFieldZURLFieldZ	UUIDFieldZIPAddressFieldprotocolZDecimalFieldr   COERCE_DECIMAL_TO_STRINGdecimal_placesr   Zmax_whole_digitsr   _map_min_maxZ
FloatFieldZIntegerFieldr^   Z	FileFieldZBooleanFieldZ	JSONFieldZ	DictFieldZHStoreFieldrZ   )r"   r   datar   r   r   ry   ZFIELD_CLASS_SCHEMA_TYPEr$   r$   r%   r   i  s    




,zAutoSchema.map_fieldc                 C   s$   |j r|j |d< |jr |j|d< d S )Nr   r   )Z	max_valueZ	min_value)r"   r   ry   r$   r$   r%   r     s    
zAutoSchema._map_min_maxc                 C   s   g }i }|j  D ]}t|tjr$q|jr6||j | |}|j	rNd|d< |j
r\d|d< |jrjd|d< |jd ur|jtkrt|js|j|d< |jrt|j|d< | || |||j< qd|d}|r||d	< |S )
NTZreadOnlyZ	writeOnlyZnullabledefaultr!   r   )r   
propertiesr   )fieldsvaluesrK   r   ZHiddenFieldr   r   
field_namer   Z	read_onlyZ
write_onlyZ
allow_nullr   r   callabler   rL   map_field_validators)r"   rn   r   r   r   r3   resultr$   r$   r%   rx     s4    

zAutoSchema.map_serializerc                 C   sd  |j D ]V}t|trd|d< t|tr0d|d< t|trP|jjdd|d< qt|trzd}t|t	j
rnd}|j||< qt|trd	}t|t	j
rd
}|j||< qt|tr|j|d< qt|tr|j|d< qt|trt|dtjs|jrtd|jd d  d |d< |jr|j}|jdur<|jdkr<||j8 }t|d d |d< |d  |d< qdS )z&
        map field validators
        r   r-   r   z\Zz\zrt   Z	maxLengthZmaxItemsZ	minLengthZminItemsr   r   r   r   r   r   r   r   Nr   r   )Z
validatorsrK   r   r   r   regexrt   replacer   r   r   Zlimit_valuer
   r	   r   r   r   r   r   r   r   Z
max_digitsr   )r"   r   r3   v	attr_namedigitsr$   r$   r%   r   !  s@    








zAutoSchema.map_field_validatorsc                 C   s   t | jdd }|r| S d S )Npagination_class)r   rA   )r"   r   r$   r$   r%   r   I  s    zAutoSchema.get_paginatorc                 C   s   t ttd| jjS )N
media_type)r   mapr   rA   Zparser_classesr   r$   r$   r%   map_parsersO  s    zAutoSchema.map_parsersc                 C   s0   g }| j jD ]}t|tjrq||j q|S rI   )rA   Zrenderer_classes
issubclassr   ZBrowsableAPIRendererr   r   )r"   r@   r)   Zmedia_typesZrendererr$   r$   r%   map_renderersR  s    zAutoSchema.map_renderersc              
   C   sR   | j }t|dsd S z
| W S  tjyL   td|jj	|| Y d S 0 d S )Nrv   zt{}.get_serializer() raised an exception during schema generation. Serializer fields will not be generated for {} {}.)
rA   r   rv   r   ZAPIExceptionr+   r,   r-   rZ   rE   )r"   r@   r)   rA   r$   r$   r%   rv   [  s    


zAutoSchema.get_serializerc                 C   s   dd | |iS )Nz$refz#/components/schemas/{})r-   ru   r"   rn   r$   r$   r%   _get_referencej  s    zAutoSchema._get_referencec                    sZ   |dvri S |  ||| _| ||}t|tjs8i  n
| | d fdd| jD iS )N)PUTPATCHPOSTry   c                    s   i | ]}|d  iqS r3   r$   rM   ctitem_schemar$   r%   
<dictcomp>{  s   z/AutoSchema.get_request_body.<locals>.<dictcomp>)r   request_media_typesrv   rK   r   rw   r   )r"   r@   r)   rn   r$   r   r%   rj   m  s    
zAutoSchema.get_request_bodyc                    s   |dkrdddiiS |  ||| _| ||}t|tjs@i }n
| |}t||| jrzd|d | 	 }|r~|
  n| |dkrdnd	}| fd
d| jD ddiS )NDELETEZ204r!   r   r   r   r   Z201Z200c                    s   i | ]}|d  iqS r   r$   r   Zresponse_schemar$   r%   r     s   z,AutoSchema.get_responses.<locals>.<dictcomp>)ry   r!   )r   response_media_typesrv   rK   r   rw   r   r   rA   r   Zget_paginated_response_schema)r"   r@   r)   rn   r   r   status_coder$   r   r%   rk     s4    

zAutoSchema.get_responsesc                 C   s:   | j r| j S |dr"|dd  }|dd ddgS )Nr1   r   r   r?   -)rS   r8   r{   r   r   r$   r$   r%   rl     s
    
zAutoSchema.get_tagsc                 C   s   t jdtdd | ||S )NzwMethod `_get_path_parameters()` has been renamed to `get_path_parameters()`. The old name will be removed in DRF v3.14.   
stacklevel)r+   r,   r   rg   r   r$   r$   r%   _get_path_parameters  s
    zAutoSchema._get_path_parametersc                 C   s   t jdtdd | ||S )Nz{Method `_get_filter_parameters()` has been renamed to `get_filter_parameters()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   ri   r   r$   r$   r%   _get_filter_parameters  s
    z!AutoSchema._get_filter_parametersc                 C   s   t jdtdd | ||S )NzkMethod `_get_responses()` has been renamed to `get_responses()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   rk   r   r$   r$   r%   _get_responses  s
    zAutoSchema._get_responsesc                 C   s   t jdtdd | ||S )NzqMethod `_get_request_body()` has been renamed to `get_request_body()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   rj   r   r$   r$   r%   _get_request_body  s
    zAutoSchema._get_request_bodyc                 C   s   t jdtdd | ||S )NzmMethod `_get_serializer()` has been renamed to `get_serializer()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   rv   r   r$   r$   r%   _get_serializer  s
    zAutoSchema._get_serializerc                 C   s   t jdtdd |  S )NzkMethod `_get_paginator()` has been renamed to `get_paginator()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   r   )r"   r$   r$   r%   _get_paginator  s
    zAutoSchema._get_paginatorc                 C   s   t jdtdd | ||S )NzyMethod `_map_field_validators()` has been renamed to `map_field_validators()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   r   )r"   r   r3   r$   r$   r%   _map_field_validators  s
    z AutoSchema._map_field_validatorsc                 C   s   t jdtdd | |S )NzmMethod `_map_serializer()` has been renamed to `map_serializer()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   rx   r   r$   r$   r%   _map_serializer  s
    zAutoSchema._map_serializerc                 C   s   t jdtdd | |S )NzcMethod `_map_field()` has been renamed to `map_field()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   r   r"   r   r$   r$   r%   
_map_field  s
    zAutoSchema._map_fieldc                 C   s   t jdtdd | |S )NzoMethod `_map_choicefield()` has been renamed to `map_choicefield()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   r   r   r$   r$   r%   _map_choicefield  s
    zAutoSchema._map_choicefieldc                 C   s   t jdtdd | ||S )NzMethod `_get_pagination_parameters()` has been renamed to `get_pagination_parameters()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   rh   r   r$   r$   r%   _get_pagination_parameters  s
    z%AutoSchema._get_pagination_parametersc                 C   s   t jdtdd | ||S )NzmMethod `_allows_filters()` has been renamed to `allows_filters()`. The old name will be removed in DRF v3.14.r   r   )r+   r,   r   r   r   r$   r$   r%   _allows_filters  s
    zAutoSchema._allows_filters)NNN)+rE   rF   rG   rW   r   r   r   r4   ru   r5   r}   r   re   rg   ri   r   rh   r   r   r   rx   r   r   r   r   rv   r   rj   rk   rl   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r$   r$   rY   r%   rH   w   sZ   %$ $(	)rH   ),ro   r+   collectionsr   r   r   operatorr   urllib.parser   Zdjango.core.validatorsr   r   r   r	   r
   r   r   r   Z	django.dbr   Zdjango.utils.encodingr   Zrest_frameworkr   r   r   r   Zrest_framework.compatr   Zrest_framework.fieldsr   r   Zrest_framework.settingsr   
generatorsr   Z
inspectorsr   utilsr   r   r   rH   r$   r$   r$   r%   <module>   s"   (\