o
    º Žiå  ã                   @  sž   d dl m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 dgZG dd„ deƒZG dd„ deƒZdS )é    )ÚannotationsN)Úbasename)Úurlparse)Úsettings)Úfinders)Ústaticfiles_storage)ÚFileResponse)Ú
WhiteNoise)Úensure_leading_trailing_slashÚWhiteNoiseMiddlewarec                   @  s   e Zd ZdZdd„ ZdS )ÚWhiteNoiseFileResponsea  
    Wrap Django's FileResponse to prevent setting any default headers. For the
    most part these just duplicate work already done by WhiteNoise but in some
    cases (e.g. the content-disposition header introduced in Django 3.0) they
    are actively harmful.
    c                 O  s   d S ©N© )ÚselfÚargsÚkwargsr   r   ú_C:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\whitenoise/middleware.pyÚset_headers   s   z"WhiteNoiseFileResponse.set_headersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                      sf   e Zd ZdZdef‡ fdd„	Zdd„ Zedd„ ƒZd	d
„ Z	‡ fdd„Z
dd„ Zdd„ Zdd„ Z‡  ZS )r   zh
    Wrap WhiteNoise to allow it to function as Django middleware, rather
    than WSGI middleware.
    Nc                   sR  || _ z|j}W n ty   |j}Y nw z|j}W n ty+   |jr'd}nd}Y nw z|j}W n ty<   d}Y nw z|j}W n tyM   d}Y nw z|j}W n ty^   d }Y nw z|j}W n tyo   d }Y nw z|j	}	W n ty€   d }	Y nw z|j
}
W n ty‘   d }
Y nw tƒ jd |||||||	|
d�	 z|j| _W n ty´   |j| _Y nw z|j| _W n+ tyæ   t|jpÇdƒj| _|jrä|j d¡}| j |¡rä| jt|ƒd … | _Y nw t| jƒ| _|j| _| jrý| j| j| jd� z|j}W n t�y   d }Y nw |�r|  |¡ | j�r%| j�s'|  ¡  d S d S d S )	Nr   é<   Tzutf-8)	ZapplicationÚautorefreshÚmax_ageÚallow_all_originsÚcharsetÚ	mimetypesÚadd_headers_functionÚ
index_fileÚimmutable_file_testÚ ú/)Úprefix)Úget_responseZWHITENOISE_AUTOREFRESHÚAttributeErrorÚDEBUGZWHITENOISE_MAX_AGEZWHITENOISE_ALLOW_ALL_ORIGINSZWHITENOISE_CHARSETZWHITENOISE_MIMETYPESZWHITENOISE_ADD_HEADERS_FUNCTIONZWHITENOISE_INDEX_FILEZWHITENOISE_IMMUTABLE_FILE_TESTÚsuperÚ__init__ZWHITENOISE_USE_FINDERSÚuse_findersZWHITENOISE_STATIC_PREFIXÚstatic_prefixr   Z
STATIC_URLÚpathZFORCE_SCRIPT_NAMEÚrstripÚ
startswithÚlenr
   ZSTATIC_ROOTZstatic_rootZ	add_filesZWHITENOISE_ROOTr   Úadd_files_from_finders)r   r$   r   r   r   r   r   r   r   r   r    Úscript_nameÚroot©Ú	__class__r   r   r(   $   s¨   

ÿ
€ü
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ÷ÿ€û
ÿ
ÿzWhiteNoiseMiddleware.__init__c                 C  s@   | j r
|  |j¡}n| j |j¡}|d ur|  ||¡S |  |¡S r   )r   Z	find_fileZ	path_infoÚfilesÚgetÚserver$   )r   ÚrequestÚstatic_filer   r   r   Ú__call__t   s   
zWhiteNoiseMiddleware.__call__c                 C  sN   |   |j|j¡}t|jƒ}t|jpd|d�}|d= |jD ]\}}|||< q|S )Nr   )Ústatuszcontent-type)r$   ÚmethodZMETAÚintr:   r   ÚfileÚheaders)r8   r7   Úresponser:   Úhttp_responseÚkeyÚvaluer   r   r   r6   }   s   

zWhiteNoiseMiddleware.servec                 C  sª   i }t  ¡ D ]2}| d ¡D ]*\}}t|dd ƒpd d¡}d | j||r%dnd| dd¡f¡}| || 	|¡¡ qqdd„ | 
¡ D ƒ}| ¡ D ]\}}| j|||d� qFd S )Nr#   r!   r"   ú\c                 S  s   i | ]}|t  |¡“qS r   )ÚosÚstat)Ú.0r+   r   r   r   Ú
<dictcomp>—   s    z?WhiteNoiseMiddleware.add_files_from_finders.<locals>.<dictcomp>)Ú
stat_cache)r   Zget_findersÚlistÚgetattrÚstripÚjoinr*   ÚreplaceÚ
setdefaultr+   ÚvaluesÚitemsZadd_file_to_dictionary)r   r4   Úfinderr+   Zstorager#   ÚurlrH   r   r   r   r/   ˆ   s"   

üÿ	õÿz+WhiteNoiseMiddleware.add_files_from_findersc                 #  sV   � | j r| | j¡rt |t| jƒd … ¡}|r|V  tƒ  |¡}|D ]}|V  q#d S r   )r)   r-   r*   r   Úfindr.   r'   Úcandidate_paths_for_url)r   rR   r+   Úpathsr2   r   r   rT   ›   s   €ÿz,WhiteNoiseMiddleware.candidate_paths_for_urlc                 C  sZ   |  | j¡sdS |t| jƒd… }|  |¡}||krdS |  |¡}t|o+t|ƒt|ƒkƒS )z¾
        Determine whether given URL represents an immutable file (i.e. a
        file with a hash of its contents as part of its name) which can
        therefore be cached forever
        FN)r-   r*   r.   Úget_name_without_hashÚget_static_urlÚboolr   )r   r+   rR   ÚnameZname_without_hashZ
static_urlr   r   r   r    ¤   s   

z(WhiteNoiseMiddleware.immutable_file_testc                 C  s(   t j |¡\}}t j |¡d }|| S )aW  
        Removes the version hash from a filename e.g, transforms
        'css/application.f3ea4bcc2.css' into 'css/application.css'

        Note: this is specific to the naming scheme used by Django's
        CachedStaticFilesStorage. You may have to override this if
        you are using a different static files versioning system
        r   N)rD   r+   Úsplitext)r   ÚfilenameZname_with_hashÚextrY   r   r   r   rV   ¶   s   	z*WhiteNoiseMiddleware.get_name_without_hashc                 C  s"   zt  |¡W S  ty   Y d S w r   )r   rR   Ú
ValueError)r   rY   r   r   r   rW   Ã   s
   ÿz#WhiteNoiseMiddleware.get_static_url)r   r   r   r   r   r(   r9   Ústaticmethodr6   r/   rT   r    rV   rW   Ú__classcell__r   r   r2   r   r      s    P	

	)Ú
__future__r   rD   Ú	posixpathr   Úurllib.parser   Zdjango.confr   Zdjango.contrib.staticfilesr   Z"django.contrib.staticfiles.storager   Zdjango.httpr   Zwhitenoise.baser	   Zwhitenoise.string_utilsr
   Ú__all__r   r   r   r   r   r   Ú<module>   s    