o
    "7hc6                     @   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   k/var/www/epreuve.sigeris.cm/public_html/epreuve/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                   @   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   s^   |d u r	|  |}| |rd| d| d}t|| j|||f t| dr-| `d S 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_prefix_viewsetr0   new_basenamer   r   	<genexpr>G   s    z3BaseRouter.is_already_registered.<locals>.<genexpr>)anyr$   )r&   r7   r   r6   r   r+   C   s   z BaseRouter.is_already_registeredc                 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*   I   s   zBaseRouter.get_default_basenamec                 C   r:   )zO
        Return a list of URL patterns, given the registered viewsets.
        zget_urls must be overriddenr<   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.urlsr#   )
__name__
__module____qualname__r'   r2   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updatepartial_updatedestroy)rG   putpatchdeletez{basename}-detailTInstancez.^{prefix}/{lookup}/{url_path}{trailing_slash}$c                    s   |rdnd| _ || _|rd| _d| _t| _n8d| _d| _t| _g }| jD ]&}|j}|d dkr5|d	d  }|d
 dkrA|d d
 }|	|j
|d q$|| _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&   rZ   use_regex_path_routesroute	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 )r;   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/   rh   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)r3   rd   r   r   r   
<listcomp>   s     z+SimpleRouter.get_routes.<locals>.<listcomp>c                    s   g | ]
}|j  v r|j qS r   )r@   r3   action)known_actionsr   r   rp      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   rq   r   r   r   rp          c                 S   s   g | ]}|j s|qS r   rt   rq   r   r   r   rp      ru   c                       g | ]}  |qS r   _get_dynamic_routerq   rd   r&   r   r   rp          c                    rv   r   rw   rq   ry   r   r   rp      rz   )
rE   r!   r_   get_extra_actionsr   joinrn   r   r   r,   )r&   r/   extra_actionsnot_allowedr1   detail_actionslist_actionsr_   r   )rs   rd   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   copyrL   kwargsr   r   r   r   r   r   r   url_namer   )r&   rd   rr   r   r   r   r   r   rx      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methodrr   r   r   r   get_method_map   s   
zSimpleRouter.get_method_maprT   c                 C   sZ   t |dd}t |ddp|}d}| jst |dd}|du r$t |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_kwargNlookup_value_converterlookup_value_regex)lookup_prefixr   lookup_value)ri   r[   r]   r\   format)r&   r/   r   r   r   r   r   r   r   get_lookup_regex   s   zSimpleRouter.get_lookup_regexc              	   C   s   g }| j D ]u\}}}| |}| |}|D ]c}| ||j}|s"q|jj||| jd}	|sP| jt	u r@|	d dkr?|	dd }	n|	dd dkrPd|	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.   lookuprZ   r   rS   rW   N   z^/rV   )r0   r   r0   r   )r$   r   r   r   r   r   r   rZ   r^   r   r   r   rL   r   as_viewr   r,   )r&   retr.   r/   r0   r   r_   rd   r   regexr   viewr   r   r   r   r>   
  s<   




#zSimpleRouter.get_urls)TT)rT   )r@   rA   rB   r   r   r_   r'   r*   r   rx   r   r   r>   __classcell__r   r   rf   r   rD   ]   sR    +$
rD   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   sj   i }|j j}| j D ]%\}}|r|d | }zt|||||dd||< W q ty0   Y qw t|S )N:r   )argsr   requestr   )resolver_match	namespaceapi_root_dictr   r	   rG   r   r   )r&   r   r   r   r   r   keyr   r   r   r   rG   B  s"   zAPIRootView.get)r@   rA   rB   __doc___ignore_model_permissionsschemar   rG   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   rE   r   DEFAULT_RENDERER_CLASSESra   r'   )r&   r   r   rf   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   	list_namer.   r/   r0   r   r   r   get_api_root_viewl  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   rT   r   )	ra   r>   include_root_viewr   r   root_view_namer,   include_format_suffixesr   )r&   r?   r   root_urlrf   r   r   r>   w  s   

zDefaultRouter.get_urlsr#   )r@   rA   rB   r   r   r   r   default_schema_renderersr   r   APISchemaViewr
   r'   r   r>   r   r   r   rf   r   r   X  s    
r   )!r   r   collectionsr   django.core.exceptionsr   django.urlsr   r   r   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"   rD   APIViewr   r   r   r   r   r   <module>   s*    - ^