
    g                          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e
      Zy)    N)settings)VALID_KEY_CHARSCreateErrorSessionBaseUpdateError)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperationc                        e Zd ZdZd fd	Zed        ZddZd Zd Z	d Z
d Zd	 Zd
 ZddZddZd Zd ZddZddZed        Zed        Z xZS )SessionStorez/
    Implement a file based session store.
    c                 x    | j                         | _        t        j                  | _        t
        |   |       y N)_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuper__init__)selfsession_key	__class__s     }/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/contrib/sessions/backends/file.pyr   zSessionStore.__init__   s/     224#77%    c                     	 | j                   S # t        $ r` t        t        dd       xs t	        j
                         }t        j                  j                  |      st        d|z        || _         |cY S w xY 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   s     r   r   zSessionStore._get_storage_path   s    	 $$$ 	 "5t<U@S@S@U  77==.*68DE  !-C	 s    A&A76A7c                     || j                         }t        |      j                  t              st	        d      t
        j                  j                  | j                  | j                  |z         S )z@
        Get the file associated with this session key.
        z!Invalid characters in session key)
_get_or_create_session_keysetissubsetr   r   r!   r"   joinr   r   r   r   s     r   _key_to_filezSessionStore._key_to_file/   s_     99;K
 ;((9#$GHHww||D--t/?/?+/MNNr   c                     t        j                  | j                               j                  }t        j
                  rt        j                  j                  nd}t        j                  j                  ||      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-   s      r   _last_modificationzSessionStore._last_modification>   sV     wwt0023<<&.ooX""4  ..|.CCr   c                     |j                  d      xs5 | j                         t        j                  | j	                               z   S )zS
        Return the expiry time of the file storing the session's content.
        _session_expiry)seconds)getr6   r1   	timedeltaget_session_cookie_age)r   session_datas     r   _expiry_datezSessionStore._expiry_dateF   sF      12 
##%  )D)D)FGH	
r   c                    i }	 t        | j                         d      5 }|j                         }d d d        rZ	 | j                  |      }| j                  | j                  |            }|dk  r"i }| j!                          | j                          |S # 1 sw Y   gxY w# t        t
        f$ rp}t        |t
              rFt        j                  d|j                  j                  z        }|j                  t        |             | j                          Y d }~d }~ww xY w# t"        t
        f$ r d | _        Y |S w xY 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   r=   session_file	file_dataelogger
expiry_ages          r   loadzSessionStore.loadO   s    	%d'')G<(--/	 = "#';;y#9L "008I8I,8W0X
?#%LKKMKKM / =< !"56 "!!%89!(!2!201;;3G3GG" s1v.KKMM" ,- 	% $D	%sR   D$ B
D$ B" AD$ BD$ "D!1A&DD$ D!!D$ $D>=D>c                 *   K   | j                         S wr   )rW   r   s    r   aloadzSessionStore.aloadk   s     yy{   c                     	 | j                         | _        	 | j                  d       d| _        y # t        $ r Y <w xY w)NTmust_create)_get_new_session_keyrQ   saver   modifiedrY   s    r   rM   zSessionStore.createn   sI     $ 9 9 ;D		d	+ !DM  s   2 	>>c                 *   K   | j                         S wr   )rM   rY   s    r   acreatezSessionStore.acreatex   s     {{}r[   c                    | j                   | j                         S | j                  |      }| j                         }	 t        j
                  t        t        dd      z  }|r$|t        j                  t        j                  z  z  }t	        j                  ||      }t	        j                  |       t        j                  j!                  |      \  }}	 t#        j$                  ||dz         \  }}	d}
	 	 t	        j&                  || j)                  |      j)                                t	        j                  |       t+        j,                  |	|       d}
|
st	        j.                  |	       y y # t        $ r |st        Y t        $ r |rt        Y w xY w# t	        j                  |       w xY w# |
st	        j.                  |	       w w xY w# t0        t2        f$ r Y y w xY w)N)no_loadO_BINARYr   _out_)dirprefixFT)r   rM   _get_sessionr+   r!   O_WRONLYr   O_EXCLO_CREATrC   closeFileNotFoundErrorr   FileExistsErrorr   r"   splitr   mkstempwriteencodeshutilmoveunlinkrF   rP   )r   r^   r=   session_file_nameflagsfdrh   ri   output_file_fdoutput_file_namerenameds              r   r`   zSessionStore.save{   s   #;;=  (((= --/	" KK'"j!"<<ERZZ//*E2BHHRL. ggmm$56V	/7/?/? 00,N, G0-HH^T[[-F-M-M-OPHH^,
 ,.?@II./ S ! 	"!!  	"!! 	"< HH^, II./ '" 		sU    A3E0 G 73F *-F3 G 0FFFF00F3 3GG G"!G"c                 .   K   | j                  |      S w)Nr]   )r`   )r   r^   s     r   asavezSessionStore.asave   s     yy[y11   c                 ^    t         j                  j                  | j                  |            S r   )r!   r"   existsr+   r*   s     r   r   zSessionStore.exists   s     ww~~d//<==r   c                 ,   K   | j                  |      S wr   )r   r*   s     r   aexistszSessionStore.aexists   s     {{;''   c                     || j                   y | j                   }	 t        j                  | j                  |             y # t        $ r Y y w xY wr   )r   r!   rw   r+   rP   r*   s     r   rO   zSessionStore.delete   sP    '**K	IId''45 		s   $A 	AAc                 .   K   | j                  |      S w)N)r   )rO   r*   s     r   adeletezSessionStore.adelete   s     {{{{33r   c                     | j                         }t        j                  }t        j                  |      D ]E  }|j                  |      s|j                  |      } | |      }d |_        |j                          G y )Nc                       y r    r   r   r   <lambda>z,SessionStore.clear_expired.<locals>.<lambda>   s    Tr   )	r   r   r   r!   listdir
startswithremoveprefixrM   rW   )r$   r   r   rR   r   sessions         r   clear_expiredzSessionStore.clear_expired   sk    ,,.22JJ|4L**;7&33K@K+&G *GNLLN 5r   c                 ,   K   | j                          y wr   )r   )r$   s    r   aclear_expiredzSessionStore.aclear_expired   s     r   r   )F)rJ   
__module____qualname____doc__r   classmethodr   r+   r6   r>   rW   rZ   rM   rc   r`   r   r   r   rO   r   r   r   __classcell__)r   s   @r   r   r      s    &
    $OD
8=~2>(4    r   r   )r1   rH   r!   ru   r   django.confr   %django.contrib.sessions.backends.baser   r   r   r   "django.contrib.sessions.exceptionsr   django.core.exceptionsr	   r
   r   r   r   r   <module>r      s8      	      A LP; Pr   