3
g[8                 @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dl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dd Zeje dS )    N)async_to_syncsync_to_async)settings)ImproperlyConfiguredMiddlewareNotUsed)request_finished)connectionstransaction)get_resolverset_urlconf)log_response)import_string   )convert_exception_to_responsezdjango.requestc               @   sr   e Zd ZdZdZdZdZdddZdddZdd Z	d	d
 Z
dd Zdd Zdd ZdddZdd Zdd ZdS )BaseHandlerNFc             C   s  g | _ g | _g | _|r| jn| j}t|}|}xhttjD ]X}t	|}t
|dd}t
|dd}| r|| r|td| n| r|rd}	n|}	y&| j|	||tjd| d}
||
}W nP tk
r } z2tjrt|rtjd|| ntjd	| w<W Y d
d
}~X nX |
}|d
kr"td| t|drF| j jd| j||j t|drh| jj| j||j t|dr| jj| jd|j t|}|	}q<W | j|||}|| _d
S )z
        Populate middleware lists from settings.MIDDLEWARE.

        Must be called after the environment is fixed (see __call__ in subclasses).
        Zsync_capableTZasync_capableFzOMiddleware %s must have at least one of sync_capable/async_capable set to True.zmiddleware %s)debugnamezMiddlewareNotUsed(%r): %szMiddlewareNotUsed: %rNz$Middleware factory %s returned None.process_viewr   process_template_responseprocess_exception)_view_middleware_template_response_middleware_exception_middleware_get_response_async_get_responser   reversedr   Z
MIDDLEWAREr   getattrRuntimeErroradapt_method_modeDEBUGr   strloggerr   r   hasattrinsertr   appendr   r   _middleware_chain)selfis_asyncget_responsehandlerZhandler_is_asyncZmiddleware_pathZ
middlewareZmiddleware_can_syncZmiddleware_can_asyncZmiddleware_is_asyncZadapted_handlerZmw_instanceexc r+   W/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/core/handlers/base.pyload_middleware   s\    



zBaseHandler.load_middlewarec             C   sp   |dkrt j|}|r*| r*|p(d|j }|rP|sl|rBtjd| t|ddS n|rl|rdtjd| t|S |S )a\  
        Adapt a method to be in the correct "mode":
        - If is_async is False:
          - Synchronous methods are left alone
          - Asynchronous methods are wrapped with async_to_sync
        - If is_async is True:
          - Synchronous methods are wrapped with sync_to_async()
          - Asynchronous methods are left alone
        Nzmethod %s()zSynchronous %s adapted.T)thread_sensitivezAsynchronous %s adapted.)asyncioiscoroutinefunction__qualname__r!   r   r   r   )r&   r'   methodZmethod_is_asyncr   r   r+   r+   r,   r   c   s    

zBaseHandler.adapt_method_modec             C   sF   t tj | j|}|jj|j |jdkrBtd|j	|j
||d |S )z8Return an HttpResponse object for the given HttpRequest.i  z%s: %s)responserequest)r   r   ROOT_URLCONFr%   _resource_closersr$   closestatus_coder   reason_phrasepath)r&   r4   r3   r+   r+   r,   r(   ~   s    



zBaseHandler.get_responsec                sZ   t tj | j|I dH }|jj|j |jdkrVtt	ddd|j
|j||dI dH  |S )z
        Asynchronous version of get_response.

        Funneling everything, including WSGI, into a single async
        get_response() is too slow. Avoid the context switch by using
        a separate async response path.
        Ni  F)r.   z%s: %s)r3   r4   )r   r   r5   r%   r6   r$   r7   r8   r   r   r9   r:   )r&   r4   r3   r+   r+   r,   get_response_async   s    	



zBaseHandler.get_response_asyncc       	      C   sP  d}| j |\}}}x"| jD ]}|||||}|rP qW |dkr| j|}tj|r\t|}y||f||}W n8 tk
r } z| j||}|dkr W Y dd}~X nX | j|| t	|dot
|jrLx4| jD ]*}|||}| j||d|jjjf d qW y|j }W n< tk
rJ } z| j||}|dkr: W Y dd}~X nX |S )z
        Resolve and call the view, then apply view, exception, and
        template_response middleware. This method is everything that happens
        inside the request/response middleware.
        Nrenderz%s.process_template_response)r   )resolve_requestr   make_view_atomicr/   r0   r   	Exceptionprocess_exception_by_middlewarecheck_responser"   callabler<   r   __self__	__class____name__)	r&   r4   r3   callbackcallback_argscallback_kwargsmiddleware_methodwrapped_callbacker+   r+   r,   r      s@    



zBaseHandler._get_responsec       	         s  d}| j |\}}}x(| jD ]}|||||I dH }|rP qW |dkr| j|}tj|sft|dd}y||f||I dH }W nF tk
r } z*t| jdd||I dH }|dkr W Y dd}~X nX | j|| t	|dot
|jrx:| jD ]0}|||I dH }| j||d|jjjf d qW y8tj|jrF|j I dH }nt|jdd I dH }W nJ tk
r } z,t| jdd||I dH }|dkr W Y dd}~X nX tj|rtd|S )z
        Resolve and call the view, then apply view, exception, and
        template_response middleware. This method is everything that happens
        inside the request/response middleware.
        NT)r.   r<   z%s.process_template_response)r   zResponse is still a coroutine.)r=   r   r>   r/   r0   r   r?   r@   rA   r"   rB   r<   r   rC   rD   rE   iscoroutiner   )	r&   r4   r3   rF   rG   rH   rI   rJ   rK   r+   r+   r,   r      sT    


zBaseHandler._get_response_asyncc             C   s>   t |dr"|j}t| t|}nt }|j|j}||_|S )zw
        Retrieve/set the urlconf for the request. Return the view resolved,
        with its args and kwargs.
        urlconf)r"   rM   r   r
   resolveZ	path_inforesolver_match)r&   r4   rM   resolverrO   r+   r+   r,   r=     s    

zBaseHandler.resolve_requestc             C   sz   |dkpt j|sdS |sJt|tjr8d|j|jf }nd|j|jjf }|dkr`td| nt j|rvtd| dS )zT
        Raise an error if the view returned None or an uncalled coroutine.
        NzThe view %s.%szThe view %s.%s.__call__zB%s didn't return an HttpResponse object. It returned None instead.z%s didn't return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an 'await' into your view.)	r/   rL   
isinstancetypesFunctionType
__module__rE   rD   
ValueError)r&   r3   rF   r   r+   r+   r,   rA   &  s     

zBaseHandler.check_responsec             C   s\   t |dt }xHtj D ]<}|jd r|j|krtj|rBtdt	j
|jd|}qW |S )NZ_non_atomic_requestsZATOMIC_REQUESTSz0You cannot use ATOMIC_REQUESTS with async views.)Zusing)r   setr   allZsettings_dictaliasr/   r0   r   r	   Zatomic)r&   viewZnon_atomic_requestsdbr+   r+   r,   r>   B  s    
zBaseHandler.make_view_atomicc             C   s&   x | j D ]}|||}|r|S qW dS )z
        Pass the exception to the exception middleware. If no middleware
        return a response for this exception, return None.
        N)r   )r&   	exceptionr4   rI   r3   r+   r+   r,   r@   M  s
    
z+BaseHandler.process_exception_by_middleware)F)NFN)N)rE   rT   r1   r   r   r   r%   r-   r   r(   r;   r   r   r=   rA   r>   r@   r+   r+   r+   r,   r      s   
J
4A
r   c             K   s   t d dS )z1Reset the URLconf after each request is finished.N)r   )Zsenderkwargsr+   r+   r,   reset_urlconfY  s    r]   )r/   loggingrR   Zasgiref.syncr   r   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.core.signalsr   Z	django.dbr   r	   Zdjango.urlsr
   r   Zdjango.utils.logr   Zdjango.utils.module_loadingr   r[   r   	getLoggerr!   r   r]   connectr+   r+   r+   r,   <module>   s"   
  G