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