o
    `oi                     @   s   d Z ddl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 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mZ ddlmZmZ G dd dZdd Z dS )zA
The main DebugToolbar class that loads and renders the Toolbar.
    N)OrderedDict)	lru_cache)apps)ImproperlyConfigured)Signal)TemplateSyntaxError)render_to_string)pathresolve)Resolver404import_string)get_languageoverride)APP_NAMEsettingsc                   @   s   e Zd Ze Zdd Zedd Zedd Zdd Z	d	d
 Z
dd Ze Zdd Zedd ZdZedd ZdZedd Zedd Zeedddd ZdS )DebugToolbarc                 C   s   || _ t  | _g }t|  D ]}|| |}|| |jr$|j	}q|| _	t
 | _|r:| }|| j|j< |s.i | _i | _d | _| jj|| d d S )N)toolbar)requestdt_settings
get_configcopyconfigreversedget_panel_classesappendenabledprocess_requestr   _panelspoppanel_idstatsZserver_timing_statsstore_id_createdsend)selfr   Zget_responsepanelspanel_classpanel r)   ]/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/site-packages/debug_toolbar/toolbar.py__init__   s&   

zDebugToolbar.__init__c                 C   s   t | j S )z5
        Get a list of all available panels.
        )listr   valuesr%   r)   r)   r*   r&   2   s   zDebugToolbar.panelsc                 C   s   dd | j  D S )zG
        Get a list of panels enabled for the current request.
        c                 S   s   g | ]}|j r|qS r)   )r   ).0r(   r)   r)   r*   
<listcomp>>   s    z/DebugToolbar.enabled_panels.<locals>.<listcomp>)r   r-   r.   r)   r)   r*   enabled_panels9   s   zDebugToolbar.enabled_panelsc                 C   s
   | j | S )zV
        Get the panel with the given id, which is the class name by default.
        )r   )r%   r    r)   r)   r*   get_panel_by_id@   s   
zDebugToolbar.get_panel_by_idc                 C   s   |   s|   z)d| i}| jd pt }t| td|W  d   W S 1 s*w   Y  W dS  tyC   tdsBt	dd w )zA
        Renders the overall Toolbar with panels inside.
        r   ZTOOLBAR_LANGUAGEzdebug_toolbar/base.htmlNzdjango.contrib.staticfileszThe debug toolbar requires the staticfiles contrib app. Add 'django.contrib.staticfiles' to INSTALLED_APPS and define STATIC_URL in your settings.)
should_render_panelsstorer   r   lang_overrider   r   r   is_installedr   )r%   contextlangr)   r)   r*   render_toolbarH   s"   
(
zDebugToolbar.render_toolbarc                 C   s&   | j d  }du r| jjdd}|S )zDetermine whether the panels should be rendered during the request

        If False, the panels will be loaded via Ajax.
        ZRENDER_PANELSNzwsgi.multiprocessT)r   r   ZMETAget)r%   Zrender_panelsr)   r)   r*   r3   ]   s   z!DebugToolbar.should_render_panelsc                 C   sP   | j rd S t j| _ | | j| j < t| jd t| jD ]	}| jjdd qd S )NZRESULTS_CACHE_SIZEF)last)	r"   uuiduuid4hex_storeranger   lenpopitem)r%   _r)   r)   r*   r4   o   s   zDebugToolbar.storec                 C   s   | j |S )N)r?   r:   )clsr"   r)   r)   r*   fetchx   s   zDebugToolbar.fetchNc                 C   s(   | j d u rdd t D }|| _ | j S )Nc                 S   s   g | ]}t |qS r)   r   )r/   Z
panel_pathr)   r)   r*   r0      s    z2DebugToolbar.get_panel_classes.<locals>.<listcomp>)_panel_classesr   Z
get_panels)rD   Zpanel_classesr)   r)   r*   r      s   
zDebugToolbar.get_panel_classesc                 C   sN   | j d u r$ddlm} td|jddg}|  D ]}|| 7 }q|| _ | j S )N   )viewszrender_panel/render_panel)name)_urlpatterns rH   r	   rI   r   get_urls)rD   rH   Zurlpatternsr'   r)   r)   r*   rM      s   
zDebugToolbar.get_urlsc                 C   sH   z|j pt|jt|dd}W n
 ty   Y dS w |jo#|jd tkS )zF
        Determine if the request is for a DebugToolbar view.
        ZurlconfNF)resolver_matchr
   r	   getattrr   
namespacesr   )rD   r   rO   r)   r)   r*   is_toolbar_request   s   zDebugToolbar.is_toolbar_request)maxsizec                  C   s"   t  d } t| trt| S | S )NZOBSERVE_REQUEST_CALLBACK)r   r   
isinstancestrr   )Zfunc_or_pathr)   r)   r*   get_observe_request   s   
z DebugToolbar.get_observe_request)__name__
__module____qualname__r   r#   r+   propertyr&   r1   r2   r9   r3   r   r?   r4   classmethodrE   rF   r   rK   rM   rR   staticmethodr   rV   r)   r)   r)   r*   r      s2    

	

	

r   c                 C   s   t |  S )zM
    Determine whether to update the toolbar from a client side request.
    )r   rR   )r   r)   r)   r*   observe_request   s   r]   )!__doc__r<   collectionsr   	functoolsr   Zdjango.appsr   Zdjango.core.exceptionsr   Zdjango.dispatchr   Zdjango.templater   Zdjango.template.loaderr   Zdjango.urlsr	   r
   Zdjango.urls.exceptionsr   Zdjango.utils.module_loadingr   Zdjango.utils.translationr   r   r5   Zdebug_toolbarr   r   r   r   r]   r)   r)   r)   r*   <module>   s"     #