o
    QDi                     @   s  d Z ddlZ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 ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ dZdedefddZ dedefddZ!edefddZ"defddZ#G dd dZ$dS )z
Debug Toolbar middleware
    N)Callable)cache)async_to_synciscoroutinefunctionmarkcoroutinefunctionsync_to_async)settings)HttpRequestHttpResponse)import_string)GetResponse)Panel)DebugToolbar)clear_stack_trace_cachesis_processable_html_response)z	text/htmlzapplication/xhtml+xmlrequestreturnc                 C   s$   t jsdS | jdt jv rdS dS )T
    Default function to determine whether to show the toolbar on a given page.
    FREMOTE_ADDRT)r   DEBUGMETAgetINTERNAL_IPS)r    r   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/debug_toolbar/middleware.pyshow_toolbar   s
   r   c                 C   sv   t jsdS | jdt jv rdS zdtdddd d }| jd|kr-W dS W dS  tj	y:   Y dS w )	r   Fr   T.zhost.docker.internalNz.1)
r   r   r   r   r   joinsocketgethostbynamersplitgaierror)r   	docker_ipr   r   r   show_toolbar_with_docker,   s"   r$   c                  C   s"   t  d } t| trt| S | S )zf
    Fetch the show toolbar callback from settings

    Cached to avoid importing multiple times.
    SHOW_TOOLBAR_CALLBACK)dt_settings
get_config
isinstancestrr   )func_or_pathr   r   r   show_toolbar_func_or_pathK   s   	
r+   c                 C   s6   t  }t|}|r| st|}|S |s| rt|}|S )z
    Get the callback function to show the toolbar.

    Will wrap the function with sync_to_async or
    async_to_sync depending on the status of async_mode
    and whether the underlying function is a coroutine.
    )r+   r   r   r   )
async_moder   is_coroutiner   r   r   get_show_toolbar[   s   r.   c                	   @   s   e Zd ZdZdZdZdefddZdede	fdd	Z
dede	fd
dZdede	dede	fddZededee deeef fddZdS )DebugToolbarMiddlewarezm
    Middleware to set up Debug Toolbar on incoming request and render toolbar
    on outgoing response.
    Tget_responsec                 C   s(   || _ t| j | _| jrt|  d S d S )N)r0   r   r,   r   )selfr0   r   r   r   __init__u   s
   zDebugToolbarMiddleware.__init__r   r   c              	   C   s   | j r| |S t| j d}||rt|r| |S t|| j}|jD ]}|  q%z||}W t	  t
|jD ]}|  q;nt	  t
|jD ]}|  qKw | |||S )Nr,   )r,   	__acall__r.   r   is_toolbar_requestr0   enabled_panelsenable_instrumentationprocess_requestr   reverseddisable_instrumentation_postprocess)r1   r   r   toolbarpanelresponser   r   r   __call__   s&   





zDebugToolbarMiddleware.__call__c              	      s   t | jd}||I d H rt|r| |I d H }|S t|| j}|jD ]}t|dr5| I d H  q&|  q&z|	|I d H }W t
  t|jD ]}|  qLnt
  t|jD ]}|  q\w | |||S )Nr3   aenable_instrumentation)r.   r,   r   r5   r0   r6   hasattrr@   r7   r8   r   r9   r:   r;   )r1   r   r   r>   r<   r=   r   r   r   r4      s*   




z DebugToolbarMiddleware.__acall__r>   r<   c                 C   s   t |jD ]}||| ||| q| }| ||j D ]	\}}||j|< q!t|s1|S |j	
|j}t d }	t|	}
tj|
|tjd}t|dkrk|d  |7  < |	||_	d|v rkt|j	|d< |S )z,
        Post-process the response.
        INSERT_BEFORE)flags   zContent-Length)r9   r6   generate_statsgenerate_server_timingrender_toolbarget_headersitemsheadersr   contentdecodecharsetr&   r'   reescapesplit
IGNORECASElenr   )r1   r   r>   r<   r=   renderedheadervaluerL   insert_beforepatternbitsr   r   r   r;      s$   
z#DebugToolbarMiddleware._postprocesspanelsc                 C   sR   i }|D ]"}| |  D ]\}}||v r!||  d| 7  < q|||< qq|S )Nz, )rI   rJ   )r   rZ   rK   r=   rU   rV   r   r   r   rI      s   
z"DebugToolbarMiddleware.get_headersN)__name__
__module____qualname____doc__sync_capableasync_capabler   r2   r	   r
   r?   r4   r   r;   staticmethodlistr   dictr)   rI   r   r   r   r   r/   l   s$    
#(r/   )%r^   rO   r   collections.abcr   	functoolsr   asgiref.syncr   r   r   r   django.confr   django.httpr	   r
   django.utils.module_loadingr   debug_toolbarr&   debug_toolbar._stubsr   debug_toolbar.panelsr   debug_toolbar.toolbarr   debug_toolbar.utilsr   r   _HTML_TYPESboolr   r$   r+   r.   r/   r   r   r   r   <module>   s*    