B
    Ö-
d²  ã               @   sv   d Z ddlmZmZmZ G dd„ deƒZdd„ Zdd„ Zdd
d„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )zCFunctions that help with dynamically creating decorators for views.é    )ÚpartialÚupdate_wrapperÚwrapsc                   s   e Zd Zd‡ fdd„	Z‡  ZS )ÚclassonlymethodNc                s   |d k	rt dƒ‚tƒ  ||¡S )Nz=This method is available only on the class, not on instances.)ÚAttributeErrorÚsuperÚ__get__)ÚselfÚinstanceÚcls)Ú	__class__© ú;/tmp/pip-install-nbqhn9mb/Django/django/utils/decorators.pyr      s    zclassonlymethod.__get__)N)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   )r   r   r      s   r   c             C   s   |dd„ ƒ}t | |ƒ d S )Nc              _   s   d S )Nr   )ÚargsÚkwargsr   r   r   Údummy   s    z%_update_method_wrapper.<locals>.dummy)r   )Ú_wrapperÚ	decoratorr   r   r   r   Ú_update_method_wrapper   s    r   c                sT   t ˆ dƒrˆ ddd… ‰ nˆ g‰ ‡ ‡fdd„}xˆ D ]}t||ƒ q4W t|ˆƒ |S )zŠ
    Decorate `method` with one or more function decorators. `decorators` can be
    a single decorator or an iterable of decorators.
    Ú__iter__Néÿÿÿÿc                s4   t ˆ | t| ƒ¡ƒ}xˆ D ]}||ƒ}qW |||ŽS )N)r   r   Útype)r	   r   r   Zbound_methodÚdec)Ú
decoratorsÚmethodr   r   r   #   s    
z!_multi_decorate.<locals>._wrapper)Úhasattrr   r   )r   r   r   r   r   )r   r   r   Ú_multi_decorate   s    


r    Ú c                sF   ‡ ‡fdd„}t ˆ dƒs"t|ˆ ƒ t ˆ dƒr0ˆ nˆ j}d|j |_|S )z>
    Convert a function decorator into a method decorator
    c                sp   t | tƒstˆ | ƒS ˆr"t| ˆƒs2td| ˆf ƒ‚t| ˆƒ}t|ƒsVtdˆ| |f ƒ‚tˆ |ƒ}t| ˆ|ƒ | S )NzfThe keyword argument `name` must be the name of a method of the decorated class: %s. Got '%s' instead.zACannot decorate '%s' as it isn't a callable attribute of %s (%s).)	Ú
isinstancer   r    r   Ú
ValueErrorÚgetattrÚcallableÚ	TypeErrorÚsetattr)Úobjr   r   )r   Únamer   r   Ú_dec=   s    



zmethod_decorator.<locals>._decr   r   zmethod_decorator(%s))r   r   r   r   )r   r)   r*   r(   r   )r   r)   r   Úmethod_decorator5   s    

r+   c             C   s   t | ƒS )a<  
    Like decorator_from_middleware, but return a function
    that accepts the arguments to be passed to the middleware_class.
    Use like::

         cache_page = decorator_from_middleware_with_args(CacheMiddleware)
         # ...

         @cache_page(3600)
         def my_view(request):
             # ...
    )Úmake_middleware_decorator)Úmiddleware_classr   r   r   Ú#decorator_from_middleware_with_argsY   s    r.   c             C   s
   t | ƒƒ S )zÇ
    Given a middleware class (not an instance), return a view decorator. This
    lets you use middleware functionality on a per-view basis. The middleware
    is created with no params passed.
    )r,   )r-   r   r   r   Údecorator_from_middlewarei   s    r/   c                s   ‡ fdd„}|S )Nc                 s   ‡ ‡‡fdd„}|S )Nc                s*   ˆˆfˆžˆŽ‰ t ˆƒ‡ ‡fdd„ƒ}|S )Nc          
      s  t ˆdƒr ˆ ˆ ¡}|d k	r |S t ˆdƒrFˆ ˆ ˆ||¡}|d k	rF|S yˆˆ f|ž|Ž}W nF tk
r  } z(t ˆdƒrŽˆ ˆ |¡}|d k	rŽ|S ‚ W d d }~X Y nX t |dƒrðt|jƒrðt ˆdƒrÌˆ ˆ |¡}t ˆdƒrî‡‡ fdd„}| |¡ nt ˆdƒrˆ 	ˆ |¡S |S )	NÚprocess_requestÚprocess_viewÚprocess_exceptionÚrenderÚprocess_template_responseÚprocess_responsec                s   ˆ   ˆ| ¡S )N)r5   )Úresponse)Ú
middlewareÚrequestr   r   Úcallback   s    zomake_middleware_decorator.<locals>._make_decorator.<locals>._decorator.<locals>._wrapped_view.<locals>.callback)
r   r0   r1   Ú	Exceptionr2   r%   r3   r4   Zadd_post_render_callbackr5   )r8   r   r   Úresultr6   Úer9   )r7   Ú	view_func)r8   r   Ú_wrapped_vieww   s2    





z]make_middleware_decorator.<locals>._make_decorator.<locals>._decorator.<locals>._wrapped_view)r   )r=   r>   )Úm_argsÚm_kwargsr-   )r7   r=   r   Ú
_decoratort   s    zFmake_middleware_decorator.<locals>._make_decorator.<locals>._decoratorr   )r?   r@   rA   )r-   )r?   r@   r   Ú_make_decorators   s    #z2make_middleware_decorator.<locals>._make_decoratorr   )r-   rB   r   )r-   r   r,   r   s    %r,   c             C   s   d| _ d| _| S )zj
    Mark a middleware factory as returning a hybrid middleware supporting both
    types of request.
    T)Úsync_capableÚasync_capable)Úfuncr   r   r   Úsync_and_async_middleware›   s    rF   c             C   s   d| _ d| _| S )z\
    Mark a middleware factory as returning a sync middleware.
    This is the default.
    TF)rC   rD   )rE   r   r   r   Úsync_only_middleware¥   s    rG   c             C   s   d| _ d| _| S )z;Mark a middleware factory as returning an async middleware.FT)rC   rD   )rE   r   r   r   Úasync_only_middleware¯   s    rH   N)r!   )Ú__doc__Ú	functoolsr   r   r   Úclassmethodr   r   r    r+   r.   r/   r,   rF   rG   rH   r   r   r   r   Ú<module>   s   	
$	)

