
    Hwg-                         d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ ej                  ej                  z   Z G d d	e      Z G d
 de      Z G d d      Zy)    N)datetime	timedelta)settings)signing)timezone)get_random_string)import_stringc                       e Zd ZdZy)CreateErrorz
    Used internally as a consistent exception type to catch from save (see the
    docstring for SessionBase.save() for details).
    N__name__
__module____qualname____doc__     \/var/www/horilla/myenv/lib/python3.12/site-packages/django/contrib/sessions/backends/base.pyr   r      s    
 	r   r   c                       e Zd ZdZy)UpdateErrorzF
    Occurs if Django tries to update a session that was deleted.
    Nr   r   r   r   r   r      s     	r   r   c                   j   e Zd ZdZdZdZ e       Zd-dZd Z	d Z
d Zd	 Zed
        Zd-dZefdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z" ee!      Z# ee!e"      Z$d.dZ% ee%      Z&d  Z'd! Z(d" Z)d# Z*d$ Z+d% Z,d& Z-d' Z.d( Z/d.d)Z0d-d*Z1d+ Z2e3d,        Z4y)/SessionBasez-
    Base class for all Session classes.
    
testcookieworkedNc                 j    || _         d| _        d| _        t        t        j
                        | _        y )NF)_session_keyaccessedmodifiedr	   r   SESSION_SERIALIZER
serializerselfsession_keys     r   __init__zSessionBase.__init__+   s*    ''(C(CDr   c                     || j                   v S N_sessionr!   keys     r   __contains__zSessionBase.__contains__1       dmm##r   c                      | j                   |   S r%   r&   r(   s     r   __getitem__zSessionBase.__getitem__4   s    }}S!!r   c                 0    || j                   |<   d| _        y NTr'   r   r!   r)   values      r   __setitem__zSessionBase.__setitem__7   s    "cr   c                 ,    | j                   |= d| _        y r/   r0   r(   s     r   __delitem__zSessionBase.__delitem__;   s    MM#r   c                 4    d| j                   j                  z   S )Nzdjango.contrib.sessions.)	__class__r   r!   s    r   key_saltzSessionBase.key_salt?   s    )DNN,G,GGGr   c                 :    | j                   j                  ||      S r%   )r'   get)r!   r)   defaults      r   r;   zSessionBase.getC   s    }}  g..r   c                     | j                   xs || j                  v | _         || j                  u rdn|f} | j                  j                  |g| S )Nr   )r   r'   _SessionBase__not_givenpop)r!   r)   r<   argss       r   r?   zSessionBase.popF   sM    =)= 0 00rwj t}}  ,t,,r   c                 l    || j                   v r| j                   |   S d| _        || j                   |<   |S r/   r0   r1   s      r   
setdefaultzSessionBase.setdefaultK   s7    $--==%% DM!&DMM#Lr   c                 6    | j                   | | j                  <   y r%   )TEST_COOKIE_VALUETEST_COOKIE_NAMEr8   s    r   set_test_cookiezSessionBase.set_test_cookieS   s    &*&<&<T""#r   c                 R    | j                  | j                        | j                  k(  S r%   )r;   rE   rD   r8   s    r   test_cookie_workedzSessionBase.test_cookie_workedV   s"    xx--.$2H2HHHr   c                     | | j                   = y r%   )rE   r8   s    r   delete_test_cookiezSessionBase.delete_test_cookieY   s    &&'r   c                 \    t        j                  || j                  | j                  d      S )zGReturn the given session dictionary serialized and encoded as a string.T)saltr   compress)r   dumpsr9   r   )r!   session_dicts     r   encodezSessionBase.encode\   s'    }}	
 	
r   c                     	 t        j                  || j                  | j                        S # t         j                  $ r* t        j                  d      }|j                  d       Y i S t        $ r Y i S w xY w)N)rL   r   z!django.security.SuspiciousSessionzSession data corrupted)	r   loadsr9   r   BadSignaturelogging	getLoggerwarning	Exception)r!   session_dataloggers      r   decodezSessionBase.decodee   sw    
	==4==T__  ## 	5&&'JKFNN34
 		  	 			s   +. 9A6*A65A6c                 H    | j                   j                  |       d| _        y r/   )r'   updater   )r!   dict_s     r   r\   zSessionBase.updates   s    U#r   c                     || j                   v S r%   r&   r(   s     r   has_keyzSessionBase.has_keyw   r+   r   c                 6    | j                   j                         S r%   )r'   keysr8   s    r   ra   zSessionBase.keysz   s    }}!!##r   c                 6    | j                   j                         S r%   )r'   valuesr8   s    r   rc   zSessionBase.values}   s    }}##%%r   c                 6    | j                   j                         S r%   )r'   itemsr8   s    r   re   zSessionBase.items   s    }}""$$r   c                 .    i | _         d| _        d| _        y r/   )_session_cacher   r   r8   s    r   clearzSessionBase.clear   s     !r   c                 Z    	 | j                    xr | j                   S # t        $ r Y yw xY w)zBReturn True when there is no session_key and the session is empty.T)r   rg   AttributeErrorr8   s    r   is_emptyzSessionBase.is_empty   s6    	(((D1D1D-DD 		s    	**c                 L    	 t        dt              }| j                  |      s|S $)z)Return session key that isn't being used.    )r   VALID_KEY_CHARSexistsr    s     r   _get_new_session_keyz SessionBase._get_new_session_key   s(    +B@K;;{+"" r   c                 \    | j                   | j                         | _         | j                   S r%   )r   rp   r8   s    r   _get_or_create_session_keyz&SessionBase._get_or_create_session_key   s+    $ $ 9 9 ;D   r   c                 &    |xr t        |      dk\  S )z
        Key must be truthy and at least 8 characters long. 8 characters is an
        arbitrary lower bound for some minimal key security.
           )lenr(   s     r   _validate_session_keyz!SessionBase._validate_session_key   s    
 $s3x1}$r   c                     | j                   S r%   )_SessionBase__session_keyr8   s    r   _get_session_keyzSessionBase._get_session_key   s    !!!r   c                 D    | j                  |      r|| _        yd| _        y)zV
        Validate session key on assignment. Invalid values will set to None.
        N)rv   rx   r!   r2   s     r   _set_session_keyzSessionBase._set_session_key   s"     %%e,!&D!%Dr   c                     d| _         	 | j                  S # t        $ r9 | j                  |ri | _        n| j	                         | _        Y | j                  S w xY w)z
        Lazily load session from storage (unless "no_load" is True, when only
        an empty dict is stored) and store it in the current instance.
        T)r   rg   rj   r"   load)r!   no_loads     r   _get_sessionzSessionBase._get_session   s\    
 	2&&& 	2'7&(#&*iik#"""	2s    4AAc                 "    t         j                  S r%   )r   SESSION_COOKIE_AGEr8   s    r   get_session_cookie_agez"SessionBase.get_session_cookie_age   s    ***r   c                    	 |d   }	 |d   }|s| j	                         S t        |t        t        f      s|S t        |t              rt        j                  |      }||z
  }|j                  dz  |j                  z   S # t         $ r t        j                         }Y w xY w# t         $ r | j                  d      }Y w xY w)zGet the number of seconds until the session expires.

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        modificationexpiry_session_expiryiQ )KeyErrorr   nowr;   r   
isinstancer   strfromisoformatdaysseconds)r!   kwargsr   r   deltas        r   get_expiry_agezSessionBase.get_expiry_age   s    	*!.1L	1H%F ..00&8S/2Mfc"++F3F%zzE!EMM11#  	*#<<>L	*  	1XX/0F	1s"   A= B  =BB B=<B=c                 T   	 |d   }	 |d   }t	        |t
              r|S t	        |t              rt        j                  |      S |xs | j                         }|t        |      z   S # t         $ r t        j                         }Y w xY w# t         $ r | j                  d      }Y w xY w)zGet session the expiry date (as a datetime object).

        Optionally, this function accepts `modification` and `expiry` keyword
        arguments specifying the modification and expiry of the session.
        r   r   r   )r   )
r   r   r   r;   r   r   r   r   r   r   )r!   r   r   r   s       r   get_expiry_datezSessionBase.get_expiry_date   s    	*!.1L	1H%F fh'M$))&1184668i777  	*#<<>L	*
  	1XX/0F	1s"   A' B
 'BB
B'&B'c                     |	 | d= yt        |t              rt        j                         |z   }t        |t
              r|j                         }|| d<   y# t         $ r Y yw xY w)a*  
        Set a custom expiration for the session. ``value`` can be an integer,
        a Python ``datetime`` or ``timedelta`` object or ``None``.

        If ``value`` is an integer, the session will expire after that many
        seconds of inactivity. If set to ``0`` then the session will expire on
        browser close.

        If ``value`` is a ``datetime`` or ``timedelta`` object, the session
        will expire at that specific future time.

        If ``value`` is ``None``, the session uses the global session expiry
        policy.
        Nr   )r   r   r   r   r   r   	isoformatr{   s     r   
set_expiryzSessionBase.set_expiry   sn     =*+ eY'LLNU*EeX&OO%E"'  s   A 	A! A!c                 R    | j                  d      x}t        j                  S |dk(  S )a  
        Return ``True`` if the session is set to expire when the browser
        closes, and ``False`` if there's an expiry date. Use
        ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
        date/age, if there is one.
        r   r   )r;   r   SESSION_EXPIRE_AT_BROWSER_CLOSE)r!   r   s     r   get_expire_at_browser_closez'SessionBase.get_expire_at_browser_close  s/     hh011F:;;;{r   c                 R    | j                          | j                          d| _        y)zc
        Remove the current session data from the database and regenerate the
        key.
        N)rh   deleter   r8   s    r   flushzSessionBase.flush!  s    
 	

 r   c                     | j                   }| j                  }| j                          || _        |r| j	                  |       yy)zU
        Create a new session key, while retaining the current session data.
        N)r'   r"   createrg   r   )r!   datar)   s      r   	cycle_keyzSessionBase.cycle_key*  s=     }}"KK r   c                     t        d      )zF
        Return True if the given session_key already exists.
        z9subclasses of SessionBase must provide an exists() methodNotImplementedErrorr    s     r   ro   zSessionBase.exists7  s     "G
 	
r   c                     t        d      )z
        Create a new session instance. Guaranteed to create a new object with
        a unique key and will have saved the result once (with empty data)
        before the method returns.
        z8subclasses of SessionBase must provide a create() methodr   r8   s    r   r   zSessionBase.create?  s     "F
 	
r   c                     t        d      )z
        Save the session data. If 'must_create' is True, create a new session
        object (or raise CreateError). Otherwise, only update an existing
        object and don't create one (raise UpdateError if needed).
        z6subclasses of SessionBase must provide a save() methodr   )r!   must_creates     r   savezSessionBase.saveI  s     "D
 	
r   c                     t        d      )zx
        Delete the session data under this key. If the key is None, use the
        current session key value.
        z8subclasses of SessionBase must provide a delete() methodr   r    s     r   r   zSessionBase.deleteS  s    
 "F
 	
r   c                     t        d      )z@
        Load the session data and return a dictionary.
        z6subclasses of SessionBase must provide a load() methodr   r8   s    r   r~   zSessionBase.load\  s     "D
 	
r   c                     t        d      )a  
        Remove expired sessions from the session store.

        If this operation isn't possible on a given backend, it should raise
        NotImplementedError. If it isn't necessary, because the backend has
        a built-in expiration mechanism, it should be a no-op.
        z.This backend does not support clear_expired().r   )clss    r   clear_expiredzSessionBase.clear_expiredd  s     ""RSSr   r%   )F)5r   r   r   r   rE   rD   objectr>   r#   r*   r-   r3   r5   propertyr9   r;   r?   rB   rF   rH   rJ   rP   rZ   r\   r_   ra   rc   re   rh   rk   rp   rr   rv   ry   r|   r"   r   r   r'   r   r   r   r   r   r   r   ro   r   r   r   r~   classmethodr   r   r   r   r   r   !   s4    $ (KE$" H H/  + -
=I(
$$&%#!
%"& +,K,.>?L# %H+268.(8	!	




 T Tr   r   )rT   stringr   r   django.confr   django.corer   django.utilsr   django.utils.cryptor   django.utils.module_loadingr	   ascii_lowercasedigitsrn   rW   r   r   r   r   r   r   <module>r      sY      (    ! 1 5 ((6==8	) 		) 	LT LTr   