
    ~                         d Z ddlmZ ddlZddlmZ  G d dej                  j                        Z	 G d dej                        Z
y)	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                       e Zd ZdZy)CredentialsTypezXType representing credentials.

    Alias for :class:`sqlalchemy.types.PickleType`.
    N)__name__
__module____qualname____doc__     2lib/third_party/oauth2client/contrib/sqlalchemy.pyr   r   d   s    r   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )StoragezStore 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                 p    t         t        |           || _        || _        || _        || _        || _        y)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   zStorage.__init__r   s7     	gt%'& "*r   c                 0   | j                   | j                  i} | j                  j                  | j                        j
                  di |}|j                         }|r7t        || j                        }|rt        |d      r|j                  |        |S y)zzRetrieve 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_getzStorage.locked_get   s     ==$..1>""4#3#34>>II ););<Jgj+>$$T*r   c                 J   | j                   | j                  i} | j                  j                  | j                        j
                  di |}|j                         }|s | j                  di |}t        || j                  |       | j                  j                  |       y)zWrite 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_putzStorage.locked_put   s     ==$..1>""4#3#34>>II%T%%00F**K8 r   c                     | j                   | j                  i} | j                  j                  | j                        j
                  di |j                          y)z1Delete credentials from the SQLAlchemy datastore.Nr   )r   r   r   r   r   r   delete)r   r!   s     r   locked_deletezStorage.locked_delete   sE    ==$..164++,66AAHHJr   )	r   r   r	   r
   r   r$   r)   r,   __classcell__)r   s   @r   r   r   k   s    +,$! Kr   r   )r
   
__future__r   sqlalchemy.types
sqlalchemyoauth2clientr   types
PickleTyper   r   r   r   r   <module>r4      sC   L\ '  j&&11 BKfnn BKr   