o
    ˜¨ÊhJ  ã                   @   s|   d dl Z d dlZd dl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mZ G dd„ de
ƒZdS )é    N)Úsettings)ÚVALID_KEY_CHARSÚCreateErrorÚSessionBaseÚUpdateError)ÚInvalidSessionKey)ÚImproperlyConfiguredÚSuspiciousOperationc                       sˆ   e Zd ZdZd‡ fdd„	Zedd„ ƒZddd„Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zddd„Zdd„ Zddd„Zdd„ Zedd„ ƒZ‡  ZS )ÚSessionStorez/
    Implement a file based session store.
    Nc                    s"   |   ¡ | _tj| _tƒ  |¡ d S ©N)Ú_get_storage_pathÚstorage_pathr   ÚSESSION_COOKIE_NAMEÚfile_prefixÚsuperÚ__init__©ÚselfÚsession_key©Ú	__class__© úW/var/www/html/env/lib/python3.10/site-packages/django/contrib/sessions/backends/file.pyr      s   
zSessionStore.__init__c                 C   sR   z| j W S  ty(   ttdd ƒpt ¡ }tj |¡s!t	d| ƒ‚|| _ | Y S w )NÚSESSION_FILE_PATHz•The session storage path %r doesn't exist. Please set your SESSION_FILE_PATH setting to an existing directory in which Django can store session data.)
Ú_storage_pathÚAttributeErrorÚgetattrr   ÚtempfileÚ
gettempdirÚosÚpathÚisdirr   )Úclsr   r   r   r   r      s   ÿþÿózSessionStore._get_storage_pathc                 C   s<   |du r|   ¡ }t|ƒ t¡stdƒ‚tj | j| j	| ¡S )z@
        Get the file associated with this session key.
        Nz!Invalid characters in session key)
Ú_get_or_create_session_keyÚsetÚissubsetr   r   r   r    Újoinr   r   r   r   r   r   Ú_key_to_file/   s
   zSessionStore._key_to_filec                 C   s2   t  |  ¡ ¡j}tjrtjjnd}tjj	||d�S )zY
        Return the modification time of the file storing the session's content.
        N)Útz)
r   Ústatr'   Úst_mtimer   ÚUSE_TZÚdatetimeÚtimezoneÚutcÚfromtimestamp)r   Úmodificationr(   r   r   r   Ú_last_modification>   s   zSessionStore._last_modificationc                 C   s"   |  d¡p|  ¡ tj|  ¡ d� S )zS
        Return the expiry time of the file storing the session's content.
        Ú_session_expiry)Úseconds)Úgetr1   r,   Ú	timedeltaÚget_session_cookie_age)r   Úsession_datar   r   r   Ú_expiry_dateF   s
   
ÿÿzSessionStore._expiry_datec              
   C   s
  i }zst |  ¡ dd��}| ¡ }W d   ƒ n1 sw   Y  |rpz|  |¡}W n- ttfyU } zt|tƒrGt d|j	j
 ¡}| t|ƒ¡ |  ¡  W Y d }~nd }~ww | j|  |¡d�}|dkrsi }|  ¡  |  ¡  W |S W |S W |S  ttfy„   d | _Y |S w )NÚascii)Úencodingzdjango.security.%s)Úexpiryr   )Úopenr'   ÚreadÚdecodeÚEOFErrorr	   Ú
isinstanceÚloggingÚ	getLoggerr   Ú__name__ÚwarningÚstrÚcreateÚget_expiry_ager8   ÚdeleteÚOSErrorÚ_session_key)r   r7   Úsession_fileÚ	file_dataÚeÚloggerÚ
expiry_ager   r   r   ÚloadO   s>   
ÿ

ÿ€ú	
íúþþzSessionStore.loadc                 C   s:   	 |   ¡ | _z| jdd� W n	 ty   Y q w d| _d S )NT)Úmust_create)Ú_get_new_session_keyrJ   Úsaver   Úmodified©r   r   r   r   rF   k   s   
ÿzSessionStore.createFc              	   C   sP  | j d u r	|  ¡ S | j|d�}|  ¡ }z tjttddƒB }|r'|tjtjB O }t 	||¡}t 
|¡ W n ty@   |s>t‚Y n tyL   |rJt‚Y nw tj |¡\}}zFtj||d d�\}}	d}
z/zt ||  |¡ ¡ ¡ W t 
|¡ nt 
|¡ w t |	|¡ d}
W |
s�t |	¡ W d S W d S |
s›t |	¡ w w  ttfy§   Y d S w )N)Úno_loadÚO_BINARYr   Ú_out_)ÚdirÚprefixFT)r   rF   Ú_get_sessionr'   r   ÚO_WRONLYr   ÚO_EXCLÚO_CREATr<   ÚcloseÚFileNotFoundErrorr   ÚFileExistsErrorr   r    Úsplitr   ÚmkstempÚwriteÚencodeÚshutilÚmoveÚunlinkr?   rI   )r   rQ   r7   Úsession_file_nameÚflagsÚfdrY   rZ   Úoutput_file_fdÚoutput_file_nameÚrenamedr   r   r   rS   u   sN   
ÿÿÿ
ÿ
ÿÿÿzSessionStore.savec                 C   s   t j |  |¡¡S r   )r   r    Úexistsr'   r   r   r   r   ro   ´   s   zSessionStore.existsc                 C   sH   |d u r| j d u rd S | j }zt |  |¡¡ W d S  ty#   Y d S w r   )r   r   rh   r'   rI   r   r   r   r   rH   ·   s   
ÿzSessionStore.deletec                 C   ó   d S r   r   rU   r   r   r   ÚcleanÁ   s   zSessionStore.cleanc                 C   sX   |   ¡ }tj}t |¡D ]}| |¡sq|t|ƒd … }| |ƒ}dd„ |_| ¡  qd S )Nc                   S   rp   r   r   r   r   r   r   Ú<lambda>Ñ   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   ÚlistdirÚ
startswithÚlenrF   rP   )r"   r   r   rK   r   Úsessionr   r   r   Úclear_expiredÄ   s   


÷zSessionStore.clear_expiredr   )F)rC   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r'   r1   r8   rP   rF   rS   ro   rH   rq   rw   Ú__classcell__r   r   r   r   r
      s     

	

?

r
   )r,   rA   r   rf   r   Údjango.confr   Ú%django.contrib.sessions.backends.baser   r   r   r   Ú"django.contrib.sessions.exceptionsr   Údjango.core.exceptionsr   r	   r
   r   r   r   r   Ú<module>   s    