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