a
    ¦Q•hÉ  ã                   @   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__© ú\/var/www/sistema_ama/venv/lib/python3.9/site-packages/django/contrib/sessions/backends/db.pyr      s    zSessionStore.__init__c                 C   s   ddl m} |S )Nr   )ÚSession)Z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   s|   z| j jj| jt ¡ d�W S  | j jtfyv } z<t|tƒr\t	 
d|jj ¡}| t|ƒ¡ d | _W Y d }~n
d }~0 0 d S )N)r   Z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   r0   -   s    zSessionStore.existsc                 C   s<   |   ¡ | _z| jdd� W n ty0   Y q Y n0 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�}zBtj|d��" |j	|| |d� W d  ƒ n1 sn0    Y  W n6 t
y–   |r�t‚‚ Y n ty°   |sªt‚‚ Y n0 dS )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   r5   Ú_get_sessionr;   r   Údb_for_writer   r	   Úatomicr3   r   r   r   r   )r   r1   r:   Úobjr>   r   r   r   r3   I   s$    

ÿ(zSessionStore.savec                 C   sP   |d u r| j d u rd S | j }z| jjj|d� ¡  W n | jjyJ   Y n0 d S r.   )r   r   r   r   Údeleter   r   r   r   r   rE   b   s    
zSessionStore.deletec                 C   s   |   ¡ jjt ¡ d� ¡  d S )N)Zexpire_date__lt)r   r   r/   r
   r   rE   )r   r   r   r   Úclear_expiredl   s    zSessionStore.clear_expired)N)F)N)r#   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r   r   r)   r-   r0   r5   r;   r3   rE   rF   Ú__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   