a
    còf;  ã                   @   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mZ dd	l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ƒ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)Ú
URLPatternÚURLResolver)Ú
exceptions)Úclone_request)Úapi_settings)Ú_get_pkc                 C   s   | j jj }t|ƒjS ©N)Ú_metaZconcrete_modelr   Úname)ÚmodelÚmeta© r   ú^/home/httpd/docs/test/DocsMgr/lib/python3.9/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)Z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   Z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 r4|d u rt j}t|tƒr*t|ƒ}n|}|j}|| _d S r   )r   ZROOT_URLCONFÚ
isinstanceÚstrr   ZurlpatternsÚpatterns)Úselfr-   ÚurlconfÚurlsr   r   r   Ú__init__9   s    

zEndpointEnumerator.__init__Ú c                 C   s¦   |du r| j }g }|D ]‚}|t|jƒ }t|tƒrt|  |¡}|j}|  ||¡r˜|  |¡D ]}|||f}	| 	|	¡ qXqt|t
ƒr| j|j|d�}
| |
¡ qt|td�S )zZ
        Return a list of all available API endpoints by inspecting the URL conf.
        N)r-   Úprefix)Úkey)r-   r,   Úpatternr+   r   Úget_path_from_regexr   Úshould_include_endpointÚget_allowed_methodsÚappendr   Úget_api_endpointsZurl_patternsÚextendÚsortedr)   )r.   r-   r3   Zapi_endpointsr5   Ú
path_regexr'   r   r(   r&   Z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)r.   r=   r'   r   r   r   r6   d   s    z&EndpointEnumerator.get_path_from_regexc                 C   sT   t |ƒsdS |jjdu rdS d|jv r8|jd du r8dS | d¡sL| d¡rPdS dS )zI
        Return `True` if the given endpoint should be included.
        FNÚschemaz	.{format}z
.{format}/T)r   r   rA   Ú
initkwargsÚendswith)r.   r'   r   r   r   r   r7   q   s    
z*EndpointEnumerator.should_include_endpointc                 C   sL   t |dƒr4t|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>‹   ó    z:EndpointEnumerator.get_allowed_methods.<locals>.<listcomp>c                 S   s   g | ]}|d vr|‘qS ))ÚOPTIONSÚHEADr   rF   r   r   r   rH   �   rI   )ÚhasattrÚsetrD   r   Úhttp_method_namesÚallowed_methods)r.   r   rD   rN   Úmethodsr   r   r   r8   „   s    


z&EndpointEnumerator.get_allowed_methods)NN)Nr2   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r:   r6   r7   r8   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ú/)rC   r   ZSCHEMA_COERCE_PATH_PKÚcoerce_path_pkr-   r/   ÚtitleÚdescriptionÚversionÚurlÚ	endpoints)r.   rX   r[   rY   r-   r/   rZ   r   r   r   r1   š   s    zBaseSchemaGenerator.__init__c                 C   s(   | j d u r$|  | j| j¡}| ¡ | _ d S r   )r\   Úendpoint_inspector_clsr-   r/   r:   )r.   Z	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.
        )r\   Úcreate_viewÚcoerce_pathr9   )r.   ÚrequestÚpathsZview_endpointsr'   r(   r   Úviewr   r   r   Ú_get_paths_and_endpoints­   s    
z,BaseSchemaGenerator._get_paths_and_endpointsc                 C   sŠ   |j f i t|di ƒ¤Ž}d|_i |_d|_d|_t|ddƒ|_t|ddƒ}|durr|dkrbd|_n| | 	¡ ¡|_|dur†t
||ƒ|_|S )zC
        Given a callback, return an actual view instance.
        rB   r   NrD   rJ   Úmetadata)r   r   ÚargsÚkwargsZformat_kwargra   Z
action_mapÚactionr%   Úlowerr
   )r.   r   r(   ra   rc   rD   r   r   r   r_   »   s    zBaseSchemaGenerator.create_viewc                 C   sH   | j rd|vr|S tt|ddƒddƒ}|r4t|ƒ}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}ZquerysetNr   Úidz{%s})rW   r   r   Úreplace)r.   r'   r(   rc   r   Ú
field_namer   r   r   r`   Ò   s    
zBaseSchemaGenerator.coerce_pathFc                 C   s   t dƒ‚d S )Nz0.get_schema() must be implemented in subclasses.)ÚNotImplementedError)r.   ra   Úpublicr   r   r   Ú
get_schemaá   s    zBaseSchemaGenerator.get_schemac              
   C   s@   |j du rdS z| |j ¡ W n tjttfy:   Y dS 0 dS )zY
        Return `True` if the incoming request has the correct view permissions.
        NTF)ra   Zcheck_permissionsr	   ZAPIExceptionr   r   )r.   r'   r(   rc   r   r   r   Úhas_view_permissionsä   s    
z(BaseSchemaGenerator.has_view_permissions)NNNNNN)N)NF)rQ   rR   rS   r*   r]   rW   r1   r^   rd   r_   r`   ro   rp   r   r   r   r   rU   ’   s   


rU   )rT   r>   Ú	importlibr   Zdjango.confr   Zdjango.contrib.admindocs.viewsr   Zdjango.core.exceptionsr   Zdjango.httpr   Zdjango.urlsr   r   Zrest_frameworkr	   Zrest_framework.requestr
   Zrest_framework.settingsr   Zrest_framework.utils.model_metar   r   r   r)   Úcompiler@   r*   rU   r   r   r   r   Ú<module>   s&   
ÿ]