o
    `oi                     @   s   d dl 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 d dlmZ d dlmZmZ d d	lmZ G d
d dZedZG dd deZe
jZG dd dejZdS )    N)
ContextVar)joinnormpath)settings)findersstorage)Warning)
LazyObject)gettext_lazyngettext)panelsc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
StaticFilezA
    Representing the different properties of a static file.
    c                 C   s
   || _ d S Npathselfr    r   h/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/site-packages/debug_toolbar/panels/staticfiles.py__init__      
zStaticFile.__init__c                 C   s   | j S r   r   r   r   r   r   __str__   s   zStaticFile.__str__c                 C   s   t | jS r   )r   findr   r   r   r   r   	real_path      zStaticFile.real_pathc                 C   s   t j| jS r   )r   staticfiles_storageurlr   r   r   r   r   r      s   zStaticFile.urlN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s    r   Zdjdt_static_used_static_filesc                   @   s   e Zd ZdZdd ZdS )DebugConfiguredStoragez
    A staticfiles storage class to be used for collecting which paths
    are resolved by using the {% static %} template tag (which uses the
    `url` method).
    c                 C   s`   zddl m} |d j}W n ty!   ddl m} |tj}Y nw G dd d|}| | _d S )Nr   )storagesstaticfiles)get_storage_classc                       s   e Zd Z fddZ  ZS )z>DebugConfiguredStorage._setup.<locals>.DebugStaticFilesStoragec                    sH   t t t t| W d    n1 sw   Y  t |S r   )	
contextlibsuppressLookupErrorused_static_filesgetappendr   superr   r   	__class__r   r   r   9   s   zBDebugConfiguredStorage._setup.<locals>.DebugStaticFilesStorage.url)r   r   r    r   __classcell__r   r   r-   r   DebugStaticFilesStorage8   s    r0   )Zdjango.core.files.storager#   r.   ImportErrorr%   r   ZSTATICFILES_STORAGEZ_wrapped)r   r#   Zconfigured_storage_clsr%   r0   r   r   r   _setup+   s   
zDebugConfiguredStorage._setupN)r   r   r    r!   r2   r   r   r   r   r"   $   s    r"   c                       s   e Zd ZdZdZdZedd Z fddZdd	 Z	d
d Z
edd ZedZedd Z fddZdd Zdd Zdd Zdd Zedd Z  ZS )StaticFilesPanelz3
    A panel to display the found staticfiles.
    zStatic filesz%debug_toolbar/panels/staticfiles.htmlc                 C   s   t d| j| jd S )Nz5Static files (%(num_found)s found, %(num_used)s used))	num_foundnum_used)_r4   r5   r   r   r   r   titleP   s   zStaticFilesPanel.titlec                    s"   t  j|i | d| _g | _d S )Nr   )r,   r   r4   
used_paths)r   argskwargsr-   r   r   r   W   s   
zStaticFilesPanel.__init__c                 C   s   t  t_d S r   )r"   r   r   r   r   r   r   enable_instrumentation\   r   z'StaticFilesPanel.enable_instrumentationc                 C   s
   t t_d S r   )_original_storager   r   r   r   r   r   disable_instrumentation_   r   z(StaticFilesPanel.disable_instrumentationc                 C   s   |   }|o	|d S )Nr5   )Z	get_stats)r   statsr   r   r   r5   b   s   zStaticFilesPanel.num_usedc                 C   s   | j }tdd|d|i S )Nz%(num_used)s file usedz%(num_used)s files usedr5   )r5   r   )r   r5   r   r   r   nav_subtitlei   s   zStaticFilesPanel.nav_subtitlec                    s2   t g }t |}t   | _t | |S r   )r)   setr,   process_requestr*   copyr8   reset)r   requestZreset_tokenresponser-   r   r   rA   p   s
   

z StaticFilesPanel.process_requestc              	   C   s2   |  | jt| j| j|  |  |  d d S )N)r4   r5   r$   Zstaticfiles_appsZstaticfiles_dirsZstaticfiles_finders)Zrecord_statsr4   lenr8   get_staticfiles_appsget_staticfiles_dirsget_staticfiles_finders)r   rD   rE   r   r   r   generate_stats{   s   zStaticFilesPanel.generate_statsc           
   	   C   s   i }t  D ]K}z@|g D ]8\}}t|ddrt|j|}n|}|j}d|j|jg}|	|}||f}	|
|g |	 |  jd7  _qW q tyQ   Y qw |S )z
        Returns a sorted mapping between the finder path and the list
        of relative and file system paths which that finder was able
        to find.
        prefixN.   )r   get_finderslistgetattrr   rK   r.   r   r   r   
setdefaultr+   r4   OSError)
r   Zfinders_mappingfinderr   finder_storageZprefixed_pathZ
finder_clsZfinder_pathr   payloadr   r   r   rI      s$   
z(StaticFilesPanel.get_staticfiles_findersc                 C   s8   g }t  D ]}t|t jr||j qdd |D S )zP
        Returns a list of paths to inspect for additional static files
        c                 S   s   g | ]
\}}|t |fqS r   )r   ).0rK   dirr   r   r   
<listcomp>   s    z9StaticFilesPanel.get_staticfiles_dirs.<locals>.<listcomp>)r   rN   
isinstanceZFileSystemFinderextend	locations)r   dirsrS   r   r   r   rH      s   z%StaticFilesPanel.get_staticfiles_dirsc                 C   s@   g }t  D ]}t|t jr|jD ]}||vr|| qq|S )zJ
        Returns a list of app paths that have a static directory
        )r   rN   rY   ZAppDirectoriesFinderappsr+   )r   r]   rS   Zappr   r   r   rG      s   

z%StaticFilesPanel.get_staticfiles_appsc                 C   s^   g }t  D ]&}z|g D ]	\}}|| qW q ty,   |tdddd Y qw |S )z
        Check that the integration is configured correctly for the panel.

        Specifically look for static files that haven't been collected yet.

        Return a list of :class: `django.core.checks.CheckMessage` instances.
        zAdebug_toolbar requires the STATICFILES_DIRS directories to exist.z;Running manage.py collectstatic may help uncover the issue.zdebug_toolbar.staticfiles.W001)hintid)r   rN   rO   r   rR   r+   r   )clserrorsrS   r   rT   r   r   r   
run_checks   s    	zStaticFilesPanel.run_checks)r   r   r    r!   nametemplatepropertyr7   r   r;   r=   r5   r6   Z	nav_titler?   rA   rJ   rI   rH   rG   classmethodrb   r/   r   r   r-   r   r3   H   s*    



r3   )r&   Zcontextvarsr   Zos.pathr   r   Zdjango.confr   Zdjango.contrib.staticfilesr   r   Zdjango.core.checksr   Zdjango.utils.functionalr	   Zdjango.utils.translationr
   r6   r   Zdebug_toolbarr   r   r)   r"   r   r<   Panelr3   r   r   r   r   <module>   s    !