o
    e                     @   sl   d dl Z d dlmZmZm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 G dd deZdS )	    N)CreateErrorSessionBaseUpdateError)SuspiciousOperation)DatabaseErrorIntegrityErrorroutertransaction)timezone)cached_propertyc                       s   e Zd ZdZd fdd	Zedd Ze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edd Z  ZS )SessionStorez+
    Implement database session store.
    Nc                    s   t  | d S N)super__init__selfsession_key	__class__ V/var/www/html/venv/lib/python3.10/site-packages/django/contrib/sessions/backends/db.pyr      s   zSessionStore.__init__c                 C   s   ddl m} |S )Nr   )Session)django.contrib.sessions.modelsr   )clsr   r   r   r   get_model_class   s   zSessionStore.get_model_classc                 C   s   |   S r   )r   r   r   r   r   model   s   zSessionStore.modelc              
   C   sz   z| j jj| jt dW S  | j jtfy< } zt|tr.t	
d|jj }|t| d | _W Y d }~d S d }~ww )N)r   expire_date__gtzdjango.security.%s)r   objectsgetr   r
   nowDoesNotExistr   
isinstancelogging	getLoggerr   __name__warningstr_session_key)r   eloggerr   r   r   _get_session_from_db   s   

z!SessionStore._get_session_from_dbc                 C   s   |   }|r| |jS i S r   )r+   decodesession_data)r   sr   r   r   load)   s   zSessionStore.loadc                 C   s   | j jj|d S N)r   )r   r   filterexistsr   r   r   r   r2   -   s   zSessionStore.existsc                 C   s:   	 |   | _z| jdd W n	 ty   Y q w d| _d S )NT)must_create)_get_new_session_keyr(   saver   modifiedr   r   r   r   create0   s   
zSessionStore.createc                 C   s   | j |  | ||  dS )z
        Return a new instance of the session model object, which represents the
        current session state. Intended to be used for saving the session data
        to the database.
        )r   r-   expire_date)r   _get_or_create_session_keyencodeget_expiry_date)r   datar   r   r   create_model_instance=   s
   z"SessionStore.create_model_instanceFc                 C   s   | j du r	|  S | j|d}| |}tj| j|d}z$tj|d |j	|| |d W d   W dS 1 s9w   Y  W dS  t
yL   |rKt  tyW   |sVt w )z
        Save the current session data to the database. If 'must_create' is
        True, raise a database error if the saving operation doesn't create a
        new entry (as opposed to possibly updating an existing entry).
        N)no_load)instance)using)force_insertforce_updater@   )r   r7   _get_sessionr=   r   db_for_writer   r	   atomicr5   r   r   r   r   )r   r3   r<   objr@   r   r   r   r5   I   s(   

&zSessionStore.savec                 C   sP   |d u r| j d u rd S | j }z| jjj|d  W d S  | jjy'   Y d S w r0   )r   r   r   r   deleter!   r   r   r   r   rG   b   s   
zSessionStore.deletec                 C   s   |   jjt d  d S )N)expire_date__lt)r   r   r1   r
   r    rG   )r   r   r   r   clear_expiredl   s   zSessionStore.clear_expiredr   )F)r%   
__module____qualname____doc__r   classmethodr   r   r   r+   r/   r2   r7   r=   r5   rG   rI   __classcell__r   r   r   r   r   
   s     




r   )r#   %django.contrib.sessions.backends.baser   r   r   django.core.exceptionsr   	django.dbr   r   r   r	   django.utilsr
   django.utils.functionalr   r   r   r   r   r   <module>   s    