3
v^}                 @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ e jdZG d	d
 d
ZG dd dZG dd dZG dd deeeZG dd deZdS )    N)update_wrapper)ImproperlyConfigured)HttpResponseHttpResponseGoneHttpResponseNotAllowedHttpResponsePermanentRedirectHttpResponseRedirect)TemplateResponse)reverse)classonlymethodzdjango.requestc               @   s   e Zd ZdZdZdd ZdS )ContextMixinz
    A default context mixin that passes the keyword arguments received by
    get_context_data() as the template context.
    Nc             K   s&   |j d|  | jd k	r"|j| j |S )Nview)
setdefaultextra_contextupdate)selfkwargs r   =/usr/lib/python3.6/site-packages/django/views/generic/base.pyget_context_data   s    
zContextMixin.get_context_data)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c               @   s`   e Zd ZdZdddddddd	gZd
d Zedd Zdd Zdd Z	dd Z
dd Zdd ZdS )Viewz}
    Intentionally simple parent class for all views. Only implements
    dispatch-by-method and simple sanity checking.
    getpostputpatchdeleteheadoptionstracec             K   s&   x |j  D ]\}}t| || q
W dS )z|
        Constructor. Called in the URLconf; can contain helpful extra
        keyword arguments, and other things.
        N)itemssetattr)r   r   keyvaluer   r   r   __init__&   s    zView.__init__c                s   xDD ]<}| j kr&td| jf t |std j|f qW  fdd} |_|_t| f d t| jf d |S )z0Main entry point for a request-response process.zUYou tried to pass in the %s method name as a keyword argument to %s(). Don't do that.zm%s() received an invalid keyword %r. as_view only accepts arguments that are already attributes of the class.c                sd    f }t |dr(t |d r(|j|_|j| f|| t |dsRtd j |j| f||S )Nr   r    requestz\%s instance has no 'request' attribute. Did you override setup() and forget to call super()?)hasattrr   r    setupAttributeErrorr   dispatch)r(   argsr   r   )cls
initkwargsr   r   r   =   s    


zView.as_view.<locals>.view)updated)assigned)http_method_names	TypeErrorr   r)   Z
view_classZview_initkwargsr   r,   )r.   r/   r%   r   r   )r.   r/   r   as_view0   s    


zView.as_viewc             O   s   || _ || _|| _dS )z1Initialize attributes shared by all view methods.N)r(   r-   r   )r   r(   r-   r   r   r   r   r*   S   s    z
View.setupc             O   s<   |j j | jkr&t| |j j | j}n| j}||f||S )N)methodlowerr2   getattrhttp_method_not_allowed)r   r(   r-   r   handlerr   r   r   r,   Y   s    zView.dispatchc             O   s(   t jd|j|jd|dd t| j S )NzMethod Not Allowed (%s): %si  )status_coder(   )extra)loggerwarningr5   pathr   _allowed_methods)r   r(   r-   r   r   r   r   r8   c   s    
zView.http_method_not_allowedc             O   s$   t  }dj| j |d< d|d< |S )z8Handle responding to requests for the OPTIONS HTTP verb.z, Allow0zContent-Length)r   joinr?   )r   r(   r-   r   responser   r   r   r!   j   s    zView.optionsc                s    fdd j D S )Nc                s   g | ]}t  |r|j qS r   )r)   upper).0m)r   r   r   
<listcomp>r   s    z)View._allowed_methods.<locals>.<listcomp>)r2   )r   r   )r   r   r?   q   s    zView._allowed_methodsN)r   r   r   r   r2   r'   r   r4   r*   r,   r8   r!   r?   r   r   r   r   r      s   
#
r   c               @   s0   e Zd ZdZdZdZeZdZdd Z	dd Z
dS )TemplateResponseMixinz.A mixin that can be used to render a template.Nc             K   s0   |j d| j | jf | j| j || jd|S )z
        Return a response, using the `response_class` for this view, with a
        template rendered with the given context.

        Pass response_kwargs to the constructor of the response class.
        content_type)r(   templatecontextusing)r   rI   response_classr(   get_template_namestemplate_engine)r   rK   Zresponse_kwargsr   r   r   render_to_response|   s    z(TemplateResponseMixin.render_to_responsec             C   s    | j dkrtdn| j gS dS )z
        Return a list of template names to be used for the request. Must return
        a list. May not be called if render_to_response() is overridden.
        NztTemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()')template_namer   )r   r   r   r   rN      s    
z(TemplateResponseMixin.get_template_names)r   r   r   r   rQ   rO   r	   rM   rI   rP   rN   r   r   r   r   rH   u   s   rH   c               @   s   e Zd ZdZdd ZdS )TemplateViewzT
    Render a template. Pass keyword arguments from the URLconf to the context.
    c             O   s   | j f |}| j|S )N)r   rP   )r   r(   r-   r   rK   r   r   r   r      s    zTemplateView.getN)r   r   r   r   r   r   r   r   r   rR      s   rR   c               @   s`   e Zd ZdZdZdZdZdZdd Zdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd ZdS )RedirectViewz&Provide a redirect on any GET request.FNc             O   sX   | j r| j | }n| jr*t| j||d}ndS | jjjdd}|rT| jrTd||f }|S )z
        Return the URL redirect to. Keyword arguments from the URL pattern
        match generating the redirect request are provided as kwargs to this
        method.
        )r-   r   NQUERY_STRING z%s?%s)urlpattern_namer
   r(   METAr   query_string)r   r-   r   rV   r   r   r   get_redirect_url   s    
zRedirectView.get_redirect_urlc             O   sJ   | j ||}|r(| jrt|S t|S ntjd|jd|dd t S d S )NzGone: %si  )r:   r(   )r;   )rZ   	permanentr   r   r<   r=   r>   r   )r   r(   r-   r   rV   r   r   r   r      s    
zRedirectView.getc             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r       s    zRedirectView.headc             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r      s    zRedirectView.postc             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r!      s    zRedirectView.optionsc             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r      s    zRedirectView.deletec             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r      s    zRedirectView.putc             O   s   | j |f||S )N)r   )r   r(   r-   r   r   r   r   r      s    zRedirectView.patch)r   r   r   r   r[   rV   rW   rY   rZ   r   r    r   r!   r   r   r   r   r   r   r   rS      s   rS   )logging	functoolsr   django.core.exceptionsr   django.httpr   r   r   r   r   Zdjango.template.responser	   django.urlsr
   Zdjango.utils.decoratorsr   	getLoggerr<   r   r   rH   rR   rS   r   r   r   r   <module>   s   
W$	