U
    Ù;{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   úE/tmp/pip-unpacked-wheel-1rosx3yy/rest_framework/schemas/generators.pyÚget_pk_name   s    
r   c                 C   s*   ddl m} t| ddƒ}|dk	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 kr4|d k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k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,   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>})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krdS d|jkr8|jd dk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   r@   Ú
initkwargsÚendswith)r-   r&   r   r   r   r   r6   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>‹   s     z:EndpointEnumerator.get_allowed_methods.<locals>.<listcomp>c                 S   s   g | ]}|d kr|‘qS ))ÚOPTIONSÚHEADr   rE   r   r   r   rG   �   s      )ÚhasattrÚsetrC   r   Úhttp_method_namesZ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-   rU   rX   rV   r,   r.   rW   r   r   r   r0   š   s    zBaseSchemaGenerator.__init__c                 C   s(   | j d kr$|  | j| j¡}| ¡ | _ d S r   )rY   Ú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.
        )rY   Ú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 f t|di ƒŽ}d|_i |_d|_d|_t|ddƒ|_t|ddƒ}|dk	rn|dkr^d|_n| | 	¡ ¡|_|dk	r‚t
||ƒ|_|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^   r`   rC   r   r   r   r\   »   s    zBaseSchemaGenerator.create_viewc                 C   sH   | j rd|k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})rT   r   r   Úreplace)r-   r&   r'   r`   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-   r^   Úpublicr   r   r   Ú
get_schemaá   s    zBaseSchemaGenerator.get_schemac              
   C   sB   |j dkrdS z| |j ¡ W n tjttfk
r<   Y dS X dS )zY
        Return `True` if the incoming request has the correct view permissions.
        NTF)r^   Zcheck_permissionsr	   ZAPIExceptionr   r   )r-   r&   r'   r`   r   r   r   Úhas_view_permissionsä   s    
z(BaseSchemaGenerator.has_view_permissions)NNNNNN)N)NF)rN   rO   rP   r)   rZ   rT   r0   r[   ra   r\   r]   rl   rm   r   r   r   r   rR   ’   s   


rR   )rQ   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)   rR   r   r   r   r   Ú<module>   s&   
ÿ]