o
    ‚ó·iJ  ã                   @   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__© úkC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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 )NZ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.)
Z_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)
Z_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   ZUSE_TZÚdatetimeÚtimezoneÚutcÚfromtimestamp)r   Z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.
        Z_session_expiry)ÚsecondsN)Úgetr,   r(   Ú	timedeltaZ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)Zexpiryr   )Úopenr$   ÚreadÚdecodeÚEOFErrorr	   Ú
isinstanceÚloggingÚ	getLoggerr   Ú__name__ÚwarningÚstrÚcreateZget_expiry_ager1   ÚdeleteÚOSErrorÚ_session_key)r   r0   Úsession_fileZ	file_dataÚeÚloggerZ
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)Z_get_new_session_keyrA   Úsaver   Úmodified©r   r   r   r   r>   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)Zno_loadÚO_BINARYr   Z_out_)ÚdirÚprefixFT)r   r>   Z_get_sessionr$   r   ÚO_WRONLYr   ÚO_EXCLÚO_CREATr4   ÚcloseÚFileNotFoundErrorr   ÚFileExistsErrorr   r   Úsplitr   ÚmkstempÚwriteÚencodeÚshutilÚmoveÚunlinkr7   r@   )r   rF   r0   Zsession_file_nameÚflagsÚfdrK   rL   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rG   u   sN   
ÿÿÿ
ÿ
ÿÿÿzSessionStore.savec                 C   s   t j |  |¡¡S r   )r   r   Úexistsr$   r   r   r   r   r\   ´   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   rY   r$   r@   r   r   r   r   r?   ·   s   
ÿzSessionStore.deletec                 C   ó   d S r   r   rI   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   r]   r   r   r   r   r   r   Ú<lambda>Ñ   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   ÚlistdirÚ
startswithÚlenr>   rE   )r    r   r   rB   r   Úsessionr   r   r   Úclear_expiredÄ   s   


÷zSessionStore.clear_expiredr   )F)r;   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r$   r,   r1   rE   r>   rG   r\   r?   r^   rd   Ú__classcell__r   r   r   r   r
      s     

	

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