o
    ).h;                     @   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)_metaconcrete_modelr   name)modelmeta r   `/var/www/supernova/superenvnew/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)selfr2   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)r2   prefix)key)r2   r0   patternr/   r   get_path_from_regexr   should_include_endpointget_allowed_methodsappendr   get_api_endpointsurl_patternsextendsortedr,   )r3   r2   r8   api_endpointsr:   
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)r3   rD   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   rI   
initkwargsendswith)r3   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   rN   r   r   r   rP      s    )hasattrsetrL   r   http_method_namesallowed_methods)r3   r   rL   rU   methodsr   r   r   r=      s   


z&EndpointEnumerator.get_allowed_methods)NN)Nr7   )	__name__
__module____qualname____doc__r6   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/)rK   r   SCHEMA_COERCE_PATH_PKcoerce_path_pkr2   r4   titledescriptionversionurl	endpoints)r3   r`   rc   ra   r2   r4   rb   r   r   r   r6      s   
zBaseSchemaGenerator.__init__c                 C   s,   | j d u r| | j| j}| | _ d S d S r   )rd   endpoint_inspector_clsr2   r4   r?   )r3   	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.
        )rd   create_viewcoerce_pathr>   )r3   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.
        rJ   r   NrL   rQ   metadata)r   r   argskwargsformat_kwargrj   
action_mapactionr'   lowerr
   )r3   r   r*   rj   rm   rL   r   r   r   rh      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)r3   r)   r*   rm   r   
field_namer   r   r   ri      s   
zBaseSchemaGenerator.coerce_pathFc                 C   s   t d)Nz0.get_schema() must be implemented in subclasses.)NotImplementedError)r3   rj   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)rj   check_permissionsr	   APIExceptionr   r   )r3   r)   r*   rm   r   r   r   has_view_permissions   s   
z(BaseSchemaGenerator.has_view_permissions)NNNNNNr   )NF)rX   rY   rZ   r-   re   r_   r6   rg   rn   rh   ri   r|   r   r   r   r   r   r\      s    


r\   )r[   rF   	importlibr   django.confr   django.contrib.admindocs.viewsr   django.core.exceptionsr   django.httpr   django.urlsr   r   rest_frameworkr	   rest_framework.requestr
   rest_framework.settingsr   rest_framework.utils.model_metar   r   r   r,   compilerH   r-   r\   r   r   r   r   <module>   s(    
]