o
    g                     @   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   `/var/www/html/bloggers_ems/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   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    r1   )django.core.files.storager$   r/   ImportErrorr&   r   STATICFILES_STORAGE_wrapped)r   r$   configured_storage_clsr&   r1   r   r   r   _setup+   s   
zDebugConfiguredStorage._setupN)r   r   r    r!   r7   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)_r9   r:   r   r   r   r   titleP   s   zStaticFilesPanel.titlec                    s"   t  j|i | d| _g | _d S )Nr   )r-   r   r9   
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 )Nr:   )	get_stats)r   statsr   r   r   r:   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 usedr:   )r:   r   )r   r:   r   r   r   nav_subtitlei   s   zStaticFilesPanel.nav_subtitlec                    s2   t g }t |}t   | _t | |S r   )r*   setr-   process_requestr+   copyr=   reset)r   requestreset_tokenresponser.   r   r   rG   p   s
   

z StaticFilesPanel.process_requestc              	   C   s2   |  | jt| j| j|  |  |  d d S )N)r9   r:   r%   staticfiles_appsstaticfiles_dirsstaticfiles_finders)record_statsr9   lenr=   get_staticfiles_appsget_staticfiles_dirsget_staticfiles_finders)r   rJ   rL   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   rV   r/   r   r   r   
setdefaultr,   r9   OSError)
r   finders_mappingfinderr   finder_storageprefixed_path
finder_clsfinder_pathr   payloadr   r   r   rT      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   ).0rV   dirr   r   r   
<listcomp>   s    z9StaticFilesPanel.get_staticfiles_dirs.<locals>.<listcomp>)r   rY   
isinstanceFileSystemFinderextend	locations)r   dirsr_   r   r   r   rS      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   rY   rh   AppDirectoriesFinderappsr,   )r   rn   r_   appr   r   r   rR      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   rY   rZ   r   r]   r,   r   )clserrorsr_   r   r`   r   r   r   
run_checks   s    	zStaticFilesPanel.run_checks)r   r   r    r!   nametemplatepropertyr<   r   r@   rB   r:   r;   	nav_titlerE   rG   rU   rT   rS   rR   classmethodrt   r0   r   r   r.   r   r8   H   s*    



r8   )r'   contextvarsr   os.pathr   r   django.confr   django.contrib.staticfilesr   r   django.core.checksr   django.utils.functionalr	   django.utils.translationr
   r;   r   debug_toolbarr   r   r*   r#   r   rA   Panelr8   r   r   r   r   <module>   s    !