o
    wvXjÉ/  ã                   @   s  d Z ddl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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)ÚOrderedDictÚ
namedtuple)Úurl)ÚImproperlyConfigured)ÚNoReverseMatch)Úviews)ÚResponse)Úreverse)ÚSchemaGenerator)Ú
SchemaView)Úapi_settings)Úformat_suffix_patternsÚRoute©r   Ú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/var/www/html/myproject/venv/lib/python3.10/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)Úlist_of_listsr   r   r   Úflatten*   s   
r    c                   @   s:   e Zd Zdd„ Zd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__2   s   
zBaseRouter.__init__Nc                 C   s:   |d u r	|   |¡}| j |||f¡ t| dƒr| `d S d S ©NÚ_urls)Úget_default_basenamer#   ÚappendÚhasattrr(   )r%   ÚprefixÚviewsetÚbasenamer   r   r   Úregister5   s   

ÿzBaseRouter.registerc                 C   ó   t dƒ‚)úq
        If `basename` is not specified, attempt to automatically determine
        it from the viewset.
        z'get_default_basename must be overridden©ÚNotImplementedError)r%   r-   r   r   r   r)   >   s   zBaseRouter.get_default_basenamec                 C   r0   )zO
        Return a list of URL patterns, given the registered viewsets.
        zget_urls must be overriddenr2   r$   r   r   r   Úget_urlsE   s   zBaseRouter.get_urlsc                 C   s   t | dƒs
|  ¡ | _| jS r'   )r+   r4   r(   r$   r   r   r   ÚurlsK   s   

zBaseRouter.urlsr"   )	Ú__name__Ú
__module__Ú__qualname__r&   r/   r)   r4   Úpropertyr5   r   r   r   r   r!   1   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ÚupdateÚpartial_updateÚdestroy)r=   ÚputÚpatchÚdeletez{basename}-detailTÚInstancez.^{prefix}/{lookup}/{url_path}{trailing_slash}$c                    s   |rdnd| _ tƒ  ¡  d S )Nú/Ú )Útrailing_slashÚsuperr&   )r%   rK   ©Ú	__class__r   r   r&      s   zSimpleRouter.__init__c                 C   s*   t |ddƒ}|dusJ dƒ‚|jjj ¡ S )r1   Ú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Ú_metaÚobject_nameÚlower)r%   r-   rO   r   r   r   r)   ƒ   s   z!SimpleRouter.get_default_basenamec                    sÚ   t tdd„ ˆjD ƒƒƒ‰ | ¡ }‡ fdd„|D ƒ}|r&d}t|d |¡ ƒ‚dd„ |D ƒ}dd„ |D ƒ}g }ˆjD ]1‰tˆtƒrPˆjrP|‡‡fdd„|D ƒ7 }q9tˆtƒreˆjse|‡‡fd	d„|D ƒ7 }q9| 	ˆ¡ q9|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)Ú.0Úrouter   r   r   Ú
<listcomp>˜   s     z+SimpleRouter.get_routes.<locals>.<listcomp>c                    s   g | ]
}|j ˆ v r|j ‘qS r   )r6   ©rW   Úaction)Úknown_actionsr   r   rY   œ   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   rZ   r   r   r   rY   ¦   ó    c                 S   s   g | ]}|j s|‘qS r   r]   rZ   r   r   r   rY   §   r^   c                    ó   g | ]}ˆ  ˆ |¡‘qS r   ©Ú_get_dynamic_routerZ   ©rX   r%   r   r   rY   ¬   ó    c                    r_   r   r`   rZ   rb   r   r   rY   ®   rc   )
r;   r    ÚroutesÚget_extra_actionsr   ÚjoinrU   r   r   r*   )r%   r-   Úextra_actionsÚnot_allowedÚmsgÚdetail_actionsÚlist_actionsrd   r   )r\   rX   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   ÚcopyrB   Úkwargsr   r   r   r   r   r   r   Úurl_namer   )r%   rX   r[   r   r   r   r   r   ra   ´   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-   Ú
method_mapÚbound_methodsÚmethodr[   r   r   r   Úget_method_mapÂ   s   
€zSimpleRouter.get_method_maprJ   c                 C   s<   d}t |ddƒ}t |ddƒp|}t |ddƒ}|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
        z5(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})Úlookup_fieldÚpkÚlookup_url_kwargNÚlookup_value_regexz[^/.]+)Úlookup_prefixrw   Úlookup_value)rP   Úformat)r%   r-   ry   Ú
base_regexru   rw   rz   r   r   r   Úget_lookup_regexÎ   s   ýzSimpleRouter.get_lookup_regexc              	   C   sÔ   g }| j D ]b\}}}|  |¡}|  |¡}|D ]P}|  ||j¡}|s"q|jj||| jd�}	|s>|	dd… dkr>d|	dd…  }	|j 	¡ }
|
 
||jdœ¡ |j|fi |
¤Ž}|jj|d�}| t|	||d�¡ qq|S )	zQ
        Use the registered viewsets to generate a list of URL patterns.
        )r,   ÚlookuprK   Né   z^/ú^)r.   r   ©r.   ©r   )r#   r}   rl   rt   r   r   r{   rK   r   rm   rB   r   Úas_viewr   r*   )r%   Úretr,   r-   r.   r~   rd   rX   r   Úregexr   Úviewr   r   r   r   r4   å   s2   

ý

þãzSimpleRouter.get_urls)T)rJ   )r6   r7   r8   r   r   rd   r&   r)   rl   ra   rt   r}   r4   Ú__classcell__r   r   rM   r   r:   R   sR    þøüüöüÝ+$
r:   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   sn   t ƒ }|jj}| j ¡ D ]&\}}|r|d | }zt||||| dd ¡d�||< W q ty2   Y qw t|ƒS )Nú:r{   )Úargsrn   Úrequestr{   )	r   Úresolver_matchÚ	namespaceÚapi_root_dictrp   r	   r=   r   r   )r%   r‹   rŠ   rn   r„   r�   Úkeyro   r   r   r   r=     s"   
ûþzAPIRootView.get)r6   r7   r8   Ú__doc__Ú_ignore_model_permissionsÚschemarŽ   r=   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”   r;   r   ÚDEFAULT_RENDERER_CLASSESrL   r&   )r%   rŠ   rn   rM   r   r   r&   <  s   zDefaultRouter.__init__c                 C   sB   t ƒ }| jd j}| jD ]\}}}|j|d�||< q| jj|d�S )z+
        Return a basic root view.
        r   r�   )rŽ   )r   rd   r   r#   r{   rˆ   rƒ   )r%   Úapi_urlsrŽ   Ú	list_namer,   r-   r.   r   r   r   Úget_api_root_viewC  s
   zDefaultRouter.get_api_root_viewc                    sH   t ƒ  ¡ }| jr| j|d�}td|| jd�}| |¡ | jr"t|ƒ}|S )z”
        Generate the list of URL patterns, including a default root view
        for the API, and appending `.json` style format suffixes.
        )r—   z^$r‚   )	rL   r4   Úinclude_root_viewr™   r   Úroot_view_namer*   Úinclude_format_suffixesr   )r%   r5   r†   Úroot_urlrM   r   r   r4   N  s   

zDefaultRouter.get_urlsr"   )r6   r7   r8   r�   rš   rœ   r›   Údefault_schema_renderersrˆ   r   ÚAPISchemaViewr
   r&   r™   r4   r‡   r   r   rM   r   r“   /  s    
r“   )"r�   r   Úcollectionsr   r   Údjango.conf.urlsr   Údjango.core.exceptionsr   Údjango.urlsr   Úrest_frameworkr   Úrest_framework.responser   Úrest_framework.reverser	   Úrest_framework.schemasr
   Úrest_framework.schemas.viewsr   Úrest_framework.settingsr   Úrest_framework.urlpatternsr   r   r   r   r    r!   r:   ÚAPIViewrˆ   r“   r   r   r   r   Ú<module>   s,    ! @