o
    <g                      @   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 Zdd Zd'ddZd'ddZdd Zdd Zd&ddZd&d d!Ze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__ X/var/www/html/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_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.)
_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                    
   |   S r   )rP   r   r   r   r   aloadk      zSessionStore.aloadc                 C   s:   	 |   | _z| jdd W n	 ty   Y q w d| _d S )NTmust_create)_get_new_session_keyrJ   saver   modifiedrR   r   r   r   rF   n   s   
zSessionStore.createc                    rQ   r   )rF   rR   r   r   r   acreatex   rT   zSessionStore.acreateFc              	   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)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   rV   r7   session_file_nameflagsfdr^   r_   output_file_fdoutput_file_namerenamedr   r   r   rX   {   sN   


zSessionStore.savec                       | j |dS )NrU   )rX   )r   rV   r   r   r   asave      zSessionStore.asavec                 C   s   t j| |S r   )r   r    existsr'   r   r   r   r   rw      s   zSessionStore.existsc                    s   |  |S r   )rw   r   r   r   r   aexists   s   
zSessionStore.aexistsc                 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   rm   r'   rI   r   r   r   r   rH      s   
zSessionStore.deletec                    rt   )N)r   )rH   r   r   r   r   adelete   rv   zSessionStore.adeletec                 C   sR   |   }tj}t|D ]}||sq||}| |}dd |_|  qd S )Nc                   S   s   d S r   r   r   r   r   r   <lambda>   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   listdir
startswithremoveprefixrF   rP   )r"   r   r   rK   r   sessionr   r   r   clear_expired   s   



zSessionStore.clear_expiredc                    s   |    d S r   )r   )r"   r   r   r   aclear_expired   s   zSessionStore.aclear_expiredr   )F)rC   
__module____qualname____doc__r   classmethodr   r'   r1   r8   rP   rS   rF   rZ   rX   ru   rw   rx   rH   ry   r   r   __classcell__r   r   r   r   r
      s,    

	


?



r
   )r,   rA   r   rk   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    