3
W­E^}  ã               @   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   ú;/tmp/pip-build-78s3knkw/Django/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, 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ÚcontextZusing)r   rH   Úresponse_classr(   Úget_template_namesÚtemplate_engine)r   rJ   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   rL   Œ   s    
z(TemplateResponseMixin.get_template_names)r   r   r   r   rO   rM   r	   rK   rH   rN   rL   r   r   r   r   rG   u   s   rG   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   rN   )r   r(   r-   r   rJ   r   r   r   r   �   s    zTemplateView.getN)r   r   r   r   r   r   r   r   r   rP   ™   s   rP   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(   ZMETAr   Úquery_string)r   r-   r   rT   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;   )rW   Ú	permanentr   r   r<   r=   r>   r   )r   r(   r-   r   rT   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   rX   rT   rU   rV   rW   r   r    r   r!   r   r   r   r   r   r   r   rQ   ¢   s   rQ   )ÚloggingÚ	functoolsr   Zdjango.core.exceptionsr   Zdjango.httpr   r   r   r   r   Zdjango.template.responser	   Zdjango.urlsr
   Zdjango.utils.decoratorsr   Ú	getLoggerr<   r   r   rG   rP   rQ   r   r   r   r   Ú<module>   s   
W$	