Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1 | from django.db import models |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame^] | 2 | from oauth2client.client import Storage as BaseStorage |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 3 | |
| 4 | class CredentialsField(models.Field): |
| 5 | |
| 6 | __metaclass__ = models.SubfieldBase |
| 7 | |
| 8 | def db_type(self): |
| 9 | return 'VARCHAR' |
| 10 | |
| 11 | def to_python(self, value): |
| 12 | if value is None: |
| 13 | return None |
| 14 | if isinstance(value, oauth2client.Credentials): |
| 15 | return value |
| 16 | return pickle.loads(base64.b64decode(value)) |
| 17 | |
| 18 | def get_db_prep_value(self, value): |
| 19 | return base64.b64encode(pickle.dumps(value)) |
| 20 | |
| 21 | |
| 22 | class FlowField(models.Field): |
| 23 | |
| 24 | __metaclass__ = models.SubfieldBase |
| 25 | |
| 26 | def db_type(self): |
| 27 | return 'VARCHAR' |
| 28 | |
| 29 | def to_python(self, value): |
| 30 | print "In to_python", value |
| 31 | if value is None: |
| 32 | return None |
| 33 | if isinstance(value, oauth2client.Flow): |
| 34 | return value |
| 35 | return pickle.loads(base64.b64decode(value)) |
| 36 | |
| 37 | def get_db_prep_value(self, value): |
| 38 | return base64.b64encode(pickle.dumps(value)) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame^] | 39 | |
| 40 | |
| 41 | class Storage(BaseStorage): |
| 42 | """Store and retrieve a single credential to and from |
| 43 | the datastore. |
| 44 | |
| 45 | This Storage helper presumes the Credentials |
| 46 | have been stored as a CredenialsField |
| 47 | on a db model class. |
| 48 | """ |
| 49 | |
| 50 | def __init__(self, model_class, key_name, key_value, property_name): |
| 51 | """Constructor for Storage. |
| 52 | |
| 53 | Args: |
| 54 | model: db.Model, model class |
| 55 | key_name: string, key name for the entity that has the credentials |
| 56 | key_value: string, key value for the entity that has the credentials |
| 57 | property_name: string, name of the property that is an CredentialsProperty |
| 58 | """ |
| 59 | self.model_class = model_class |
| 60 | self.key_name = key_name |
| 61 | self.key_value = key_value |
| 62 | self.property_name = property_name |
| 63 | |
| 64 | def get(self): |
| 65 | """Retrieve Credential from datastore. |
| 66 | |
| 67 | Returns: |
| 68 | oauth2client.Credentials |
| 69 | """ |
| 70 | query = {self.key_name: self.key_value} |
| 71 | entity = self.model_class.objects.filter(*query)[0] |
| 72 | credential = getattr(entity, self.property_name) |
| 73 | if credential and hasattr(credential, 'set_store'): |
| 74 | credential.set_store(self.put) |
| 75 | return credential |
| 76 | |
| 77 | def put(self, credentials): |
| 78 | """Write a Credentials to the datastore. |
| 79 | |
| 80 | Args: |
| 81 | credentials: Credentials, the credentials to store. |
| 82 | """ |
| 83 | entity = self.model_class(self.key_name=self.key_value) |
| 84 | setattr(entity, self.property_name, credentials) |
| 85 | entity.save() |