o
    {,h                     @   sx   d dl Z d dlm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)sync_to_async)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 Zdd Zdd Zdd Zdd Zd+ddZd+d d!Zd*d"d#Zd*d$d%Ze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__ Y/var/www/html/rh/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              
      s   z| j jj| jt dI d H W S  | j jtfy@ } zt|tr2t	
d|jj }|t| d | _W Y d }~d S d }~ww r   )r   r    agetr   r   r"   r#   r   r$   r%   r&   r   r'   r(   r)   r*   r+   r   r   r   _aget_session_from_db+   s   

z"SessionStore._aget_session_from_dbc                 C   s   |   }|r| |jS i S r   )r.   decodesession_datar   sr   r   r   load6   s   zSessionStore.loadc                    s$   |   I d H }|r| |jS i S r   )r0   r1   r2   r3   r   r   r   aload:   s   zSessionStore.aloadc                 C   s   | j jj|d S N)r   )r   r    filterexistsr   r   r   r   r9   >   s   zSessionStore.existsc                    s   | j jj|d I d H S r7   )r   r    r8   aexistsr   r   r   r   r:   A   s   zSessionStore.aexistsc                 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   createD   s   
zSessionStore.createc                    sH   	 |   I d H | _z| jddI d H  W n	 ty   Y qw d| _d S r;   )_aget_new_session_keyr*   asaver   r?   r   r   r   r   acreateQ   s   zSessionStore.acreatec                 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   r2   expire_date)r   _get_or_create_session_keyencodeget_expiry_dater   datar   r   r   create_model_instance^   s
   z"SessionStore.create_model_instancec                    s,   | j |  I dH | ||  I dH dS )zSee create_model_instance().NrD   )r   _aget_or_create_session_keyrG   aget_expiry_daterI   r   r   r   acreate_model_instancej   s   z#SessionStore.acreate_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_updaterT   )r   r@   _get_sessionrK   r	   db_for_writer   r
   atomicr>   r   r   r   r   )r   r<   rJ   objrT   r   r   r   r>   r   s(   

&zSessionStore.savec                    s   | j du r|  I dH S | j dI dH }| |I dH tj| jdzt fdd}| I dH  W dS  tyE    rDt	  t
yP    sOt w )zSee save().NrO   rQ   c                      sD   t jd j   d W d    d S 1 sw   Y  d S )NrS   rU   )r
   rZ   r>   r   r<   r[   rT   r   r   sync_transaction   s   "z,SessionStore.asave.<locals>.sync_transaction)r   rC   _aget_sessionrN   r	   rY   r   r   r   r   r   r   )r   r<   rJ   r]   r   r\   r   rB      s&   
zSessionStore.asavec                 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 r7   )r   r   r    r!   deleter#   r   r   r   r   r_      s   
zSessionStore.deletec                    sb   |d u r| j d u rd S | j }z| jjj|dI d H }| I d H  W d S  | jjy0   Y d S w r7   )r   r   r    r/   adeleter#   )r   r   r[   r   r   r   r`      s   
zSessionStore.adeletec                 C   s   |   jjt d  d S N)expire_date__lt)r   r    r8   r   r"   r_   r   r   r   r   clear_expired   s   zSessionStore.clear_expiredc                    s&   |   jjt d I d H  d S ra   )r   r    r8   r   r"   r`   rc   r   r   r   aclear_expired   s   
zSessionStore.aclear_expiredr   )F)r'   
__module____qualname____doc__r   classmethodr   r   r   r.   r0   r5   r6   r9   r:   r@   rC   rK   rN   r>   rB   r_   r`   rd   re   __classcell__r   r   r   r   r      s2    







r   )r%   asgiref.syncr   %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    