o
    ;h-                     @   s   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 deZdddZG d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                   @   s   e Zd ZdS )SqlAlchemySessionN)__name__
__module____qualname__ r   r   V/var/www/html/venv/lib/python3.10/site-packages/flask_session/sqlalchemy/sqlalchemy.pyr      s    r   c                    s    G  fddd| j }|S )Nc                       s   e Zd ZZrdini Z ZreeeddneeddZ	ee
dddZeeZeeZdededefd	d
Zdd ZdS )z%create_session_model.<locals>.SessionschemaT)Zprimary_key   )unique
session_iddataexpiryc                 S   s   || _ || _|| _d S )Nr   r   r   )selfr   r   r   r   r   r   __init__#   s   
z.create_session_model.<locals>.Session.__init__c                 S   s   d| j  dS )Nz<Session data >)r   r!   r   r   r   __repr__(   s   z.create_session_model.<locals>.Session.__repr__N)r   r   r   Z__tablename__Z__table_args__Z__bind_key__r	   r   r   idr   r   r   r   r
   r   strr   r   r"   r%   r   bind_keyr   sequence
table_namer   r   Session   s    
r,   )ZModel)dbr+   r   r)   r*   r,   r   r(   r   create_session_model   s   r.   c                       s  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dZe dddZe dedee fddZe deddfddZe dede deddfddZ!  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tabler*   r   r)   cleanup_n_requestsc              	      s   || _ |d u st|tstjdtdd t|}|| _t|||
||	| _|	   |r4| jj
||d}n| jj}| jjj|dd W d    n1 sKw   Y  t ||||||| d S )NznNo valid SQLAlchemy instance provided, attempting to create a new instance on localhost with default settings.   )
stacklevel)bindT)r;   Z
checkfirst)r0   
isinstancer   warningswarnRuntimeWarningr1   r.   sql_session_modelZapp_contextZ
get_engineengineZ	__table__createsuperr"   )r!   r0   r1   r2   r3   r4   r5   r6   r7   r*   r   r)   r8   rA   	__class__r   r   r"   L   s6   

z#SqlAlchemySessionInterface.__init__returnNc                 C   sZ   z| j j| j| jjt kjdd | j j	  W d S  t
y,   | j j   w )NF)Zsynchronize_session)r1   sessionqueryr@   filterr   r   utcnowdeletecommit	Exceptionrollbackr$   r   r   r   _delete_expired_sessions|   s   z3SqlAlchemySessionInterface._delete_expired_sessionsstore_idc                 C   s   | j jj|d }|r8|jd u s|jt kr8z| jj	| | jj
  W n ty5   | jj   w d }|rEt|j}| j|S d S Nr   )r@   rH   	filter_byfirstr   r   rJ   r1   rG   rK   rL   rM   rN   r   r   
serializerdecode)r!   rP   recordserialized_session_datar   r   r   _retrieve_session_data   s   
z1SqlAlchemySessionInterface._retrieve_session_datac                 C   sD   z| j jj|d  | jj  W d S  ty!   | jj   w rQ   )	r@   rH   rS   rK   r1   rG   rL   rM   rN   )r!   rP   r   r   r   _delete_session   s   z*SqlAlchemySessionInterface._delete_sessionsession_lifetimerG   c                 C   s   t  | }| j|}z+| jjj|d }|r ||_||_	n| j|||d}| j
j| | j
j  W d S  tyE   | j
j   w )NrR   r    )r   rJ   rU   encoder@   rH   rS   rT   r   r   r1   rG   addrL   rM   rN   )r!   r[   rG   rP   Zstorage_expiration_datetimerX   rW   r   r   r   _upsert_session   s$   z*SqlAlchemySessionInterface._upsert_session)rF   N)#r   r   r   __doc__r   Zsession_classttlr   ZSESSION_SQLALCHEMYZSESSION_KEY_PREFIXZSESSION_USE_SIGNERZSESSION_PERMANENTZSESSION_ID_LENGTHZSESSION_SERIALIZATION_FORMATZSESSION_SQLALCHEMY_TABLEZSESSION_SQLALCHEMY_SEQUENCEZSESSION_SQLALCHEMY_SCHEMAZSESSION_SQLALCHEMY_BIND_KEYZSESSION_CLEANUP_N_REQUESTSr   r   r   r'   boolintr"   r   rO   dictrY   rZ   	TimeDeltar   r^   __classcell__r   r   rD   r   r/   .   sp    	
0
r/   )NNN)r=   r   r   rd   typingr   r   Zflaskr   Zflask_sqlalchemyr   Zitsdangerousr   Z
sqlalchemyr	   r
   r   r   r   r   Z_utilsr   baser   r   defaultsr   r   r.   r/   r   r   r   r   <module>   s     
