o
    ^"<f<                     @   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__ \/var/www/html/kck/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   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|  d S )zS
        Return the expiry time of the file storing the session's content.
        _session_expiry)seconds)getr3   r-   	timedelta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)expiryr   )openr(   readdecodeEOFErrorr	   
isinstancelogging	getLoggerr   __name__warningstrcreateget_expiry_ager:   deleteOSError_session_key)r   r9   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_keyrL   saver   modifiedr   r   r   r   rH   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)no_loadO_BINARYr   _out_)dirprefixFT)r   rH   _get_sessionr(   r    O_WRONLYr   O_EXCLO_CREATr>   closeFileNotFoundErrorr   FileExistsErrorr   r!   splitr   mkstempwriteencodeshutilmoveunlinkrA   rK   )r   rS   r9   session_file_nameflagsfdr[   r\   output_file_fdoutput_file_namerenamedr   r   r   rU   o   sJ   

zSessionStore.savec                 C   s   t j| |S r   )r    r!   existsr(   r   r   r   r   rq      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    rj   r(   rK   r   r   r   r   rJ      s   
zSessionStore.deletec                 C      d S r   r   rW   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   rr   r   r   r   r   r   r   <lambda>   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r    listdir
startswithlenrH   rR   )r#   r   r   rM   r   sessionr   r   r   clear_expired   s   


zSessionStore.clear_expiredr   )F)rE   
__module____qualname____doc__r   classmethodr   r(   r3   r:   rR   rH   rU   rq   rJ   rs   ry   __classcell__r   r   r   r   r      s     




=

r   )r-   rC   r    rh   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    