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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 credential = None | 109 credential = None |
110 | 110 |
111 query = {self.key_name: self.key_value} | 111 query = {self.key_name: self.key_value} |
112 entities = self.model_class.objects.filter(**query) | 112 entities = self.model_class.objects.filter(**query) |
113 if len(entities) > 0: | 113 if len(entities) > 0: |
114 credential = getattr(entities[0], self.property_name) | 114 credential = getattr(entities[0], self.property_name) |
115 if credential and hasattr(credential, 'set_store'): | 115 if credential and hasattr(credential, 'set_store'): |
116 credential.set_store(self) | 116 credential.set_store(self) |
117 return credential | 117 return credential |
118 | 118 |
119 def locked_put(self, credentials): | 119 def locked_put(self, credentials, overwrite=False): |
120 """Write a Credentials to the datastore. | 120 """Write a Credentials to the datastore. |
121 | 121 |
122 Args: | 122 Args: |
123 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. |
124 """ | 126 """ |
125 args = {self.key_name: self.key_value} | 127 args = {self.key_name: self.key_value} |
126 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 |
127 setattr(entity, self.property_name, credentials) | 134 setattr(entity, self.property_name, credentials) |
128 entity.save() | 135 entity.save() |
129 | 136 |
130 def locked_delete(self): | 137 def locked_delete(self): |
131 """Delete Credentials from the datastore.""" | 138 """Delete Credentials from the datastore.""" |
132 | 139 |
133 query = {self.key_name: self.key_value} | 140 query = {self.key_name: self.key_value} |
134 entities = self.model_class.objects.filter(**query).delete() | 141 entities = self.model_class.objects.filter(**query).delete() |
OLD | NEW |