
                         >    d Z ddlmZ  G d dej                        Zy)zGContains a storage module that stores credentials using the Django ORM.    )clientc                   4     e Zd ZdZ fdZd Zd Zd Z xZS )DjangoORMStoragezStore and retrieve a single credential to and from the Django datastore.

    This Storage helper presumes the Credentials
    have been stored as a CredentialsField
    on a db model class.
    c                 b    t         t        |           || _        || _        || _        || _        y)a  Constructor for Storage.

        Args:
            model: string, fully qualified name of db.Model model class.
            key_name: string, key name for the entity that has the credentials
            key_value: string, key value for the entity that has the
               credentials.
            property_name: string, name of the property that is an
                           CredentialsProperty.
        N)superr   __init__model_classkey_name	key_valueproperty_name)selfr	   r
   r   r   	__class__s        ;lib/third_party/oauth2client/contrib/django_util/storage.pyr   zDjangoORMStorage.__init__   s1     	.0& "*    c                    | j                   | j                  i} | j                  j                  j                  di |}t        |      dkD  r9t        |d   | j                        }t        |dd      |j                  |        |S y)a  Retrieve stored credential from the Django ORM.

        Returns:
            oauth2client.Credentials retrieved from the Django ORM, associated
             with the ``model``, ``key_value``->``key_name`` pair used to query
             for the model, and ``property_name`` identifying the
             ``CredentialsProperty`` field, all of which are defined in the
             constructor for this Storage object.

        r   	set_storeN )	r
   r   r	   objectsfilterlengetattrr   r   )r   queryentities
credentials       r   
locked_getzDjangoORMStorage.locked_get-   s     /24##++22;U;x=1 !d.@.@AJz;5A$$T*r   c                      | j                   j                  j                  di | j                  | j                  i\  }}t        || j                  |       |j                          y)zWrite a Credentials to the Django datastore.

        Args:
            credentials: Credentials, the credentials to store.
        Nr   )r	   r   get_or_creater
   r   setattrr   save)r   credentialsentity_s       r   
locked_putzDjangoORMStorage.locked_putB   sX     ;D$$,,:: /}}dnn-/	 	**K8r   c                     | j                   | j                  i} | j                  j                  j                  di |j                          y)z&Delete Credentials from the datastore.Nr   )r
   r   r	   r   r   delete)r   r   s     r   locked_deletezDjangoORMStorage.locked_deleteN   s<    /'  ''0%0779r   )	__name__
__module____qualname____doc__r   r   r#   r&   __classcell__)r   s   @r   r   r      s    +"*
:r   r   N)r*   oauth2clientr   Storager   r   r   r   <module>r.      s    N =:v~~ =:r   