|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Helpers functions for updating different kinds of Expando models. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 from soc.logic.models import base |
|
26 |
|
27 |
|
28 class Logic(base.Logic): |
|
29 """Base logic for Expando entity classes. |
|
30 """ |
|
31 |
|
32 def __init__(self, model, base_model=None, scope_logic=None, |
|
33 name=None, skip_properties=None, id_based=False): |
|
34 """Defines the name, key_name and model for this entity. |
|
35 """ |
|
36 |
|
37 super(Logic, self).__init__(model=model, base_model=base_model, |
|
38 scope_logic=scope_logic, name=name, |
|
39 skip_properties=skip_properties, |
|
40 id_based=id_based) |
|
41 |
|
42 def updateEntityProperties(self, entity, entity_properties, silent=False): |
|
43 """Update existing entity using supplied properties. |
|
44 |
|
45 Overwrites base because of Expando properties. |
|
46 |
|
47 Args: |
|
48 entity: a model entity |
|
49 entity_properties: keyword arguments that correspond to entity |
|
50 properties and their values |
|
51 silent: iff True does not call _onUpdate method |
|
52 |
|
53 Returns: |
|
54 The original entity with any supplied properties changed. |
|
55 """ |
|
56 |
|
57 if not entity: |
|
58 raise NoEntityError |
|
59 |
|
60 if not entity_properties: |
|
61 raise InvalidArgumentError |
|
62 |
|
63 for name, value in entity_properties.iteritems(): |
|
64 # if the property is not to be updated, skip it |
|
65 if name in self._skip_properties: |
|
66 continue |
|
67 |
|
68 if self._updateField(entity, entity_properties, name): |
|
69 setattr(entity, name, value) |
|
70 |
|
71 entity.put() |
|
72 |
|
73 # call the _onUpdate method |
|
74 if not silent: |
|
75 self._onUpdate(entity) |
|
76 |
|
77 return entity |