U
    ڲg'                     @   s6  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mZ ddlmZ dd	 ZG d
d dejZG dd dejeZG dd dejeZG dd dejeZG dd dejeZG dd dejeZG dd dejejeZG dd dejejeZG dd dejejeZG dd dejejejeZdS )z7
Generic views that provide commonly needed behaviour.
    )ValidationError)QuerySet)Http404)get_object_or_404)mixinsviews)api_settingsc              
   O   s6   zt | f||W S  tttfk
r0   tY nX dS )z
    Same as Django's standard shortcut, but make sure to also raise 404
    if the filter_kwargs don't match the required types.
    N)_get_object_or_404	TypeError
ValueErrorr   r   )querysetZfilter_argsfilter_kwargs r   ;/tmp/pip-unpacked-wheel-11h17kvo/rest_framework/generics.pyr      s    r   c                   @   s   e Zd ZdZdZdZdZdZej	Z
ejZdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Zedd Zdd Zdd ZdS )GenericAPIViewz1
    Base class for all other generic views.
    Npkc                 O   s   | S Nr   )clsargskwargsr   r   r   __class_getitem__1   s    z GenericAPIView.__class_getitem__c                 C   s6   | j dk	std| jj | j }t|tr2| }|S )a,  
        Get the list of items for this view.
        This must be an iterable, and may be a queryset.
        Defaults to using `self.queryset`.

        This method should always be used rather than accessing `self.queryset`
        directly, as `self.queryset` gets evaluated only once, and those results
        are cached for all subsequent requests.

        You may want to override this if you need to provide different
        querysets depending on the incoming request.

        (Eg. return a list of items that is specific to the user)
        Nz['%s' should either include a `queryset` attribute, or override the `get_queryset()` method.)r   AssertionError	__class____name__
isinstancer   allselfr   r   r   r   get_queryset4   s    
zGenericAPIView.get_querysetc                 C   sf   |  |  }| jp| j}|| jks8td| jj|f | j| j| i}t|f|}| 	| j
| |S )z
        Returns the object the view is displaying.

        You may want to override this if you need to provide non-standard
        queryset lookups.  Eg if objects are referenced using multiple
        keyword arguments in the url conf.
        zExpected view %s to be called with a URL keyword argument named "%s". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.)filter_querysetr   lookup_url_kwarglookup_fieldr   r   r   r   r   Zcheck_object_permissionsrequest)r   r   r    r   objr   r   r   
get_objectO   s    
zGenericAPIView.get_objectc                 O   s"   |   }|d|   |||S )z
        Return the serializer instance that should be used for validating and
        deserializing input, and for serializing output.
        context)get_serializer_class
setdefaultget_serializer_context)r   r   r   serializer_classr   r   r   get_serializerk   s    zGenericAPIView.get_serializerc                 C   s    | j dk	std| jj | j S )a?  
        Return the class to use for the serializer.
        Defaults to using `self.serializer_class`.

        You may want to override this if you need to provide different
        serializations depending on the incoming request.

        (Eg. admins get full serialization, others get basic serialization)
        Nzk'%s' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method.)r)   r   r   r   r   r   r   r   r&   t   s    
z#GenericAPIView.get_serializer_classc                 C   s   | j | j| dS )zA
        Extra context provided to the serializer class.
        )r"   formatview)r"   Zformat_kwargr+   r   r   r   r(      s    z%GenericAPIView.get_serializer_contextc                 C   s&   t | jD ]}| | j|| }q
|S )aW  
        Given a queryset, filter it with whichever filter backend is in use.

        You are unlikely to want to override this method, although you may need
        to call it either from a list view, or from a custom `get_object`
        method if you want to apply the configured filtering backend to the
        default queryset.
        )listfilter_backendsr   r"   )r   r   backendr   r   r   r      s    	zGenericAPIView.filter_querysetc                 C   s,   t | ds&| jdkrd| _n
|  | _| jS )zM
        The paginator instance associated with the view, or `None`.
        
_paginatorN)hasattrpagination_classr1   r+   r   r   r   	paginator   s
    


zGenericAPIView.paginatorc                 C   s"   | j dkrdS | j j|| j| dS )zW
        Return a single page of results, or `None` if pagination is disabled.
        N)r-   )r4   paginate_querysetr"   r   r   r   r   r5      s    
z GenericAPIView.paginate_querysetc                 C   s   | j dk	st| j |S )zW
        Return a paginated style `Response` object for the given output data.
        N)r4   r   get_paginated_response)r   datar   r   r   r6      s    z%GenericAPIView.get_paginated_response)r   
__module____qualname____doc__r   r)   r!   r    r   ZDEFAULT_FILTER_BACKENDSr/   ZDEFAULT_PAGINATION_CLASSr3   r   r   r$   r*   r&   r(   r   propertyr4   r5   r6   r   r   r   r   r      s$   		

r   c                   @   s   e Zd ZdZdd ZdS )CreateAPIViewz6
    Concrete view for creating a model instance.
    c                 O   s   | j |f||S r   creater   r"   r   r   r   r   r   post   s    zCreateAPIView.postN)r   r8   r9   r:   r@   r   r   r   r   r<      s   r<   c                   @   s   e Zd ZdZdd ZdS )ListAPIViewz/
    Concrete view for listing a queryset.
    c                 O   s   | j |f||S r   r.   r?   r   r   r   get   s    zListAPIView.getNr   r8   r9   r:   rC   r   r   r   r   rA      s   rA   c                   @   s   e Zd ZdZdd ZdS )RetrieveAPIViewz8
    Concrete view for retrieving a model instance.
    c                 O   s   | j |f||S r   retriever?   r   r   r   rC      s    zRetrieveAPIView.getNrD   r   r   r   r   rE      s   rE   c                   @   s   e Zd ZdZdd ZdS )DestroyAPIViewz6
    Concrete view for deleting a model instance.
    c                 O   s   | j |f||S r   destroyr?   r   r   r   delete   s    zDestroyAPIView.deleteN)r   r8   r9   r:   rK   r   r   r   r   rH      s   rH   c                   @   s    e Zd ZdZdd Zdd ZdS )UpdateAPIViewz6
    Concrete view for updating a model instance.
    c                 O   s   | j |f||S r   updater?   r   r   r   put   s    zUpdateAPIView.putc                 O   s   | j |f||S r   Zpartial_updater?   r   r   r   patch   s    zUpdateAPIView.patchN)r   r8   r9   r:   rO   rQ   r   r   r   r   rL      s   rL   c                   @   s    e Zd ZdZdd Zdd ZdS )ListCreateAPIViewzL
    Concrete view for listing a queryset or creating a model instance.
    c                 O   s   | j |f||S r   rB   r?   r   r   r   rC      s    zListCreateAPIView.getc                 O   s   | j |f||S r   r=   r?   r   r   r   r@      s    zListCreateAPIView.postN)r   r8   r9   r:   rC   r@   r   r   r   r   rR      s   rR   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RetrieveUpdateAPIViewzB
    Concrete view for retrieving, updating a model instance.
    c                 O   s   | j |f||S r   rF   r?   r   r   r   rC      s    zRetrieveUpdateAPIView.getc                 O   s   | j |f||S r   rM   r?   r   r   r   rO     s    zRetrieveUpdateAPIView.putc                 O   s   | j |f||S r   rP   r?   r   r   r   rQ     s    zRetrieveUpdateAPIView.patchN)r   r8   r9   r:   rC   rO   rQ   r   r   r   r   rS      s   rS   c                   @   s    e Zd ZdZdd Zdd ZdS )RetrieveDestroyAPIViewzD
    Concrete view for retrieving or deleting a model instance.
    c                 O   s   | j |f||S r   rF   r?   r   r   r   rC     s    zRetrieveDestroyAPIView.getc                 O   s   | j |f||S r   rI   r?   r   r   r   rK     s    zRetrieveDestroyAPIView.deleteN)r   r8   r9   r:   rC   rK   r   r   r   r   rT   	  s   rT   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )RetrieveUpdateDestroyAPIViewzN
    Concrete view for retrieving, updating or deleting a model instance.
    c                 O   s   | j |f||S r   rF   r?   r   r   r   rC     s    z RetrieveUpdateDestroyAPIView.getc                 O   s   | j |f||S r   rM   r?   r   r   r   rO      s    z RetrieveUpdateDestroyAPIView.putc                 O   s   | j |f||S r   rP   r?   r   r   r   rQ   #  s    z"RetrieveUpdateDestroyAPIView.patchc                 O   s   | j |f||S r   rI   r?   r   r   r   rK   &  s    z#RetrieveUpdateDestroyAPIView.deleteN)r   r8   r9   r:   rC   rO   rQ   rK   r   r   r   r   rU     s
   rU   N)r:   Zdjango.core.exceptionsr   Zdjango.db.models.queryr   Zdjango.httpr   Zdjango.shortcutsr   r	   Zrest_frameworkr   r   Zrest_framework.settingsr   ZAPIViewr   ZCreateModelMixinr<   ZListModelMixinrA   ZRetrieveModelMixinrE   ZDestroyModelMixinrH   ZUpdateModelMixinrL   rR   rS   rT   rU   r   r   r   r   <module>   sR    %				