Cleaned up OAuth 2.0 support fully using Storage() and updating samples.
diff --git a/oauth2client/django_orm.py b/oauth2client/django_orm.py
index 50b3dd6..b5e020e 100644
--- a/oauth2client/django_orm.py
+++ b/oauth2client/django_orm.py
@@ -1,5 +1,5 @@
from django.db import models
-
+from oauth2client.client import Storage as BaseStorage
class CredentialsField(models.Field):
@@ -36,3 +36,50 @@
def get_db_prep_value(self, value):
return base64.b64encode(pickle.dumps(value))
+
+
+class Storage(BaseStorage):
+ """Store and retrieve a single credential to and from
+ the datastore.
+
+ This Storage helper presumes the Credentials
+ have been stored as a CredenialsField
+ on a db model class.
+ """
+
+ def __init__(self, model_class, key_name, key_value, property_name):
+ """Constructor for Storage.
+
+ Args:
+ model: 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
+ """
+ self.model_class = model_class
+ self.key_name = key_name
+ self.key_value = key_value
+ self.property_name = property_name
+
+ def get(self):
+ """Retrieve Credential from datastore.
+
+ Returns:
+ oauth2client.Credentials
+ """
+ query = {self.key_name: self.key_value}
+ entity = self.model_class.objects.filter(*query)[0]
+ credential = getattr(entity, self.property_name)
+ if credential and hasattr(credential, 'set_store'):
+ credential.set_store(self.put)
+ return credential
+
+ def put(self, credentials):
+ """Write a Credentials to the datastore.
+
+ Args:
+ credentials: Credentials, the credentials to store.
+ """
+ entity = self.model_class(self.key_name=self.key_value)
+ setattr(entity, self.property_name, credentials)
+ entity.save()