author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Fri, 07 Nov 2008 22:13:20 +0000 | |
changeset 447 | 83b49d60effd |
parent 442 | 92c17629af0e |
child 459 | 2cfcedaf7c16 |
permissions | -rw-r--r-- |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Helpers functions for updating different kinds of models in datastore. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
429
e50e18936f06
Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents:
410
diff
changeset
|
22 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
23 |
'"Lennard de Rijk" <rijk0214@gmail.com>', |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
'"Pawel Solyga" <pawel.solyga@gmail.com>', |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
] |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
from google.appengine.ext import db |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
30 |
from django.utils.translation import ugettext_lazy |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
31 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
32 |
from soc.logic import dicts |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
from soc.logic import out_of_band |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
|
441
8a7110ad3d82
Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
436
diff
changeset
|
36 |
class Error(Exception): |
8a7110ad3d82
Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
436
diff
changeset
|
37 |
"""Base class for all exceptions raised by this module.""" |
8a7110ad3d82
Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
436
diff
changeset
|
38 |
pass |
8a7110ad3d82
Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
436
diff
changeset
|
39 |
|
8a7110ad3d82
Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
436
diff
changeset
|
40 |
|
389
9b873166d7d5
Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
387
diff
changeset
|
41 |
class Logic: |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
"""Base logic for entity classes. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
The BaseLogic class functions specific to Entity classes by relying |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
on the the child-classes to implement _model, _name and _key_name |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
def _updateField(self, model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
"""Hook called when a field is updated. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
Base classes should override if any special actions need to be |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
taken when a field is updated. The field is not updated if the |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
method does not return a True value. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
return True |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
|
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
58 |
def _keyName(self, **kwargs): |
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
59 |
"""Returns the KeyName constructed from kwargs for this type of entity |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
60 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
61 |
The KeyName is in the following format: |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
62 |
entity.name:<key_value1>:<key_value2>:...:<key_valueN> |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
63 |
""" |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
64 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
65 |
# get the KeyFieldNames for this entity |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
66 |
key_field_names = self.getKeyFieldNames() |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
67 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
68 |
# check if all given KeyFieldNames are valid for this entity |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
69 |
if not all(key in key_field_names for key in kwargs.keys()): |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
70 |
raise Error("Some of the provided arguments are not key fields") |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
71 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
72 |
# check if all key_field_names for this entity are present in kwargs |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
73 |
if not all(field in kwargs.keys() for field in key_field_names): |
447
83b49d60effd
Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
442
diff
changeset
|
74 |
raise Error("Not all the required key fields are present") |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
75 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
76 |
# check if all kwargs.values() are non-false |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
77 |
if not all(kwargs.values()): |
447
83b49d60effd
Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
442
diff
changeset
|
78 |
raise Error("Not all KeyValues are non-false") |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
79 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
80 |
# construct the KeyValues in the order given by getKeyFieldNames() |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
81 |
keyvalues = [] |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
82 |
for key_field_name in key_field_names: |
447
83b49d60effd
Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
442
diff
changeset
|
83 |
keyvalues.append(kwargs[key_field_name]) |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
84 |
|
436
f3c313d54aa4
Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
435
diff
changeset
|
85 |
# construct the KeyName in the appropriate format |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
86 |
return ":".join([self._name] + keyvalues) |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
87 |
|
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
88 |
def getKeyValues(self, entity): |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
89 |
"""Exctracts the key values from entity and returns them |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
90 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
91 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
92 |
entity: the entity from which to extract the key values |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
93 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
94 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
95 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
96 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
97 |
def getKeyValuesFromFields(self, fields): |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
98 |
"""Exctracts the key values from a dict and returns them |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
99 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
100 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
101 |
fields: the dict from which to extract the key values |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
102 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
103 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
104 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
105 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
106 |
def getKeyFieldNames(self): |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
107 |
"""Returns an array with the names of the Key Fields |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
108 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
109 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
110 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
111 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
112 |
def getKeySuffix(self, entity): |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
113 |
"""Returns a suffix for the specified entity or None if no entity specified |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
114 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
115 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
116 |
entity: the entity for which to get the suffix |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
117 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
118 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
119 |
if not entity: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
120 |
return None |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
121 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
122 |
key_values = self.getKeyValues(entity) |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
123 |
suffix = '/'.join(key_values) |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
124 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
125 |
return suffix |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
126 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
127 |
def getKeyFieldsFromDict(self, dictionary): |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
128 |
"""Does any required massaging and filtering of dictionary |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
129 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
130 |
The resulting dictionary contains just the key names, and has any |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
131 |
required translations/modifications performed. |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
132 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
133 |
Args: |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
134 |
dictionary: The arguments to massage |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
135 |
""" |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
136 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
137 |
keys = self.getKeyFieldNames() |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
138 |
values = self.getKeyValuesFromFields(dictionary) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
139 |
key_fields = dicts.zip(keys, values) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
140 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
141 |
return key_fields |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
142 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
def getFromKeyName(self, key_name): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
""""Returns User entity for key_name or None if not found. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
- |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
- Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
- key_name: key name of entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
150 |
return self._model.get_by_key_name(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
151 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
def getFromFields(self, **kwargs): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
153 |
"""Returns the entity for the specified key names, or None if not found. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
**kwargs: the fields of the entity that uniquely identifies it |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
159 |
key_name = self.getKeyNameForFields(kwargs) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
if key_name: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
entity = self._model.get_by_key_name(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
else: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
entity = None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
168 |
def getIfFields(self, fields): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
169 |
"""Returns entity for supplied link name if one exists. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
170 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
171 |
Args: |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
172 |
fields: the fields of the entity that uniquely identifies it |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
173 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
174 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
175 |
* None if a field is false. |
317
87215136c268
Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
308
diff
changeset
|
176 |
* Entity for supplied fields |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
177 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
178 |
Raises: |
354
b931c74abbee
Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
317
diff
changeset
|
179 |
out_of_band.ErrorResponse if link name is not false, but no entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
180 |
with the supplied link name exists in the Datastore |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
181 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
182 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
183 |
if not all(fields.values()): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
184 |
# exit without error, to let view know that link_name was not supplied |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
185 |
return None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
186 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
187 |
entity = self.getFromFields(**fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
188 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
189 |
if entity: |
354
b931c74abbee
Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
317
diff
changeset
|
190 |
# an entity exist for this link_name, so return that entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
191 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
192 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
193 |
format_text = ugettext_lazy('"%(key)s" is "%(value)s"') |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
194 |
|
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
195 |
msg_pairs = [format_text % {'key': key, 'value': value} |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
196 |
for key, value in fields.iteritems()] |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
197 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
198 |
joined_pairs = ' and '.join(msg_pairs) |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
199 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
200 |
msg = ugettext_lazy( |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
201 |
'There is no "%(name)s" where %(pairs)s.') % { |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
202 |
'name': self._name, 'pairs': joined_pairs} |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
203 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
204 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
205 |
# else: fields were supplied, but there is no Entity that has it |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
206 |
raise out_of_band.ErrorResponse(msg, status=404) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
208 |
def getKeyNameForFields(self, fields): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
209 |
"""Return a Datastore key_name for a Entity from the specified fields. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
210 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
211 |
Args: |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
212 |
fields: the fields of the entity that uniquely identifies it |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
214 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
215 |
if not all(fields.values()): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
216 |
return None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
217 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
218 |
return self._keyName(**fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
219 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
220 |
def getForLimitAndOffset(self, limit, offset=0): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
221 |
"""Returns entities for given offset and limit or None if not found. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
222 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
223 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
limit: max amount of entities to return |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
225 |
offset: optional number of results to skip first; default zero. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
226 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
227 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
228 |
query = self._model.all() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
229 |
return query.fetch(limit, offset) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
230 |
|
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
231 |
def getForFields(self, properties, unique=False, limit=1000, offset=0): |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
232 |
"""Returns all entities that have the specified properties |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
233 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
234 |
Args: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
235 |
properties: the properties that the entity should have |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
236 |
unique: if set, only the first item from the resultset will be returned |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
237 |
limit: max amount of entities to return |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
238 |
offset: optional number of results to skip first; default zero. |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
239 |
""" |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
240 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
241 |
if not properties: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
242 |
raise Error("Properties did not contain any values") |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
243 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
244 |
format_text = '%(key)s = :%(key)s' |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
245 |
msg_pairs = [format_text % {'key': key} for key in properties.iterkeys()] |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
246 |
joined_pairs = ' AND '.join(msg_pairs) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
247 |
condition = 'WHERE %s' % joined_pairs |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
248 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
249 |
query = self._model.gql(condition, **properties) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
250 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
251 |
if unique: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
252 |
return query.get() |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
253 |
|
447
83b49d60effd
Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
442
diff
changeset
|
254 |
return query.fetch(limit, offset) |
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
255 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
256 |
def updateModelProperties(self, model, model_properties): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
257 |
"""Update existing model entity using supplied model properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
258 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
259 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
260 |
model: a model entity |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
261 |
model_properties: keyword arguments that correspond to model entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
262 |
properties and their values |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
263 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
264 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
265 |
the original model entity with any supplied properties changed |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
266 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
267 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
268 |
def update(): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
269 |
return self._unsafeUpdateModelProperties(model, model_properties) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
270 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
271 |
return db.run_in_transaction(update) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
272 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
273 |
def _unsafeUpdateModelProperties(self, model, model_properties): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
274 |
"""(see updateModelProperties) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
275 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
276 |
Like updateModelProperties(), but not run within a transaction. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
277 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
278 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
279 |
properties = model.properties() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
280 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
281 |
for prop in properties.values(): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
282 |
name = prop.name |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
283 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
284 |
if not name in self._skip_properties and name in model_properties: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
285 |
value = model_properties[prop.name] |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
286 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
287 |
if self._updateField(model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
288 |
prop.__set__(model, value) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
289 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
290 |
model.put() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
291 |
return model |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
292 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
293 |
def updateOrCreateFromKeyName(self, properties, key_name): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
294 |
"""Update existing entity, or create new one with supplied properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
295 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
296 |
Args: |
317
87215136c268
Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
308
diff
changeset
|
297 |
properties: dict with entity properties and their values |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
298 |
key_name: the key_name of the entity that uniquely identifies it |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
299 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
300 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
301 |
the entity corresponding to the key_name, with any supplied |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
302 |
properties changed, or a new entity now associated with the |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
303 |
supplied key_name and properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
304 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
305 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
306 |
entity = self.getFromKeyName(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
307 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
308 |
if not entity: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
309 |
# entity did not exist, so create one in a transaction |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
310 |
entity = self._model.get_or_insert(key_name, **properties) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
311 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
312 |
# there is no way to be sure if get_or_insert() returned a new entity or |
354
b931c74abbee
Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
317
diff
changeset
|
313 |
# got an existing one due to a race, so update with properties anyway, |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
314 |
# in a transaction |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
315 |
return self.updateModelProperties(entity, properties) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
316 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
317 |
def updateOrCreateFromFields(self, properties, fields): |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
318 |
"""Like updateOrCreateFromKeyName, but resolves fields to a key_name first. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
319 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
320 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
321 |
# attempt to retrieve the existing entity |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
322 |
key_name = self.getKeyNameForFields(fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
323 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
324 |
return self.updateOrCreateFromKeyName(properties, key_name) |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
325 |
|
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
326 |
def isDeletable(self, entity): |
368
f90f9b22751a
Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
363
diff
changeset
|
327 |
"""Returns whether the specified entity can be deleted. |
f90f9b22751a
Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
363
diff
changeset
|
328 |
|
f90f9b22751a
Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
363
diff
changeset
|
329 |
Args: |
f90f9b22751a
Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
363
diff
changeset
|
330 |
entity: an existing entity in datastore |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
331 |
""" |
368
f90f9b22751a
Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
363
diff
changeset
|
332 |
|
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
333 |
return True |
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
334 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
335 |
def delete(self, entity): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
336 |
"""Delete existing entity from datastore. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
337 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
338 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
339 |
entity: an existing entity in datastore |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
340 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
341 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
342 |
entity.delete() |