o
    ˷e9                     @   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dZG dd dZdd Ze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                   @   sx   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}|s8|s8td| |s?|r?d}	n|}	z| j|	||tjd| d}
||
}W n' ty{ } ztjrqt|rktd|| ntd	| W Y d
}~qd
}~ww |
}|d
u rtd| t|dr| j d| ||j t|dr| j| ||j t|dr| j| d|j t|}|	}q| |||}|| _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handlerhandler_is_asyncmiddleware_path
middlewaremiddleware_can_syncmiddleware_can_asyncmiddleware_is_asyncadapted_handlermw_instanceexc r6   P/var/www/ideatree/venv/lib/python3.10/site-packages/django/core/handlers/base.pyload_middleware   sx   



zBaseHandler.load_middlewarec                 C   sp   |du r	t |}|r|s|pd|j }|r(|s&|r td| t|ddS |S |r6|r2t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.)asyncioiscoroutinefunction__qualname__r$   r   r   r   )r)   r*   methodmethod_is_asyncr   r   r6   r6   r7   r!   h   s   
zBaseHandler.adapt_method_modec                 C   sF   t tj | |}|j|j |jdkr!t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)   rD   rC   r6   r6   r7   r+      s   


zBaseHandler.get_responsec                    s\   t tj | |I dH }|j|j |jdkr,t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.
        Nr@   Fr9   rA   rB   )r   r   rE   r(   rF   r'   rG   rH   r   r   rI   rJ   rK   r6   r6   r7   get_response_async   s   
	

zBaseHandler.get_response_asyncc           	   
   C   sR  d}|  |\}}}| jD ]}|||||}|r nq|du rX| |}t|r-t|}z||g|R i |}W n tyW } z| ||}|du rM W Y d}~nd}~ww | || t	|drt
|jr| jD ]}|||}| j||d|jjjf d qkz| }W |S  ty } z| ||}|du r W Y d}~|S d}~ww |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<   r   	Exceptionprocess_exception_by_middlewarecheck_responser%   callablerN   r   __self__	__class____name__	r)   rD   rC   callbackcallback_argscallback_kwargsmiddleware_methodwrapped_callbacker6   r6   r7   r      sZ   







zBaseHandler._get_responsec           	   
      s  d}|  |\}}}| jD ]}|||||I dH }|r nq|du rh| |}t|s3t|dd}z||g|R i |I dH }W n$ tyg } zt| jdd||I dH }|du r] W Y d}~nd}~ww | || t	|drt
|jr| jD ]}|||I dH }| j||d|jjjf d q{zt|jr| I dH }nt|jdd I dH }W n$ ty } zt| jdd||I dH }|du rɂ W Y d}~nd}~ww t|rtd|S )rM   NTr9   rN   rO   rP   zResponse is still a coroutine.)rQ   r   rR   r;   r<   r   rS   rT   rU   r%   rV   rN   r   rW   rX   rY   iscoroutiner    rZ   r6   r6   r7   r      s   




	



	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%   rb   r   r
   resolve	path_inforesolver_match)r)   rD   rb   resolverre   r6   r6   r7   rQ   ,  s   

zBaseHandler.resolve_requestc                 C   sx   |du st |sdS |s%t|tjrd|j|jf }n	d|j|jjf }|du r/td| t |r:t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;   ra   
isinstancetypesFunctionType
__module__rY   rX   
ValueError)r)   rC   r[   r   r6   r6   r7   rU   =  s,   
zBaseHandler.check_responsec                 C   sX   t |dt }tj D ]\}}|d r)||vr)t|r!tdtj	|d|}q|S )N_non_atomic_requestsATOMIC_REQUESTSz0You cannot use ATOMIC_REQUESTS with async views.)using)
r   setr   r   itemsr;   r<   r    r	   atomic)r)   viewnon_atomic_requestsaliassettings_dictr6   r6   r7   rR   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)   	exceptionrD   r^   rC   r6   r6   r7   rT   d  s   

z+BaseHandler.process_exception_by_middleware)F)NFN)N)rY   rj   r=   r   r   r   r(   r8   r!   r+   rL   r   r   rQ   rU   rR   rT   r6   r6   r6   r7   r      s$    
R
 6H
r   c                 K   s   t d dS )z1Reset the URLconf after each request is finished.N)r   )senderkwargsr6   r6   r7   reset_urlconfp  s   ry   )r;   loggingrh   asgiref.syncr   r   django.confr   django.core.exceptionsr   r   django.core.signalsr   	django.dbr   r	   django.urlsr
   r   django.utils.logr   django.utils.module_loadingr   rv   r   	getLoggerr$   r   ry   connectr6   r6   r6   r7   <module>   s$    
  ^