o
    uvXjN  ã                   @   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 d dlmZ G dd„ de
ƒZdS )	é    N)Úsettings)ÚVALID_KEY_CHARSÚCreateErrorÚSessionBaseÚUpdateError)ÚInvalidSessionKey)ÚImproperlyConfiguredÚSuspiciousOperation)Útimezonec                       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 | ƒ ¡ | _tj| _tƒ  |¡ d S ©N)ÚtypeÚ_get_storage_pathÚstorage_pathr   ÚSESSION_COOKIE_NAMEÚfile_prefixÚsuperÚ__init__©ÚselfÚsession_key©Ú	__class__© úb/var/www/html/myproject/venv/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   s<   t  |  ¡ ¡j}tjrtj |¡}|jt	j
d�S tj |¡S )zY
        Return the modification time of the file storing the session's content.
        )Útzinfo)r!   Ústatr)   Úst_mtimer   ÚUSE_TZÚdatetimeÚutcfromtimestampÚreplacer
   ÚutcÚfromtimestamp)r   Úmodificationr   r   r   Ú_last_modification9   s
   zSessionStore._last_modificationc                 C   s    |  d¡p|  ¡ tjtjd� S )zS
        Return the expiry time of the file storing the session's content.
        Ú_session_expiry)Úseconds)Úgetr4   r.   Ú	timedeltar   ÚSESSION_COOKIE_AGE)r   Úsession_datar   r   r   Ú_expiry_dateC   s   
ÿzSessionStore._expiry_datec              
   C   s  i }ztt |  ¡ ddd��}| ¡ }W d   ƒ n1 sw   Y  |rqz|  |¡}W n- ttfyV } zt|tƒrHt d|j	j
 ¡}| t|ƒ¡ |  ¡  W Y d }~nd }~ww | j|  |¡d�}|dkrti }|  ¡  |  ¡  W |S W |S W |S  ttfy…   d | _Y |S w )NÚrÚascii)Úencodingzdjango.security.%s)Úexpiryr   )Úopenr)   ÚreadÚdecodeÚEOFErrorr	   Ú
isinstanceÚloggingÚ	getLoggerr   Ú__name__ÚwarningÚstrÚcreateÚget_expiry_ager;   ÚdeleteÚIOErrorÚ_session_key)r   r:   Úsession_fileÚ	file_dataÚeÚloggerÚ
expiry_ager   r   r   ÚloadK   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_keyrN   Úsaver   Úmodified©r   r   r   r   rJ   e   s   
ÿzSessionStore.createFc              
   C   sR  | 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tfy¨   Y d S w )N)Úno_loadÚO_BINARYr   Ú_out_)ÚdirÚprefixFT)r   rJ   Ú_get_sessionr)   r!   ÚO_WRONLYr   ÚO_EXCLÚO_CREATr@   ÚcloseÚFileNotFoundErrorr   ÚFileExistsErrorr   r"   Úsplitr   ÚmkstempÚwriteÚencodeÚshutilÚmoveÚunlinkÚOSErrorrM   rC   )r   rU   r:   Úsession_file_nameÚflagsÚfdr]   r^   Úoutput_file_fdÚoutput_file_nameÚrenamedr   r   r   rW   o   sJ   
ÿÿÿ
ÿÿÿzSessionStore.savec                 C   s   t j |  |¡¡S r   )r!   r"   Úexistsr)   r   r   r   r   rt   ¬   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!   rl   r)   rm   r   r   r   r   rL   ¯   s   
ÿzSessionStore.deletec                 C   ó   d S r   r   rY   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   ru   r   r   r   r   r   r   Ú<lambda>É   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r!   ÚlistdirÚ
startswithÚlenrJ   rT   )r$   r   r   rO   r   Úsessionr   r   r   Úclear_expired¼   s   


÷zSessionStore.clear_expiredr   )F)rG   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r)   r4   r;   rT   rJ   rW   rt   rL   rv   r|   Ú__classcell__r   r   r   r   r      s     




=

r   )r.   rE   r!   rj   r   Údjango.confr   Ú%django.contrib.sessions.backends.baser   r   r   r   Ú"django.contrib.sessions.exceptionsr   Údjango.core.exceptionsr   r	   Údjango.utilsr
   r   r   r   r   r   Ú<module>   s    