o
    K Ži;  ã                   @   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)Z_metaZconcrete_modelr   Úname)ÚmodelÚmeta© r   úkC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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 r|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 ]A}|t|jƒ }t|tƒr:|  |¡}|j}|  ||¡r9|  |¡D ]}|||f}	| 	|	¡ q,qt|t
ƒrL| 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,   r2   Zapi_endpointsr4   Ú
path_regexr&   r   r'   r%   Znested_endpointsr   r   r   r9   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>}N)r   ÚreÚsubÚ_PATH_PARAMETER_COMPONENT_RE)r-   r<   r&   r   r   r   r5   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   r@   Ú
initkwargsÚendswith)r-   r&   r   r   r   r   r6   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   rE   r   r   r   rG   �   s    N)ÚhasattrÚsetrC   r   Úhttp_method_namesÚallowed_methods)r-   r   rC   rL   Úmethodsr   r   r   r7   „   s   


z&EndpointEnumerator.get_allowed_methods)NN)Nr1   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r9   r5   r6   r7   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ú/)rB   r   ZSCHEMA_COERCE_PATH_PKÚcoerce_path_pkr,   r.   ÚtitleÚdescriptionÚversionÚurlÚ	endpoints)r-   rV   rY   rW   r,   r.   rX   r   r   r   r0   š   s   
zBaseSchemaGenerator.__init__c                 C   s,   | j d u r|  | j| j¡}| ¡ | _ d S d S r   )rZ   Úendpoint_inspector_clsr,   r.   r9   )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.
        N)rZ   Úcreate_viewÚcoerce_pathr8   )r-   ÚrequestÚpathsZ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.
        rA   r   NrC   rH   Úmetadata)r   r   ÚargsÚkwargsZformat_kwargr_   Z
action_mapÚactionr$   Úlowerr
   )r-   r   r'   r_   ra   rC   r   r   r   r]   »   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}ZquerysetNr   Úidz{%s})rU   r   r   Úreplace)r-   r&   r'   ra   r   Ú
field_namer   r   r   r^   Ò   s   
zBaseSchemaGenerator.coerce_pathFc                 C   s   t dƒ‚)Nz0.get_schema() must be implemented in subclasses.)ÚNotImplementedError)r-   r_   Ú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)r_   Zcheck_permissionsr	   ZAPIExceptionr   r   )r-   r&   r'   ra   r   r   r   Úhas_view_permissionsä   s   
þÿz(BaseSchemaGenerator.has_view_permissions)NNNNNNr   )NF)rO   rP   rQ   r)   r[   rU   r0   r\   rb   r]   r^   rm   rn   r   r   r   r   rS   ’   s    


rS   )rR   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)   rS   r   r   r   r   Ú<module>   s(    
ÿ]