OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Versioned singleton entity with the global configuration.""" | 5 """Versioned singleton entity with the global configuration.""" |
6 | 6 |
7 import logging | 7 import logging |
8 | 8 |
9 from google.appengine.api import users | |
10 from google.appengine.ext import ndb | 9 from google.appengine.ext import ndb |
11 | 10 |
12 from model.versioned_model import VersionedModel | 11 from model.versioned_model import VersionedModel |
13 | 12 |
14 | 13 |
15 class VersionedConfig(VersionedModel): | 14 class VersionedConfig(VersionedModel): |
16 """Singleton entity with the global configuration of the service. | 15 """Singleton entity with the global configuration of the service. |
17 | 16 |
18 All changes are stored in the revision log. | 17 All changes are stored in the revision log. |
19 """ | 18 """ |
20 | 19 |
21 # When this revision of configuration was created. | 20 # When this revision of configuration was created. |
22 updated_ts = ndb.DateTimeProperty(indexed=False, auto_now=True) | 21 updated_ts = ndb.DateTimeProperty(indexed=False, auto_now=True) |
23 | 22 |
24 # Who created this revision of configuration. | 23 # Who created this revision of configuration. |
25 updated_by = ndb.StringProperty(indexed=False) | 24 updated_by = ndb.StringProperty(indexed=False) |
26 | 25 |
27 @classmethod | 26 @classmethod |
28 def Get(cls, version=None): | 27 def Get(cls, version=None): |
29 """Returns the version of the config entity, the latest if not specified.""" | 28 """Returns the version of the config entity, the latest if not specified.""" |
30 config_data = cls.GetVersion(version) | 29 config_data = cls.GetVersion(version) |
31 return config_data or cls() if version is None else config_data | 30 return config_data or cls() if version is None else config_data |
32 | 31 |
33 def Update(self, **kwargs): | 32 def Update(self, user, is_admin, **kwargs): |
34 """Applies |kwargs| dict to the entity and stores the entity if changed.""" | 33 """Applies |kwargs| dict to the entity and stores the entity if changed.""" |
35 if not users.is_current_user_admin(): | 34 if not is_admin: |
36 raise Exception('Only admin could update config.') | 35 raise Exception('Only admin could update config.') |
37 | 36 |
38 dirty = False | 37 dirty = False |
39 for k, v in kwargs.iteritems(): | 38 for k, v in kwargs.iteritems(): |
40 assert k in self._properties, k | 39 assert k in self._properties, k |
41 if getattr(self, k) != v: | 40 if getattr(self, k) != v: |
42 setattr(self, k, v) | 41 setattr(self, k, v) |
43 dirty = True | 42 dirty = True |
44 | 43 |
45 if dirty: | 44 if dirty: |
46 user_name = users.get_current_user().email().split('@')[0] | 45 user_name = user.email().split('@')[0] |
47 self.updated_by = user_name | 46 self.updated_by = user_name |
48 self.Save() | 47 self.Save() |
49 logging.info('Config %s was updated by %s', self.__class__, user_name) | 48 logging.info('Config %s was updated by %s', self.__class__, user_name) |
50 | 49 |
51 return dirty | 50 return dirty |
OLD | NEW |