o
    b<                     @   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j| _t | d S N)_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuper__init__selfsession_key	__class__ d/var/www/secure340b-portal/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 )NZSESSION_FILE_PATHzThe 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   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   ZUSE_TZdatetimeutcfromtimestampreplacer
   utcfromtimestamp)r   Zmodificationr   r   r   _last_modification9   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)seconds)getr.   r)   	timedeltaZget_session_cookie_age)r   session_datar   r   r   _expiry_dateC   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_ager3   deleteOSError_session_key)r   r2   session_fileZ	file_dataeloggerZ
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)Z_get_new_session_keyrC   saver   modifiedr   r   r   r   r@   e   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 |
st|	 W d S W d S |
st|	 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_CREATr6   closeFileNotFoundErrorr   FileExistsErrorr   r   splitr   mkstempwriteencodeshutilmoveunlinkr9   rB   )r   rH   r2   Zsession_file_nameflagsfdrM   rN   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rI   o   sJ   

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   r[   r%   rB   r   r   r   r   rA      s   
zSessionStore.deletec                 C      d S r   r   rK   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@   rG   )r!   r   r   rD   r   sessionr   r   r   clear_expired   s   


zSessionStore.clear_expiredr   )F)r=   
__module____qualname____doc__r   classmethodr   r%   r.   r3   rG   r@   rI   r^   rA   r`   rf   __classcell__r   r   r   r   r      s     




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