o
    wvXjh  ã                   @   sÐ   d 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 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„ Zdd„ Zdd„ Ze d¡ZG dd„ dƒZG dd„ deƒZdS )z]
generators.py   # Top-down schema generation

See schemas.__init__.py for package overview.
é    N)Úimport_module)Úsettings)Úsimplify_regex)ÚPermissionDenied)ÚHttp404)Ú
exceptions)Ú
URLPatternÚURLResolverÚget_original_route)Úclone_request)Úapi_settings)Ú_get_pkc                 C   s   | j jj }t|ƒjS ©N)Ú_metaÚconcrete_modelr   Úname)ÚmodelÚmeta© r   ú^/var/www/html/myproject/venv/lib/python3.10/site-packages/rest_framework/schemas/generators.pyÚget_pk_name   s   

r   c                 C   s*   ddl m} t| ddƒ}|duot||ƒS )zT
    Return `True` if the given view callback is a REST framework view/viewset.
    r   )ÚAPIViewÚclsN)Úrest_framework.viewsr   ÚgetattrÚ
issubclass)Úcallbackr   r   r   r   r   Úis_api_view   s   r   c                 C   s(   | \}}}ddddddœ  |d¡}|fS )Nr   é   é   é   é   )ÚGETÚPOSTÚPUTÚPATCHÚDELETEé   )Úget)ÚendpointÚpathÚmethodr   Úmethod_priorityr   r   r   Úendpoint_ordering$   s   
ûúr-   z/<(?:(?P<converter>[^>:]+):)?(?P<parameter>\w+)>c                   @   s<   e Zd ZdZddd„Zddd„Zdd	„ Zd
d„ Zdd„ ZdS )ÚEndpointEnumeratorzR
    A class to determine the available API endpoints that a project exposes.
    Nc                 C   s>   |d u r|d u rt j}t|tƒrt|ƒ}n|}|j}|| _d S r   )r   ÚROOT_URLCONFÚ
isinstanceÚstrr   ÚurlpatternsÚpatterns)Úselfr3   ÚurlconfÚurlsr   r   r   Ú__init__9   s   


zEndpointEnumerator.__init__Ú c                 C   s¤   |du r| j }g }|D ]@}|t|ƒ }t|tƒr9|  |¡}|j}|  ||¡r8|  |¡D ]}|||f}	| |	¡ q+qt|t	ƒrK| j
|j|d�}
| |
¡ qt|td�S )zZ
        Return a list of all available API endpoints by inspecting the URL conf.
        N)r3   Úprefix)Úkey)r3   r
   r0   r   Úget_path_from_regexr   Úshould_include_endpointÚget_allowed_methodsÚappendr	   Úget_api_endpointsÚurl_patternsÚextendÚsortedr-   )r4   r3   r9   Úapi_endpointsÚpatternÚ
path_regexr*   r   r+   r)   Únested_endpointsr   r   r   r?   H   s*   


€
þ
€z$EndpointEnumerator.get_api_endpointsc                 C   s   t |ƒ}t td|¡S )zG
        Given a URL conf regex, return a URI template string.
        z{\g<parameter>})r   ÚreÚsubÚ_PATH_PARAMETER_COMPONENT_RE)r4   rE   r*   r   r   r   r;   d   s   z&EndpointEnumerator.get_path_from_regexc                 C   sT   t |ƒsdS |jjdu rdS d|jv r|jd du rdS | d¡s&| d¡r(dS dS )zI
        Return `True` if the given endpoint should be included.
        FNÚschemaz	.{format}z
.{format}/T)r   r   rJ   Ú
initkwargsÚendswith)r4   r*   r   r   r   r   r<   q   s   
z*EndpointEnumerator.should_include_endpointc                 C   sL   t |dƒrt|jƒ}t|jjƒ}dd„ ||@ D ƒ}n| ¡ j}dd„ |D ƒS )zL
        Return a list of the valid HTTP methods for this endpoint.
        Úactionsc                 S   s   g | ]}|  ¡ ‘qS r   )Úupper©Ú.0r+   r   r   r   Ú
<listcomp>‹   s    z:EndpointEnumerator.get_allowed_methods.<locals>.<listcomp>c                 S   s   g | ]}|d vr|‘qS ))ÚOPTIONSÚHEADr   rO   r   r   r   rQ   �   s    )ÚhasattrÚsetrM   r   Úhttp_method_namesÚallowed_methods)r4   r   rM   rV   Úmethodsr   r   r   r=   „   s   


z&EndpointEnumerator.get_allowed_methods)NN)Nr8   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r?   r;   r<   r=   r   r   r   r   r.   5   s    

r.   c                   @   sR   e Zd ZeZdZddd„Zdd„ Zdd„ Zddd	„Z	d
d„ Z
ddd„Zdd„ ZdS )ÚBaseSchemaGeneratorNc                 C   sL   |r|  d¡s|d7 }tj| _|| _|| _|| _|| _|| _|| _	d | _
d S )Nú/)rL   r   ÚSCHEMA_COERCE_PATH_PKÚcoerce_path_pkr3   r5   ÚtitleÚdescriptionÚversionÚurlÚ	endpoints)r4   ra   rd   rb   r3   r5   rc   r   r   r   r7   š   s   
zBaseSchemaGenerator.__init__c                 C   s,   | j d u r|  | j| j¡}| ¡ | _ d S d S r   )re   Úendpoint_inspector_clsr3   r5   r?   )r4   Ú	inspectorr   r   r   Ú_initialise_endpoints¨   s   
þz)BaseSchemaGenerator._initialise_endpointsc                 C   sX   g }g }| j D ] \}}}|  |||¡}|  |||¡}| |¡ | |||f¡ q||fS )zY
        Generate (path, method, view) given (path, method, callback) for paths.
        )re   Úcreate_viewÚcoerce_pathr>   )r4   ÚrequestÚpathsÚview_endpointsr*   r+   r   Úviewr   r   r   Ú_get_paths_and_endpoints­   s   
z,BaseSchemaGenerator._get_paths_and_endpointsc                 C   sŠ   |j di t|di ƒ¤Ž}d|_i |_d|_d|_t|ddƒ|_t|ddƒ}|dur9|dkr1d|_n| | 	¡ ¡|_|durCt
||ƒ|_|S )zC
        Given a callback, return an actual view instance.
        rK   r   NrM   rR   Úmetadata)r   r   ÚargsÚkwargsÚformat_kwargrk   Ú
action_mapÚactionr(   Úlowerr   )r4   r   r+   rk   rn   rM   r   r   r   ri   »   s   zBaseSchemaGenerator.create_viewc                 C   sH   | j rd|vr	|S tt|ddƒddƒ}|rt|ƒ}nd}| dd| ¡S )zâ
        Coerce {pk} path arguments into the name of the model field,
        where possible. This is cleaner for an external representation.
        (Ie. "this is an identifier", not "this is a database primary key")
        z{pk}ÚquerysetNr   Úidz{%s})r`   r   r   Úreplace)r4   r*   r+   rn   r   Ú
field_namer   r   r   rj   Ò   s   
zBaseSchemaGenerator.coerce_pathFc                 C   s   t dƒ‚)Nz0.get_schema() must be implemented in subclasses.)ÚNotImplementedError)r4   rk   Úpublicr   r   r   Ú
get_schemaá   s   zBaseSchemaGenerator.get_schemac              
   C   s>   |j du rdS z	| |j ¡ W dS  tjttfy   Y dS w )zY
        Return `True` if the incoming request has the correct view permissions.
        NTF)rk   Úcheck_permissionsr   ÚAPIExceptionr   r   )r4   r*   r+   rn   r   r   r   Úhas_view_permissionsä   s   
þÿz(BaseSchemaGenerator.has_view_permissions)NNNNNNr   )NF)rY   rZ   r[   r.   rf   r`   r7   rh   ro   ri   rj   r}   r€   r   r   r   r   r]   ’   s    


r]   ) r\   rG   Ú	importlibr   Údjango.confr   Údjango.contrib.admindocs.viewsr   Údjango.core.exceptionsr   Údjango.httpr   Úrest_frameworkr   Úrest_framework.compatr   r	   r
   Úrest_framework.requestr   Úrest_framework.settingsr   Úrest_framework.utils.model_metar   r   r   r-   ÚcompilerI   r.   Úobjectr]   r   r   r   r   Ú<module>   s(    
ÿ]