o
    >h                     @   s   d Z ddlZddl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 ddlmZ ddlmZ ddlmZmZ d	d
 Zedd Zdd ZG dd dZdS )z
Debug Toolbar middleware
    N)cache)async_to_synciscoroutinefunctionmarkcoroutinefunctionsync_to_async)settings)import_string)DebugToolbar)clear_stack_trace_cachesis_processable_html_responsec                 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 )	zT
    Default function to determine whether to show the toolbar on a given page.
    FREMOTE_ADDRT.zhost.docker.internalNz.1)
r   DEBUGMETAgetINTERNAL_IPSjoinsocketgethostbynamersplitgaierror)request	docker_ip r   x/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/debug_toolbar/middleware.pyshow_toolbar   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_path6   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_toolbarF   s   r&   c                   @   sD   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	e
dd ZdS )DebugToolbarMiddlewarezm
    Middleware to set up Debug Toolbar on incoming request and render toolbar
    on outgoing response.
    Tc                 C   s(   || _ t| j | _| jrt|  d S d S )N)get_responser   r$   r   )selfr(   r   r   r   __init__`   s
   zDebugToolbarMiddleware.__init__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_requestr(   enabled_panelsenable_instrumentationprocess_requestr
   reverseddisable_instrumentation_postprocess)r)   r   r   toolbarpanelresponser   r   r   __call__k   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 )Nr+   aenable_instrumentation)r&   r$   r	   r-   r(   r.   hasattrr8   r/   r0   r
   r1   r2   r3   )r)   r   r   r6   r4   r5   r   r   r   r,      s*   




z DebugToolbarMiddleware.__acall__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)r1   r.   generate_statsgenerate_server_timingrender_toolbarget_headersitemsheadersr   contentdecodecharsetr   r   reescapesplit
IGNORECASElenr   )r)   r   r6   r4   r5   renderedheadervaluerD   insert_beforepatternbitsr   r   r   r3      s$   
z#DebugToolbarMiddleware._postprocessc                 C   sR   i }|D ]"}| |  D ]\}}||v r!||  d| 7  < q|||< qq|S )Nz, )rA   rB   )r   panelsrC   r5   rM   rN   r   r   r   rA      s   
z"DebugToolbarMiddleware.get_headersN)__name__
__module____qualname____doc__sync_capableasync_capabler*   r7   r,   r3   staticmethodrA   r   r   r   r   r'   W   s    !r'   )rV   rG   r   	functoolsr   asgiref.syncr   r   r   r   django.confr   django.utils.module_loadingr   debug_toolbarr   debug_toolbar.toolbarr	   debug_toolbar.utilsr
   r   r   r#   r&   r'   r   r   r   r   <module>   s    
