a
    ù!f~  ã                   @   sN   d Z ddlmZ ddlZddlmZ G dd„ dejjƒZ	G dd„ dej
ƒZ
dS )	a¥  OAuth 2.0 utilities for SQLAlchemy.

Utilities for using OAuth 2.0 in conjunction with a SQLAlchemy.

Configuration
=============

In order to use this storage, you'll need to create table
with :class:`oauth2client.contrib.sqlalchemy.CredentialsType` column.
It's recommended to either put this column on some sort of user info
table or put the column in a table with a belongs-to relationship to
a user info table.

Here's an example of a simple table with a :class:`CredentialsType`
column that's related to a user table by the `user_id` key.

.. code-block:: python

    from sqlalchemy import Column, ForeignKey, Integer
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship

    from oauth2client.contrib.sqlalchemy import CredentialsType


    Base = declarative_base()


    class Credentials(Base):
        __tablename__ = 'credentials'

        user_id = Column(Integer, ForeignKey('user.id'))
        credentials = Column(CredentialsType)


    class User(Base):
        id = Column(Integer, primary_key=True)
        # bunch of other columns
        credentials = relationship('Credentials')


Usage
=====

With tables ready, you are now able to store credentials in database.
We will reuse tables defined above.

.. code-block:: python

    from sqlalchemy.orm import Session

    from oauth2client.client import OAuth2Credentials
    from oauth2client.contrib.sql_alchemy import Storage

    session = Session()
    user = session.query(User).first()
    storage = Storage(
        session=session,
        model_class=Credentials,
        # This is the key column used to identify
        # the row that stores the credentials.
        key_name='user_id',
        key_value=user.id,
        property_name='credentials',
    )

    # Store
    credentials = OAuth2Credentials(...)
    storage.put(credentials)

    # Retrieve
    credentials = storage.get()

    # Delete
    storage.delete()

é    )Úabsolute_importN)Úclientc                   @   s   e Zd ZdZdS )ÚCredentialsTypezXType representing credentials.

    Alias for :class:`sqlalchemy.types.PickleType`.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r	   r	   ú`/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/sqlalchemy.pyr   d   s   r   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
ÚStoragez²Store and retrieve a single credential to and from SQLAlchemy.
    This helper presumes the Credentials
    have been stored as a Credentials column
    on a db model class.
    c                    s0   t t| ƒ ¡  || _|| _|| _|| _|| _dS )a?  Constructor for Storage.

        Args:
            session: An instance of :class:`sqlalchemy.orm.Session`.
            model_class: SQLAlchemy declarative mapping.
            key_name: string, key name for the entity that has the credentials
            key_value: key value for the entity that has the credentials
            property_name: A string indicating which property on the
                           ``model_class`` to store the credentials.
                           This property must be a
                           :class:`CredentialsType` column.
        N)Úsuperr   Ú__init__ÚsessionÚmodel_classÚkey_nameÚ	key_valueÚproperty_name)Úselfr   r   r   r   r   ©Ú	__class__r	   r
   r   r   s    zStorage.__init__c                 C   sb   | j | ji}| j | j¡jf i |¤Ž}| ¡ }|rZt|| jƒ}|rVt	|dƒrV| 
| ¡ |S dS dS )zzRetrieve stored credential.

        Returns:
            A :class:`oauth2client.Credentials` instance or `None`.
        Ú	set_storeN)r   r   r   Úqueryr   Ú	filter_byÚfirstÚgetattrr   Úhasattrr   )r   Úfiltersr   ÚentityZ
credentialr	   r	   r
   Ú
locked_getˆ   s    
zStorage.locked_getc                 C   s`   | j | ji}| j | j¡jf i |¤Ž}| ¡ }|sB| jf i |¤Ž}t|| j|ƒ | j 	|¡ dS )zƒWrite a credentials to the SQLAlchemy datastore.

        Args:
            credentials: :class:`oauth2client.Credentials`
        N)
r   r   r   r   r   r   r   Úsetattrr   Úadd)r   Úcredentialsr   r   r   r	   r	   r
   Ú
locked_putš   s    zStorage.locked_putc                 C   s.   | j | ji}| j | j¡jf i |¤Ž ¡  dS )z1Delete credentials from the SQLAlchemy datastore.N)r   r   r   r   r   r   Údelete)r   r   r	   r	   r
   Úlocked_deleteª   s    zStorage.locked_delete)	r   r   r   r   r   r   r"   r$   Ú__classcell__r	   r	   r   r
   r   k   s
   r   )r   Ú
__future__r   Zsqlalchemy.typesZ
sqlalchemyZoauth2clientr   ÚtypesZ
PickleTyper   r   r	   r	   r	   r
   Ú<module>   s
   N