o
    "7hL$                  	   @   s   d 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mZmZ dd	lmZ dd
lmZ dd Zdd ZG dd dZG dd deejZG dd deejZG dd dejejeZG dd dej ejej!ej"ejeZ#dS )a  
ViewSets are essentially just a type of class based view, that doesn't provide
any method handlers, such as `get()`, `post()`, etc... but instead has actions,
such as `list()`, `retrieve()`, `create()`, etc...

Actions are only bound to methods at the point of instantiating the views.

    user_list = UserViewSet.as_view({'get': 'list'})
    user_detail = UserViewSet.as_view({'get': 'retrieve'})

Typically, rather than instantiate views from viewsets directly, you'll
register the viewset with a router and let the URL conf be determined
automatically.

    router = DefaultRouter()
    router.register(r'users', UserViewSet, 'user')
    urlpatterns = router.urls
    )update_wrapper)
getmembers)VERSION)NoReverseMatch)classonlymethod)csrf_exempt)genericsmixinsviews)MethodMapper)reversec                 C   s   t | do
t| jtS )Nmapping)hasattr
isinstancer   r   )attr r   l/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/rest_framework/viewsets.py_is_extra_action    s   r   c                 C   s    | j |ksJ dj| |d| S )NzExpected function (`{func.__name__}`) to match its attribute name (`{name}`). If using a decorator, ensure the inner function is decorated with `functools.wraps`, or that `{func.__name__}.__name__` is otherwise set to `{name}`.funcname)__name__formatr   r   r   r   _check_attr_name$   s
   r   c                       sJ   e Zd ZdZedddZ fddZdd Zed	d
 Z	dd Z
  ZS )ViewSetMixinad  
    This is the magic.

    Overrides `.as_view()` so that it takes an `actions` keyword that performs
    the binding of HTTP methods to actions on the Resource.

    For example, to create a concrete view binding the 'GET' and 'POST' methods
    to the 'list' and 'create' actions...

    view = MyViewSet.as_view({'get': 'list', 'post': 'create'})
    Nc                    s   d_ d_d_d_d_ stdD ]}|jv r'td|jf t|s5tdj|f qdv rEdv rEtdj  fdd	}t	|d
d t	|j
d
d |_|_ |_tdkrld|_t|S )z
        Because of the way class based views create a closure around the
        instantiated view, we need to totally reimplement `.as_view`,
        and slightly modify the view function that is created and returned.
        NzwThe `actions` argument must be provided when calling `.as_view()` on a ViewSet. For example `.as_view({'get': 'list'})`zUYou tried to pass in the %s method name as a keyword argument to %s(). Don't do that.z#%s() received an invalid keyword %rr   suffixzO%s() received both `name` and `suffix`, which are mutually exclusive arguments.c                    s   di }d v rd vr d  d<  |_   D ]\}}t||}t||| q| |_||_||_|j| g|R i |S )Ngetheadr   )
action_mapitemsgetattrsetattrrequestargskwargsdispatch)r"   r#   r$   selfmethodactionhandleractionscls
initkwargsr   r   viewg   s   
z"ViewSetMixin.as_view.<locals>.viewr   )updated)assigned)      F)r   descriptionr   detailbasename	TypeErrorhttp_method_namesr   r   r   r%   r,   r-   r+   DJANGO_VERSIONlogin_requiredr   )r,   r+   r-   keyr.   r   r*   r   as_view:   s<   	

zViewSetMixin.as_viewc                    sH   t  j|g|R i |}|j }|dkrd| _|S | j|| _|S )z[
        Set the `.action` attribute on the view, depending on the request method.
        optionsmetadata)superinitialize_requestr'   lowerr(   r   r   )r&   r"   r#   r$   r'   	__class__r   r   r?      s   
zViewSetMixin.initialize_requestc                 O   s^   d| j |f }d}| jr| jjr| jjj}|r|d | }|d| j t|g|R i |S )z>
        Reverse the action for the given `url_name`.
        %s-%sN:r"   )r5   r"   resolver_match	namespace
setdefaultr   )r&   url_namer#   r$   rF   r   r   r   reverse_action   s   
zViewSetMixin.reverse_actionc                 C   s   dd t | tD S )zP
        Get the methods that are marked as an extra ViewSet `@action`.
        c                 S   s   g | ]	\}}t ||qS r   )r   ).0r   r'   r   r   r   
<listcomp>   s    z2ViewSetMixin.get_extra_actions.<locals>.<listcomp>)r   r   )r,   r   r   r   get_extra_actions   s   zViewSetMixin.get_extra_actionsc              	      s   i } j du r	|S  fdd  D }|D ]<}z1d j|jf } jjj}|r.d||f }t| j j	 jd} j
di |j	}||| < W q tyR   Y qw |S )z
        Build a map of {names: urls} for the extra actions.

        This method will noop if `detail` was not provided as a view initkwarg.
        Nc                    s   g | ]
}|j  j kr|qS r   )r4   )rJ   r(   r&   r   r   rK      s
    z9ViewSetMixin.get_extra_action_url_map.<locals>.<listcomp>rC   z%s:%s)r"   r   )r4   rL   r5   rH   r"   rE   rF   r   r#   r$   rB   get_view_namer   )r&   action_urlsr+   r(   rH   rF   urlr.   r   rM   r   get_extra_action_url_map   s&   


z%ViewSetMixin.get_extra_action_url_map)N)r   
__module____qualname____doc__r   r;   r?   rI   classmethodrL   rQ   __classcell__r   r   rA   r   r   -   s    Y
r   c                   @      e Zd ZdZdS )ViewSetzI
    The base ViewSet class does not provide any actions by default.
    Nr   rR   rS   rT   r   r   r   r   rX      s    rX   c                   @   rW   )GenericViewSetz
    The GenericViewSet class does not provide any actions by default,
    but does include the base set of generic view behavior, such as
    the `get_object` and `get_queryset` methods.
    NrY   r   r   r   r   rZ      s    rZ   c                   @   rW   )ReadOnlyModelViewSetzL
    A viewset that provides default `list()` and `retrieve()` actions.
    NrY   r   r   r   r   r[      s    r[   c                   @   rW   )ModelViewSetz
    A viewset that provides default `create()`, `retrieve()`, `update()`,
    `partial_update()`, `destroy()` and `list()` actions.
    NrY   r   r   r   r   r\      s    r\   N)$rT   	functoolsr   inspectr   djangor   r8   django.urlsr   django.utils.decoratorsr   django.views.decorators.csrfr   rest_frameworkr   r	   r
   rest_framework.decoratorsr   rest_framework.reverser   r   r   r   APIViewrX   GenericAPIViewrZ   RetrieveModelMixinListModelMixinr[   CreateModelMixinUpdateModelMixinDestroyModelMixinr\   r   r   r   r   <module>   s6    	 0		