
    ~                         S r SSKJr  SSKrSSKJr   " S S\R                  R                  5      r	 " S S\R                  5      r
g)	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                       \ rS rSrSrSrg)CredentialsTyped   zPType representing credentials.

Alias for :class:`sqlalchemy.types.PickleType`.
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/third_party/oauth2client/contrib/sqlalchemy.pyr   r   d   s    r   r   c                   >   ^  \ rS rSrSrU 4S jrS rS rS rSr	U =r
$ )Storagek   zStore 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                 f   > [         [        U ]  5         Xl        X l        X0l        X@l        XPl        g)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__s         r   r   Storage.__init__r   s-     	gt%'& "*r   c                 L   U R                   U R                  0nU R                  R                  U R                  5      R
                  " S0 UD6nUR                  5       nU(       a@  [        X0R                  5      nU(       a"  [        US5      (       a  UR                  U 5        U$ g)zbRetrieve stored credential.

Returns:
    A :class:`oauth2client.Credentials` instance or `None`.
	set_storeNr   )r   r   r   queryr   	filter_byfirstgetattrr   hasattrr    )r   filtersr!   entity
credentials        r   
locked_getStorage.locked_get   s     ==$..1""4#3#34>>II ););<Jgj+>>$$T*r   c                 R   U R                   U R                  0nU R                  R                  U R                  5      R
                  " S0 UD6nUR                  5       nU(       d  U R                  " S0 UD6n[        X@R                  U5        U R                  R                  U5        g)zkWrite a credentials to the SQLAlchemy datastore.

Args:
    credentials: :class:`oauth2client.Credentials`
Nr   )
r   r   r   r!   r   r"   r#   setattrr   add)r   credentialsr&   r!   r'   s        r   
locked_putStorage.locked_put   s     ==$..1""4#3#34>>II%%00F**K8 r   c                     U R                   U R                  0nU R                  R                  U R                  5      R
                  " S0 UD6R                  5         g)z1Delete credentials from the SQLAlchemy datastore.Nr   )r   r   r   r!   r   r"   delete)r   r&   s     r   locked_deleteStorage.locked_delete   sC    ==$..14++,66AAHHJr   )r   r   r   r   r   )r	   r
   r   r   r   r   r)   r/   r3   r   __classcell__)r   s   @r   r   r   k   s#    +,$! K Kr   r   )r   
__future__r   sqlalchemy.types
sqlalchemyoauth2clientr   types
PickleTyper   r   r   r   r   <module>r<      sC   L\ '  j&&11 BKfnn BKr   