a
    Qh9                     @   s   d dl Z d dlZd dlZd dlmZ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dZG dd dZdd Zee dS )    N)async_to_synciscoroutinefunction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|}|}ttjD ]^}t	|}t
|dd}t
|dd}|st|sttd| n|s|rd}	n|}	z&| j|	||tjd| d}
||
}W n\ ty } zBtjrt|rtd|| ntd	| W Y d
}~q8W Y d
}~nd
}~0 0 |
}|d
u r$td| t|drH| j d| ||j t|drj| j| ||j t|dr| j| d|j t|}|	}q8| |||}|| _d
S )z
        Populate middleware lists from settings.MIDDLEWARE.

        Must be called after the environment is fixed (see __call__ in subclasses).
        sync_capableT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   
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/   R/var/www/sistema_ama/venv/lib/python3.9/site-packages/django/core/handlers/base.pyload_middleware   st    "
zBaseHandler.load_middlewarec                 C   sl   |du rt |}|r&|s&|p$d|j }|rL|sh|r>td| t|ddS n|rh|r`t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 handler adapted for %s.Tthread_sensitivez$Asynchronous handler adapted for %s.)r   __qualname__r%   r   r   r   )r*   r+   methodZmethod_is_asyncr   r   r/   r/   r0   r"   h   s    zBaseHandler.adapt_method_modec                 C   sF   t tj | |}|j|j |jdkrBtd|j	|j
||d |S )z8Return an HttpResponse object for the given HttpRequest.  %s: %sresponserequest)r   r   ROOT_URLCONFr)   _resource_closersr(   closestatus_coder   reason_phrasepathr*   r:   r9   r/   r/   r0   r,      s    


zBaseHandler.get_responsec                    sZ   t tj | |I dH }|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.
        Nr6   Fr2   r7   r8   )r   r   r;   r)   r<   r(   r=   r>   r   r   r?   r@   rA   r/   r/   r0   get_response_async   s    	


zBaseHandler.get_response_asyncc           	   
   C   sT  d}|  |\}}}| jD ]}|||||}|r q6q|du r| |}t|rXt|}z||g|R i |}W n: ty } z"| ||}|du r W Y d}~n
d}~0 0 | || t|drPt	|j
rP| jD ]*}|||}| j||d|jjjf d qz|
 }W n> tyN } z$| ||}|du r: W Y d}~n
d}~0 0 |S )
        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render%s.process_template_responser   )resolve_requestr   make_view_atomicr   r   	Exceptionprocess_exception_by_middlewarecheck_responser&   callablerD   r   __self__	__class____name__	r*   r:   r9   callbackZcallback_argsZcallback_kwargsmiddleware_methodZwrapped_callbacker/   r/   r0   r      sH    





zBaseHandler._get_responsec           	   
      s  d}|  |\}}}| jD ] }|||||I dH }|r q<q|du r| |}t|sbt|dd}z ||g|R i |I dH }W nH ty } z0t| jdd||I dH }|du r W Y d}~n
d}~0 0 | || t|drt	|j
r| jD ]0}|||I dH }| j||d|jjjf d qz6t|j
rF|
 I dH }nt|j
dd I dH }W nL ty } z2t| jdd||I dH }|du r W Y d}~n
d}~0 0 t|rtd|S )rC   NTr2   rD   rE   rF   zResponse is still a coroutine.)rG   r   rH   r   r   rI   rJ   rK   r&   rL   rD   r   rM   rN   rO   asyncioiscoroutiner!   rP   r/   r/   r0   r      sx    







zBaseHandler._get_response_asyncc                 C   s>   t |dr"|j}t| t|}nt }||j}||_|S )zw
        Retrieve/set the urlconf for the request. Return the view resolved,
        with its args and kwargs.
        urlconf)r&   rV   r   r   resolve	path_inforesolver_match)r*   r:   rV   ZresolverrY   r/   r/   r0   rG   ,  s    

zBaseHandler.resolve_requestc                 C   sz   |du st |sdS |sJt|tjr8d|j|jf }nd|j|jjf }|du r`td| nt |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.)	rT   rU   
isinstancetypesFunctionType
__module__rO   rN   
ValueError)r*   r9   rQ   r   r/   r/   r0   rK   =  s*    
zBaseHandler.check_responsec                 C   sV   t |dt }tj D ]8\}}|d r||vrt|r@tdtj|d|}q|S )NZ_non_atomic_requestsZATOMIC_REQUESTSz0You cannot use ATOMIC_REQUESTS with async views.)Zusing)	r    setr	   r   itemsr   r!   r
   Zatomic)r*   viewZnon_atomic_requestsaliasZsettings_dictr/   r/   r0   rH   Y  s    zBaseHandler.make_view_atomicc                 C   s&   | j D ]}|||}|r|  S qdS )z
        Pass the exception to the exception middleware. If no middleware
        return a response for this exception, return None.
        N)r   )r*   	exceptionr:   rR   r9   r/   r/   r0   rJ   d  s
    


z+BaseHandler.process_exception_by_middleware)F)NFN)N)rO   r]   r4   r   r   r   r)   r1   r"   r,   rB   r   r   rG   rK   rH   rJ   r/   r/   r/   r0   r      s"   
R   
 6H
r   c                 K   s   t d dS )z1Reset the URLconf after each request is finished.N)r   )senderkwargsr/   r/   r0   reset_urlconfp  s    rf   )rT   loggingr[   asgiref.syncr   r   r   django.confr   django.core.exceptionsr   r   Zdjango.core.signalsr   Z	django.dbr	   r
   django.urlsr   r   django.utils.logr   Zdjango.utils.module_loadingr   rc   r   	getLoggerr%   r   rf   connectr/   r/   r/   r0   <module>   s"   
  ^