σ
Ύ^Yc           @@  sk   d  Z  d d l m Z d d l Z d d l m Z d e j j f d     YZ	 d e j
 f d     YZ
 d S(	   s₯  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()

i    (   t   absolute_importN(   t   clientt   CredentialsTypec           B@  s   e  Z d  Z RS(   sX   Type representing credentials.

    Alias for :class:`sqlalchemy.types.PickleType`.
    (   t   __name__t
   __module__t   __doc__(    (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyR   d   s   t   Storagec           B@  s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s²   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         C@  sD   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ d S(   s?  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(   t   superR   t   __init__t   sessiont   model_classt   key_namet	   key_valuet   property_name(   t   selfR	   R
   R   R   R   (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyR   r   s    				c         C@  s   i |  j  |  j 6} |  j j |  j  j |   } | j   } | r~ t | |  j  } | rz t	 | d  rz | j
 |   n  | Sd Sd S(   sz   Retrieve stored credential.

        Returns:
            A :class:`oauth2client.Credentials` instance or `None`.
        t	   set_storeN(   R   R   R	   t   queryR
   t	   filter_byt   firstt   getattrR   t   hasattrR   t   None(   R   t   filtersR   t   entityt
   credential(    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyt
   locked_get   s    c         C@  s|   i |  j  |  j 6} |  j j |  j  j |   } | j   } | sU |  j |   } n  t | |  j |  |  j j	 |  d S(   s   Write a credentials to the SQLAlchemy datastore.

        Args:
            credentials: :class:`oauth2client.Credentials`
        N(
   R   R   R	   R   R
   R   R   t   setattrR   t   add(   R   t   credentialsR   R   R   (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyt
   locked_put   s    c         C@  s9   i |  j  |  j 6} |  j j |  j  j |   j   d S(   s1   Delete credentials from the SQLAlchemy datastore.N(   R   R   R	   R   R
   R   t   delete(   R   R   (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyt   locked_deleteͺ   s    (   R   R   R   R   R   R   R   (    (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyR   k   s
   			(   R   t
   __future__R    t   sqlalchemy.typest
   sqlalchemyt   oauth2clientR   t   typest
   PickleTypeR   R   (    (    (    sE   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/sqlalchemy.pyt   <module>[   s
   