
    Xf-                         d dl Z d dlmZ d dlm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mZmZmZmZmZ d	d
lmZ d	dlmZmZ d	dlmZ  G d de          ZddZ G d de          ZdS )    N)datetime)	timedelta)AnyOptional)Flask)
SQLAlchemy)
want_bytes)ColumnDateTimeIntegerLargeBinarySequenceString   )retry_query)ServerSideSessionServerSideSessionInterface)Defaultsc                       e Zd ZdS )SqlAlchemySessionN)__name__
__module____qualname__     W/var/www/piapp/venv/lib/python3.11/site-packages/flask_session/sqlalchemy/sqlalchemy.pyr   r      s        Dr   r   c                 >     G fdd| j                   }|S )Nc                       e Zd ZZrdini Z Zr ee e          d          n eed          Z	 e e
d          d          Z ee          Z ee          Zdededefd	Zd
 ZdS )%create_session_model.<locals>.SessionschemaT)primary_key   )unique
session_iddataexpiryc                 0    || _         || _        || _        d S )Nr$   r%   r&   )selfr$   r%   r&   s       r   __init__z.create_session_model.<locals>.Session.__init__#   s    (DODI DKKKr   c                     d| j          dS )Nz<Session data >)r%   r)   s    r   __repr__z.create_session_model.<locals>.Session.__repr__(   s    0DI0000r   N)r   r   r   __tablename____table_args____bind_key__r
   r   r   idr   r$   r   r%   r   r&   strr   r   r*   r.   )bind_keyr    sequence
table_names   r   Sessionr      s        "/5=(F++2 3FF7HHX..DAAAAT222 	
 VFF3KK555
vk""!!	!s 	!# 	!x 	! 	! 	! 	!
	1 	1 	1 	1 	1r   r7   )Model)dbr6   r    r4   r5   r7   s    ```` r   create_session_modelr:      sR    1 1 1 1 1 1 1 1 1 1"( 1 1 1, Nr   c                       e Zd ZdZeZdZej        ej	        ej
        ej        ej        ej        ej        ej        ej        ej        ej        fdee         dee         dedededed	ed
edee         dee         dee         dee         f fdZ e            dd            Z e            dedee         fd            Z e            deddfd            Z e            dede deddfd            Z! xZ"S )SqlAlchemySessionInterfaceat  Uses the Flask-SQLAlchemy from a flask app as session storage.

    :param app: A Flask app instance.
    :param client: A Flask-SQLAlchemy instance.
    :param key_prefix: A prefix that is added to all storage keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    :param sid_length: The length of the generated session id in bytes.
    :param serialization_format: The serialization format to use for the session data.
    :param table: The table name you want to use.
    :param sequence: The sequence to use for the primary key if needed.
    :param schema: The db schema to use.
    :param bind_key: The db bind key to use.
    :param cleanup_n_requests: Delete expired sessions on average every N requests.

    .. versionadded:: 0.7
        db changed to client to be standard on all session interfaces.
        The `cleanup_n_request` parameter was added.

    .. versionadded:: 0.6
        The `sid_length`, `sequence`, `schema` and `bind_key` parameters were added.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.
    Fappclient
key_prefix
use_signer	permanent
sid_lengthserialization_formattabler5   r    r4   cleanup_n_requestsc           	         || _         |t          |t                    s+t          j        dt
          d           t          |          }|| _        t          |||
||	          | _        |	                                5  |r| j        
                    ||          }n| j        j        }| j        j                            |d           d d d            n# 1 swxY w Y   t                                          |||||||           d S )NznNo valid SQLAlchemy instance provided, attempting to create a new instance on localhost with default settings.   )
stacklevel)bindT)rI   
checkfirst)r=   
isinstancer   warningswarnRuntimeWarningr>   r:   sql_session_modelapp_context
get_engineengine	__table__createsuperr*   )r)   r=   r>   r?   r@   rA   rB   rC   rD   r5   r    r4   rE   rR   	__class__s                 r   r*   z#SqlAlchemySessionInterface.__init__L   sy    >FJ!?!?>M A   
  __F "6E68X"
 "
 __ 	R 	R ,//(/CC+",33D3QQQ	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	 	
 	
 	
 	
 	
s   >ACCCreturnNc                 r   	 | j         j                            | j                                      | j        j        t          j                    k                                  d           | j         j        	                                 d S # t          $ r  | j         j                                          w xY w)NF)synchronize_session)r>   sessionqueryrO   filterr&   r   utcnowdeletecommit	Exceptionrollbackr-   s    r   _delete_expired_sessionsz3SqlAlchemySessionInterface._delete_expired_sessions|   s    	K%%d&<==DD&-1B1BB ff///K&&((((( 	 	 	K((***	s   BB *B6store_idc                    | j         j                            |                                          }|r|j        |j        t          j                    k    rn	 | j        j        	                    |           | j        j        
                                 n-# t          $ r  | j        j                                          w xY wd }|r.t          |j                  }| j                            |          S d S Nr$   )rO   r[   	filter_byfirstr&   r   r]   r>   rZ   r^   r_   r`   ra   r	   r%   
serializerdecode)r)   rc   recordserialized_session_datas       r   _retrieve_session_dataz1SqlAlchemySessionInterface._retrieve_session_data   s     '-7787LLRRTT  	v},ARAR0R0R#**6222#**,,,,   #,,... F 	C&0&=&=#?))*ABBBts   =B *Cc                    	 | j         j                            |                                           | j        j                                         d S # t          $ r  | j        j                                          w xY wre   )	rO   r[   rg   r^   r>   rZ   r_   r`   ra   )r)   rc   s     r   _delete_sessionz*SqlAlchemySessionInterface._delete_session   s    	"(22h2GGNNPPPK&&((((( 	 	 	K((***	s   AA *A>session_lifetimerZ   c                    t          j                    |z   }| j                            |          }	 | j        j                            |                                          }|r||_        ||_	        n7|                     |||          }| j
        j                            |           | j
        j                                         d S # t          $ r  | j
        j                                          w xY w)Nrf   r(   )r   r]   ri   encoderO   r[   rg   rh   r%   r&   r>   rZ   addr_   r`   ra   )r)   rp   rZ   rc   storage_expiration_datetimerl   rk   s          r   _upsert_sessionz*SqlAlchemySessionInterface._upsert_session   s    '/o&7&7:J&J# #'/"8"8"A"A	+1;;x;PPVVXXF 	05 ;//'06 0  
 #''///K&&((((( 	 	 	K((***	s   BC *C6)rW   N)#r   r   r   __doc__r   session_classttlr   SESSION_SQLALCHEMYSESSION_KEY_PREFIXSESSION_USE_SIGNERSESSION_PERMANENTSESSION_ID_LENGTHSESSION_SERIALIZATION_FORMATSESSION_SQLALCHEMY_TABLESESSION_SQLALCHEMY_SEQUENCESESSION_SQLALCHEMY_SCHEMASESSION_SQLALCHEMY_BIND_KEYSESSION_CLEANUP_N_REQUESTSr   r   r   r3   boolintr*   r   rb   dictrm   ro   	TimeDeltar   ru   __classcell__)rV   s   @r   r<   r<   .   s        4 &M
C
 (0'B"5#6"4"4$,$I6"*"F ( B"*"F,4,O.
 .
e_.
 $.
 	.

 .
 .
 .
 ".
 .
 3-.
 .
 3-.
 %SM.
 .
 .
 .
 .
 .
` []]   ] []]s x~    ]& []]     ] []] )4EQT	   ]    r   r<   )NNN)rL   r   r   r   typingr   r   flaskr   flask_sqlalchemyr   itsdangerousr	   
sqlalchemyr
   r   r   r   r   r   _utilsr   baser   r   defaultsr   r   r:   r<   r   r   r   <module>r      sj          + + + + + +                       ' ' ' ' ' ' # # # # # # O O O O O O O O O O O O O O O O             @ @ @ @ @ @ @ @      	 	 	 	 	) 	 	 	   4O O O O O!; O O O O Or   