
     h-                         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dS )    N)datetime	timedelta)settings)signing)timezone)get_random_string)import_stringc                       e Zd ZdZdS )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__     d/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django/contrib/sessions/backends/base.pyr   r      s         
 	Dr   r   c                       e Zd ZdZdS )UpdateErrorzF
    Occurs if Django tries to update a session that was deleted.
    Nr   r   r   r   r   r      s          	Dr   r   c                      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-             Z4dS )0SessionBasez-
    Base class for all Session classes.
    
testcookieworkedNc                 l    || _         d| _        d| _        t          t          j                  | _        d S )NF)_session_keyaccessedmodifiedr	   r   SESSION_SERIALIZER
serializerselfsession_keys     r   __init__zSessionBase.__init__+   s.    ''(CDDr   c                     || j         v S N_sessionr!   keys     r   __contains__zSessionBase.__contains__1       dm##r   c                     | j         |         S r%   r&   r(   s     r   __getitem__zSessionBase.__getitem__4   s    }S!!r   c                 (    || j         |<   d| _        d S NTr'   r   r!   r)   values      r   __setitem__zSessionBase.__setitem__7   s    "cr   c                 $    | j         |= d| _        d S r/   r0   r(   s     r   __delitem__zSessionBase.__delitem__;   s    M#r   c                      d| j         j        z   S )Nzdjango.contrib.sessions.)	__class__r   r!   s    r   key_saltzSessionBase.key_salt?   s    )DN,GGGr   c                 8    | j                             ||          S r%   )r'   get)r!   r)   defaults      r   r;   zSessionBase.getC   s    }  g...r   c                 n    | j         p|| j        v | _         || j        u rdn|f} | j        j        |g|R  S )Nr   )r   r'   _SessionBase__not_givenpop)r!   r)   r<   argss       r   r?   zSessionBase.popF   sL    =)= 000rrwj t} ,t,,,,r   c                 T    || j         v r| j         |         S d| _        || j         |<   |S r/   r0   r1   s      r   
setdefaultzSessionBase.setdefaultK   s4    $-=%% DM!&DM#Lr   c                 $    | j         | | j        <   d S r%   )TEST_COOKIE_VALUETEST_COOKIE_NAMEr8   s    r   set_test_cookiezSessionBase.set_test_cookieS   s    &*&<T"###r   c                 H    |                      | j                  | j        k    S r%   )r;   rE   rD   r8   s    r   test_cookie_workedzSessionBase.test_cookie_workedV   s    xx-..$2HHHr   c                     | | j         = d S r%   )rE   r8   s    r   delete_test_cookiezSessionBase.delete_test_cookieY   s    &'''r   c                 F    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          }|                    d           Y nt          $ r Y nw xY wi S )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   s    
	=4=T_    # 	5 	5 	5&'JKKFNN344444 	 	 	 D	 	s    # 8A)	A)(A)c                 H    | j                             |           d| _        d S 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                 4    | j                                         S r%   )r'   keysr8   s    r   ra   zSessionBase.keysz   s    }!!###r   c                 4    | j                                         S r%   )r'   valuesr8   s    r   rc   zSessionBase.values}   s    }##%%%r   c                 4    | j                                         S r%   )r'   itemsr8   s    r   re   zSessionBase.items   s    }""$$$r   c                 0    i | _         d| _        d| _        d S r/   )_session_cacher   r   r8   s    r   clearzSessionBase.clear   s     !r   c                 F    	 | j          o| j         S # t          $ r Y dS 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   s@    	((D1D-DD 	 	 	44	s    
  c                 ^    	 t          dt                    }|                     |          s|S -)z)Return session key that isn't being used.T    )r   VALID_KEY_CHARSexistsr    s     r   _get_new_session_keyz SessionBase._get_new_session_key   s5    	#+B@@K;;{++ #""	#r   c                 P    | 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                 ,    |o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    
 $s3xx1}$r   c                     | j         S r%   )_SessionBase__session_keyr8   s    r   _get_session_keyzSessionBase._get_session_key   s    !!r   c                 P    |                      |          r	|| _        dS d| _        dS )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   s6     %%e,, 	&!&D!%Dr   Fc                     d| _         	 | j        S # t          $ r- | j        |ri | _        n|                                 | _        Y nw xY w| j        S )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   sj    
 	2&& 	2 	2 	2'7'&(##&*iikk#		2
 ""s    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         }n## t           $ r t          j                    }Y nw xY w	 |d         }n%# t           $ r |                     d          }Y nw xY w|s|                                 S t          |t          t          f          s|S t          |t                    rt          j        |          }||z
  }|j	        dz  |j
        z   S )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    	*!.1LL 	* 	* 	*#<>>LLL	*
	1H%FF 	1 	1 	1XX/00FFF	1  	1..000&8S/22 	Mfc"" 	4+F33F%zE!EM11    ++8 AAc                    	 |d         }n## t           $ r t          j                    }Y nw xY w	 |d         }n%# t           $ r |                     d          }Y nw xY wt	          |t
                    r|S t	          |t                    rt          j        |          S |p|                                 }|t          |          z   S )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    	*!.1LL 	* 	* 	*#<>>LLL	*	1H%FF 	1 	1 	1XX/00FFF	1 fh'' 	2M$$ 	2)&111846688i77777r   c                     |	 | d= n# t           $ r Y nw xY wdS t          |t                    rt          j                    |z   }t          |t
                    r|                                }|| d<   dS )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   s     =*++   FeY'' 	+LNNU*EeX&& 	&OO%%E"'s    
c                 T    |                      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   Nr   )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11F:;;{r   c                 d    |                                   |                                  d| _        dS )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        }|                                  || _        |r|                     |           dS dS )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*  sR     }" 	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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   !   s         $ &((KE E E E$ $ $" " "     H H XH/ / / /  + - - - -
  = = =I I I( ( (
 
 
    $ $ $$ $ $& & &% % %    # # #! ! !
% % %" " "& & & (+,,K8,.>??L# # # # x%%H+ + +2 2 268 8 8.( ( (8	 	 	! ! !	 	 	
 
 

 
 

 
 
 

 
 
 

 
 
 T T [T 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      s4     ( ( ( ( ( ( ( (                   ! ! ! ! ! ! 1 1 1 1 1 1 5 5 5 5 5 5 (6=8	 	 	 	 	) 	 	 		 	 	 	 	) 	 	 	LT LT LT LT LT LT LT LT LT LTr   