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__© úc/var/www/html/premium_crap/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    