| OLD | NEW |
| 1 # Copyright (C) 2010 Google Inc. | 1 # Copyright 2014 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 24 import base64 | 24 import base64 |
| 25 import pickle | 25 import pickle |
| 26 | 26 |
| 27 from django.db import models | 27 from django.db import models |
| 28 from oauth2client.client import Storage as BaseStorage | 28 from oauth2client.client import Storage as BaseStorage |
| 29 | 29 |
| 30 class CredentialsField(models.Field): | 30 class CredentialsField(models.Field): |
| 31 | 31 |
| 32 __metaclass__ = models.SubfieldBase | 32 __metaclass__ = models.SubfieldBase |
| 33 | 33 |
| 34 def __init__(self, *args, **kwargs): |
| 35 if 'null' not in kwargs: |
| 36 kwargs['null'] = True |
| 37 super(CredentialsField, self).__init__(*args, **kwargs) |
| 38 |
| 34 def get_internal_type(self): | 39 def get_internal_type(self): |
| 35 return "TextField" | 40 return "TextField" |
| 36 | 41 |
| 37 def to_python(self, value): | 42 def to_python(self, value): |
| 38 if not value: | 43 if value is None: |
| 39 return None | 44 return None |
| 40 if isinstance(value, oauth2client.client.Credentials): | 45 if isinstance(value, oauth2client.client.Credentials): |
| 41 return value | 46 return value |
| 42 return pickle.loads(base64.b64decode(value)) | 47 return pickle.loads(base64.b64decode(value)) |
| 43 | 48 |
| 44 def get_db_prep_value(self, value, connection, prepared=False): | 49 def get_db_prep_value(self, value, connection, prepared=False): |
| 50 if value is None: |
| 51 return None |
| 45 return base64.b64encode(pickle.dumps(value)) | 52 return base64.b64encode(pickle.dumps(value)) |
| 46 | 53 |
| 47 | 54 |
| 48 class FlowField(models.Field): | 55 class FlowField(models.Field): |
| 49 | 56 |
| 50 __metaclass__ = models.SubfieldBase | 57 __metaclass__ = models.SubfieldBase |
| 51 | 58 |
| 59 def __init__(self, *args, **kwargs): |
| 60 if 'null' not in kwargs: |
| 61 kwargs['null'] = True |
| 62 super(FlowField, self).__init__(*args, **kwargs) |
| 63 |
| 52 def get_internal_type(self): | 64 def get_internal_type(self): |
| 53 return "TextField" | 65 return "TextField" |
| 54 | 66 |
| 55 def to_python(self, value): | 67 def to_python(self, value): |
| 56 if value is None: | 68 if value is None: |
| 57 return None | 69 return None |
| 58 if isinstance(value, oauth2client.client.Flow): | 70 if isinstance(value, oauth2client.client.Flow): |
| 59 return value | 71 return value |
| 60 return pickle.loads(base64.b64decode(value)) | 72 return pickle.loads(base64.b64decode(value)) |
| 61 | 73 |
| 62 def get_db_prep_value(self, value, connection, prepared=False): | 74 def get_db_prep_value(self, value, connection, prepared=False): |
| 75 if value is None: |
| 76 return None |
| 63 return base64.b64encode(pickle.dumps(value)) | 77 return base64.b64encode(pickle.dumps(value)) |
| 64 | 78 |
| 65 | 79 |
| 66 class Storage(BaseStorage): | 80 class Storage(BaseStorage): |
| 67 """Store and retrieve a single credential to and from | 81 """Store and retrieve a single credential to and from |
| 68 the datastore. | 82 the datastore. |
| 69 | 83 |
| 70 This Storage helper presumes the Credentials | 84 This Storage helper presumes the Credentials |
| 71 have been stored as a CredenialsField | 85 have been stored as a CredenialsField |
| 72 on a db model class. | 86 on a db model class. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 95 credential = None | 109 credential = None |
| 96 | 110 |
| 97 query = {self.key_name: self.key_value} | 111 query = {self.key_name: self.key_value} |
| 98 entities = self.model_class.objects.filter(**query) | 112 entities = self.model_class.objects.filter(**query) |
| 99 if len(entities) > 0: | 113 if len(entities) > 0: |
| 100 credential = getattr(entities[0], self.property_name) | 114 credential = getattr(entities[0], self.property_name) |
| 101 if credential and hasattr(credential, 'set_store'): | 115 if credential and hasattr(credential, 'set_store'): |
| 102 credential.set_store(self) | 116 credential.set_store(self) |
| 103 return credential | 117 return credential |
| 104 | 118 |
| 105 def locked_put(self, credentials): | 119 def locked_put(self, credentials, overwrite=False): |
| 106 """Write a Credentials to the datastore. | 120 """Write a Credentials to the datastore. |
| 107 | 121 |
| 108 Args: | 122 Args: |
| 109 credentials: Credentials, the credentials to store. | 123 credentials: Credentials, the credentials to store. |
| 124 overwrite: Boolean, indicates whether you would like these credentials to |
| 125 overwrite any existing stored credentials. |
| 110 """ | 126 """ |
| 111 args = {self.key_name: self.key_value} | 127 args = {self.key_name: self.key_value} |
| 112 entity = self.model_class(**args) | 128 |
| 129 if overwrite: |
| 130 entity, unused_is_new = self.model_class.objects.get_or_create(**args) |
| 131 else: |
| 132 entity = self.model_class(**args) |
| 133 |
| 113 setattr(entity, self.property_name, credentials) | 134 setattr(entity, self.property_name, credentials) |
| 114 entity.save() | 135 entity.save() |
| 136 |
| 137 def locked_delete(self): |
| 138 """Delete Credentials from the datastore.""" |
| 139 |
| 140 query = {self.key_name: self.key_value} |
| 141 entities = self.model_class.objects.filter(**query).delete() |
| OLD | NEW |