a
    còfc6  ã                   @   sü   d Z ddl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	lmZ dd
lmZ ddlmZ ddlmZ edg d¢ƒZedg d¢ƒZdd„ Zdd„ ZG dd„ dƒZG dd„ deƒZG dd„ dejƒZG dd„ deƒZ dS )aÜ  
Routers provide a convenient and consistent way of automatically
determining the URL conf for your API.

They are used by simply instantiating a Router class, and then registering
all the required ViewSets with that router.

For example, you might have a `urls.py` that looks something like this:

    router = routers.DefaultRouter()
    router.register('users', UserViewSet, 'user')
    router.register('accounts', AccountViewSet, 'account')

    urlpatterns = router.urls
é    N)Ú
namedtuple)ÚImproperlyConfigured)ÚNoReverseMatchÚpathÚre_path)Úviews)ÚResponse)Úreverse)ÚSchemaGenerator)Ú
SchemaView)Úapi_settings)Úformat_suffix_patternsÚRoute©ÚurlÚmappingÚnameÚdetailÚ
initkwargsÚDynamicRoute©r   r   r   r   c                 C   s   |   dd¡  dd¡S )zK
    Double brackets in regex of url_path for escape string formatting
    Ú{z{{Ú}z}})Úreplace)Úurl_path© r   úS/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/rest_framework/routers.pyÚescape_curly_brackets"   s    r   c                 C   s
   t j| Ž S )zX
    Takes an iterable of iterables, returns a single iterable containing all items
    )Ú	itertoolsÚchain)Zlist_of_listsr   r   r   Úflatten)   s    r    c                   @   sB   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zedd„ ƒZ	dS )Ú
BaseRouterc                 C   s
   g | _ d S ©N)Úregistry©Úselfr   r   r   Ú__init__1   s    zBaseRouter.__init__Nc                 C   sZ   |d u r|   |¡}|  |¡r6d|› d|› d�}t|ƒ‚| j |||f¡ t| dƒrV| `d S )NzRouter with basename "zG" is already registered. Please provide a unique basename for viewset "ú"Ú_urls)Úget_default_basenameÚis_already_registeredr   r#   ÚappendÚhasattrr(   )r%   ÚprefixÚviewsetÚbasenameÚmsgr   r   r   Úregister4   s    

ÿ
zBaseRouter.registerc                    s   t ‡ fdd„| jD ƒƒS )z;
        Check if `basename` is already registered
        c                 3   s   | ]\}}}|ˆ kV  qd S r"   r   )Ú.0Ú_prefixZ_viewsetr/   ©Únew_basenamer   r   Ú	<genexpr>G   ó    z3BaseRouter.is_already_registered.<locals>.<genexpr>)Úanyr#   )r%   r5   r   r4   r   r*   C   s    z BaseRouter.is_already_registeredc                 C   s   t dƒ‚dS )úq
        If `basename` is not specified, attempt to automatically determine
        it from the viewset.
        z'get_default_basename must be overriddenN©ÚNotImplementedError)r%   r.   r   r   r   r)   I   s    zBaseRouter.get_default_basenamec                 C   s   t dƒ‚dS )zO
        Return a list of URL patterns, given the registered viewsets.
        zget_urls must be overriddenNr:   r$   r   r   r   Úget_urlsP   s    zBaseRouter.get_urlsc                 C   s   t | dƒs|  ¡ | _| jS )Nr(   )r,   r<   r(   r$   r   r   r   ÚurlsV   s    

zBaseRouter.urls)N)
Ú__name__Ú
__module__Ú__qualname__r&   r1   r*   r)   r<   Úpropertyr=   r   r   r   r   r!   0   s   
r!   c                	       s¨   e Zd Zeddddœddddid	�ed
ddi d�eddddddœddddid	�edddi d�gZd&‡ fdd„	Zdd„ Zdd„ Zdd„ Z	dd „ Z
d'd"d#„Zd$d%„ Z‡  ZS )(ÚSimpleRouterz^{prefix}{trailing_slash}$ÚlistÚcreate)ÚgetÚpostz{basename}-listFÚsuffixÚListr   z%^{prefix}/{url_path}{trailing_slash}$z{basename}-{url_name}r   z#^{prefix}/{lookup}{trailing_slash}$ÚretrieveÚupdateZpartial_updateÚdestroy)rE   ÚputÚpatchÚdeletez{basename}-detailTÚInstancez.^{prefix}/{lookup}/{url_path}{trailing_slash}$c                    sª   |rdnd| _ || _|r,d| _d| _t| _npd| _d| _t| _g }| jD ]L}|j}|d dkrj|d	d … }|d
 dkr‚|d d
… }| 	|j
|d�¡ qH|| _tƒ  ¡  d S )Nú/Ú z5(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})z[^/.]+z2<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>Ústrr   ú^é   éÿÿÿÿú$)r   )Útrailing_slashÚ
_use_regexÚ_base_patternÚ_default_value_patternr   Ú	_url_confr   Úroutesr   r+   Ú_replaceÚsuperr&   )r%   rW   Zuse_regex_pathZ_routesÚrouteZ	url_param©Ú	__class__r   r   r&   Š   s&    
zSimpleRouter.__init__c                 C   s*   t |ddƒ}|dusJ dƒ‚|jjj ¡ S )r9   ÚquerysetNz�`basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.)ÚgetattrÚmodelÚ_metaZobject_nameÚlower)r%   r.   rb   r   r   r   r)   £   s    z!SimpleRouter.get_default_basenamec                    sÚ   t tdd„ ˆjD ƒƒƒ‰ | ¡ }‡ fdd„|D ƒ}|rLd}t|d |¡ ƒ‚dd„ |D ƒ}dd„ |D ƒ}g }ˆjD ]b‰tˆtƒr ˆjr |‡‡fdd„|D ƒ7 }qrtˆtƒrÊˆjsÊ|‡‡fd	d„|D ƒ7 }qr| 	ˆ¡ qr|S )
z
        Augment `self.routes` with any dynamically generated routes.

        Returns a list of the Route namedtuple.
        c                 S   s    g | ]}t |tƒr|j ¡ ‘qS r   )Ú
isinstancer   r   Úvalues)r2   r_   r   r   r   Ú
<listcomp>¸   r7   z+SimpleRouter.get_routes.<locals>.<listcomp>c                    s   g | ]}|j ˆ v r|j ‘qS r   )r>   ©r2   Úaction)Úknown_actionsr   r   ri   ¼   s   
ÿzZCannot use the @action decorator on the following methods, as they are existing routes: %sz, c                 S   s   g | ]}|j r|‘qS r   ©r   rj   r   r   r   ri   Æ   r7   c                 S   s   g | ]}|j s|‘qS r   rm   rj   r   r   r   ri   Ç   r7   c                    s   g | ]}ˆ  ˆ |¡‘qS r   ©Ú_get_dynamic_routerj   ©r_   r%   r   r   ri   Ì   r7   c                    s   g | ]}ˆ  ˆ |¡‘qS r   rn   rj   rp   r   r   ri   Î   r7   )
rC   r    r\   Zget_extra_actionsr   Újoinrg   r   r   r+   )r%   r.   Zextra_actionsÚnot_allowedr0   Zdetail_actionsZlist_actionsr\   r   )rl   r_   r%   r   Ú
get_routes°   s$    
ÿ
zSimpleRouter.get_routesc                 C   sL   |j  ¡ }| |j¡ t|jƒ}t|j d|¡|j	|j
 d|j¡|j|d�S )Nz
{url_path}z
{url_name}r   )r   ÚcopyrJ   Úkwargsr   r   r   r   r   r   r   Úurl_namer   )r%   r_   rk   r   r   r   r   r   ro   Ô   s    

ûzSimpleRouter._get_dynamic_routec                 C   s,   i }|  ¡ D ]\}}t||ƒr|||< q|S )zÀ
        Given a viewset, and a mapping of http methods to actions,
        return a new mapping which only includes any mappings that
        are actually implemented by the viewset.
        )Úitemsr,   )r%   r.   Z
method_mapZbound_methodsÚmethodrk   r   r   r   Úget_method_mapâ   s
    

zSimpleRouter.get_method_maprQ   c                 C   sZ   t |ddƒ}t |ddƒp|}d}| js2t |ddƒ}|du rHt |d| jƒ}| jj|||d�S )a�  
        Given a viewset, return the portion of URL regex that is used
        to match against a single instance.

        Note that lookup_prefix is not used directly inside REST rest_framework
        itself, but is required in order to nicely support nested router
        implementations, such as drf-nested-routers.

        https://github.com/alanjds/drf-nested-routers
        Úlookup_fieldÚpkÚlookup_url_kwargNZlookup_value_converterZlookup_value_regex)Úlookup_prefixr|   Úlookup_value)rc   rX   rZ   rY   Úformat)r%   r.   r}   rz   r|   r~   r   r   r   Úget_lookup_regexî   s    ýzSimpleRouter.get_lookup_regexc              	   C   sú   g }| j D ]ê\}}}|  |¡}|  |¡}|D ]Æ}|  ||j¡}|sDq,|jj||| jd�}	|s | jt	u r€|	d dkr |	dd… }	n |	dd… dkr d|	dd…  }	|j
 ¡ }
|
 ||jd	œ¡ |j|fi |
¤Ž}|jj|d
�}| | j|	||d�¡ q,q
|S )zQ
        Use the registered viewsets to generate a list of URL patterns.
        )r-   ÚlookuprW   r   rP   rT   Né   z^/rS   )r/   r   ©r/   ©r   )r#   r€   rs   ry   r   r   r   rW   r[   r   r   rt   rJ   r   Úas_viewr   r+   )r%   Úretr-   r.   r/   r�   r\   r_   r   Úregexr   Úviewr   r   r   r   r<   
  s8    

ý


þzSimpleRouter.get_urls)TT)rQ   )r>   r?   r@   r   r   r\   r&   r)   rs   ro   ry   r€   r<   Ú__classcell__r   r   r`   r   rB   ]   sP   þøüüöüÝ+$
rB   c                   @   s$   e Zd ZdZdZdZdZdd„ ZdS )ÚAPIRootViewz7
    The default basic root view for DefaultRouter
    TNc              	   O   sp   i }|j j}| j ¡ D ]P\}}|r.|d | }z t||||| d¡d�||< W q tyd   Y qY q0 qt|ƒS )Nú:r   )Úargsru   Úrequestr   )Zresolver_matchÚ	namespaceÚapi_root_dictrw   r	   rE   r   r   )r%   r�   rŒ   ru   r†   rŽ   Úkeyrv   r   r   r   rE   B  s     ûzAPIRootView.get)r>   r?   r@   Ú__doc__Z_ignore_model_permissionsZschemar�   rE   r   r   r   r   rŠ   :  s
   rŠ   c                       sR   e Zd ZdZdZdZdZdZeZe	Z
eZ‡ fdd„Zddd„Z‡ fd	d
„Z‡  ZS )ÚDefaultRouterz‘
    The default router extends the SimpleRouter, but also adds in a default
    API root view, and adds format suffix patterns to the URLs.
    Tzapi-rootNc                    s8   d|v r|  d¡| _nttjƒ| _tƒ j|i |¤Ž d S )NÚroot_renderers)Úpopr“   rC   r   ZDEFAULT_RENDERER_CLASSESr^   r&   )r%   rŒ   ru   r`   r   r   r&   e  s    zDefaultRouter.__init__c                 C   s@   i }| j d j}| jD ]\}}}|j|d�||< q| jj|d�S )z+
        Return a basic root view.
        r   rƒ   )r�   )r\   r   r#   r   rŠ   r…   )r%   Úapi_urlsr�   Z	list_namer-   r.   r/   r   r   r   Úget_api_root_viewl  s
    zDefaultRouter.get_api_root_viewc                    sH   t ƒ  ¡ }| jr6| j|d�}td|| jd�}| |¡ | jrDt|ƒ}|S )z”
        Generate the list of URL patterns, including a default root view
        for the API, and appending `.json` style format suffixes.
        )r•   rQ   r„   )	r^   r<   Úinclude_root_viewr–   r   Úroot_view_namer+   Úinclude_format_suffixesr   )r%   r=   rˆ   Zroot_urlr`   r   r   r<   w  s    

zDefaultRouter.get_urls)N)r>   r?   r@   r‘   r—   r™   r˜   Zdefault_schema_renderersrŠ   r   ZAPISchemaViewr
   r&   r–   r<   r‰   r   r   r`   r   r’   X  s   
r’   )!r‘   r   Úcollectionsr   Zdjango.core.exceptionsr   Zdjango.urlsr   r   r   Zrest_frameworkr   Zrest_framework.responser   Zrest_framework.reverser	   Zrest_framework.schemasr
   Zrest_framework.schemas.viewsr   Zrest_framework.settingsr   Zrest_framework.urlpatternsr   r   r   r   r    r!   rB   ZAPIViewrŠ   r’   r   r   r   r   Ú<module>   s(   - ^