author | Sverre Rabbelier <srabbelier@gmail.com> |
Fri, 12 Dec 2008 00:34:12 +0000 | |
changeset 721 | 6f1d29857072 |
parent 689 | 46db8c4bbffc |
child 723 | 69e5130e4a0a |
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>', |
634
ad58da0b78e8
Changed email address in logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
608
diff
changeset
|
23 |
'"Lennard de Rijk" <ljvderijk@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 |
543
280a1ac6bcc1
Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents:
535
diff
changeset
|
33 |
from soc.views import out_of_band |
308
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): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
37 |
"""Base class for all exceptions raised by this module. |
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
38 |
""" |
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
|
39 |
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
|
40 |
|
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
|
41 |
|
642
671dc971c3d1
Allow for extra dynamic fields for the create form
Sverre Rabbelier <srabbelier@gmail.com>
parents:
634
diff
changeset
|
42 |
class Logic(object): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
"""Base logic for entity classes. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
The BaseLogic class functions specific to Entity classes by relying |
655
9635cbaa2dcd
Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
642
diff
changeset
|
46 |
on arguments passed to __init__. |
308
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 |
|
671
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
49 |
def __init__(self, model, base_model=None, scope_logic=None, |
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
50 |
name=None, skip_properties=None): |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
51 |
"""Defines the name, key_name and model for this entity. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
52 |
""" |
655
9635cbaa2dcd
Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
642
diff
changeset
|
53 |
|
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
54 |
self._model = model |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
55 |
self._base_model = base_model |
671
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
56 |
self._scope_logic = scope_logic |
655
9635cbaa2dcd
Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
642
diff
changeset
|
57 |
|
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
58 |
if name: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
59 |
self._name = name |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
60 |
else: |
533
ba3309b2fd30
Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents:
512
diff
changeset
|
61 |
self._name = self._model.__name__ |
655
9635cbaa2dcd
Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
642
diff
changeset
|
62 |
|
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
63 |
if skip_properties: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
64 |
self._skip_properties = skip_properties |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
65 |
else: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
66 |
self._skip_properties = [] |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
67 |
|
606 | 68 |
def getModel(self): |
689
46db8c4bbffc
Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
679
diff
changeset
|
69 |
"""Returns the model this logic class uses. |
606 | 70 |
""" |
71 |
||
72 |
return self._model |
|
73 |
||
671
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
74 |
def getScopeLogic(self): |
689
46db8c4bbffc
Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
679
diff
changeset
|
75 |
"""Returns the logic of the enclosing scope. |
671
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
76 |
""" |
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
77 |
|
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
78 |
return self._scope_logic |
2c02178037ff
Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
669
diff
changeset
|
79 |
|
679
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
80 |
def getScopeDepth(self): |
689
46db8c4bbffc
Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
679
diff
changeset
|
81 |
"""Returns the scope depth for this entity. |
679
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
82 |
|
689
46db8c4bbffc
Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
679
diff
changeset
|
83 |
Returns None if any of the parent scopes return None. |
679
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
84 |
""" |
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
85 |
|
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
86 |
if not self._scope_logic: |
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
87 |
return 0 |
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
88 |
|
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
89 |
depth = self._scope_logic.logic.getScopeDepth() |
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
90 |
return None if (depth is None) else (depth + 1) |
77a286ff6667
Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents:
673
diff
changeset
|
91 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
def _updateField(self, model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
"""Hook called when a field is updated. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
method does not return a True value. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
return True |
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
101 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
102 |
def _onCreate(self, entity): |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
103 |
"""Called when an entity has been created. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
104 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
105 |
Classes that override this can use it to do any post-creation operations. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
106 |
""" |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
107 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
108 |
pass |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
109 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
110 |
def _onUpdate(self, entity): |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
111 |
"""Called when an entity has been updated. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
112 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
113 |
Classes that override this can use it to do any post-update operations. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
114 |
""" |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
115 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
116 |
pass |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
117 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
118 |
def _onDelete(self, entity): |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
119 |
"""Called when an entity has been deleted. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
120 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
121 |
Classes that override this can use it to do any post-deletion operations. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
122 |
""" |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
123 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
124 |
pass |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
|
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
126 |
def _keyName(self, **kwargs): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
127 |
"""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
|
128 |
|
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
|
129 |
The KeyName is in the following format: |
661
d7b643255255
Change the key name to not include entity type
Sverre Rabbelier <srabbelier@gmail.com>
parents:
655
diff
changeset
|
130 |
<key_value1>:<key_value2>:...:<key_valueN> |
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
131 |
""" |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
132 |
|
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
|
133 |
# 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
|
134 |
key_field_names = self.getKeyFieldNames() |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
135 |
|
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
|
136 |
# 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
|
137 |
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
|
138 |
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
|
139 |
|
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
|
140 |
# 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
|
141 |
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
|
142 |
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
|
143 |
|
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
|
144 |
# 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
|
145 |
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
|
146 |
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
|
147 |
|
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
|
148 |
# 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
|
149 |
keyvalues = [] |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
|
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
|
153 |
# construct the KeyName in the appropriate format |
661
d7b643255255
Change the key name to not include entity type
Sverre Rabbelier <srabbelier@gmail.com>
parents:
655
diff
changeset
|
154 |
return '/'.join(keyvalues) |
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
155 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
156 |
def getFullModelClassName(self): |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
157 |
"""Returns fully-qualified model module.class name string. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
158 |
""" |
533
ba3309b2fd30
Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents:
512
diff
changeset
|
159 |
return '%s.%s' % (self._model.__module__, self._model.__name__) |
481
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
160 |
|
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
161 |
def getKeyValues(self, entity): |
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
162 |
"""Extracts the key values from entity and returns them. |
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
163 |
|
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
164 |
The default implementation uses the scope and link_id as key values. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
165 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
166 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
167 |
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
|
168 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
169 |
|
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
170 |
return [entity.scope_path, entity.link_id] |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
171 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
172 |
def getKeyValuesFromFields(self, fields): |
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
173 |
"""Extracts the key values from a dict and returns them. |
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
174 |
|
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
175 |
The default implementation uses the scope and link_id as key values. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
176 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
177 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
178 |
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
|
179 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
180 |
|
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
181 |
return [fields['scope_path'], fields['link_id']] |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
182 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
183 |
def getKeyFieldNames(self): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
184 |
"""Returns an array with the names of the Key Fields. |
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
185 |
|
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
186 |
The default implementation uses the scope and link_id as key values. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
187 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
188 |
|
535
9045b8888772
Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents:
534
diff
changeset
|
189 |
return ['scope_path', 'link_id'] |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
190 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
191 |
def getKeySuffix(self, entity): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
192 |
"""Returns a suffix for the specified entity or None if no entity specified. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
193 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
194 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
195 |
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
|
196 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
197 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
198 |
if not entity: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
199 |
return None |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
200 |
|
669
65b2475f1cc7
Remove unneeded method getSuffixValues
Sverre Rabbelier <srabbelier@gmail.com>
parents:
661
diff
changeset
|
201 |
key_values = self.getKeyValues(entity) |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
202 |
suffix = '/'.join(key_values) |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
203 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
204 |
return suffix |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
205 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
206 |
def getKeyFieldsFromDict(self, dictionary): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
207 |
"""Does any required massaging and filtering of dictionary. |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
208 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
209 |
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
|
210 |
required translations/modifications performed. |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
211 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
212 |
Args: |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
213 |
dictionary: The arguments to massage |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
214 |
""" |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
215 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
216 |
keys = self.getKeyFieldNames() |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
217 |
values = self.getKeyValuesFromFields(dictionary) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
218 |
key_fields = dicts.zip(keys, values) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
219 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
220 |
return key_fields |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
221 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
222 |
def getFromKeyName(self, key_name): |
487
1a7591ff0051
Fixed a typo in soc.logic.models.base
Sverre Rabbelier <srabbelier@gmail.com>
parents:
481
diff
changeset
|
223 |
""""Returns entity for key_name or None if not found. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
- |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
225 |
- Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
226 |
- key_name: key name of entity |
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 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
229 |
return self._model.get_by_key_name(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
230 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
231 |
def getFromFields(self, **kwargs): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
232 |
"""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
|
233 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
234 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
235 |
**kwargs: the fields of the entity that uniquely identifies it |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
236 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
237 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
238 |
key_name = self.getKeyNameForFields(kwargs) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
239 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
240 |
if key_name: |
672
c8f9281f535b
Some more cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents:
671
diff
changeset
|
241 |
entity = self.getFromKeyName(key_name) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
242 |
else: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
243 |
entity = None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
244 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
245 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
246 |
|
592
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
247 |
def getFromFieldsOr404(self, **fields): |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
248 |
"""Like getFromFields but expects to find an entity. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
249 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
250 |
Raises: |
592
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
251 |
out_of_band.Error if no User entity is found |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
252 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
253 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
254 |
entity = self.getFromFields(**fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
255 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
256 |
if entity: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
257 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
258 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
259 |
format_text = ugettext_lazy('"%(key)s" is "%(value)s"') |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
260 |
|
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
261 |
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
|
262 |
for key, value in fields.iteritems()] |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
263 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
264 |
joined_pairs = ' and '.join(msg_pairs) |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
265 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
266 |
msg = ugettext_lazy( |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
267 |
'There is no "%(name)s" where %(pairs)s.') % { |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
268 |
'name': self._name, 'pairs': joined_pairs} |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
269 |
|
592
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
270 |
raise out_of_band.Error(msg, status=404) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
271 |
|
592
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
272 |
def getIfFields(self, fields): |
689
46db8c4bbffc
Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
679
diff
changeset
|
273 |
"""Like getFromFieldsOr404 but returns None if not all fields are set. |
592
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
274 |
|
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
275 |
Raises: |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
276 |
out_of_band.Error if no User entity is found and all fields were set |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
277 |
""" |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
278 |
|
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
279 |
if not all(fields.values()): |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
280 |
return None |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
281 |
|
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
282 |
return self.getFromFieldsOr404(**fields) |
be98a2f5d8a2
Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents:
565
diff
changeset
|
283 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
284 |
def getKeyNameForFields(self, fields): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
285 |
"""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
|
286 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
287 |
Args: |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
288 |
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
|
289 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
290 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
291 |
if not all(fields.values()): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
292 |
return None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
293 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
294 |
return self._keyName(**fields) |
308
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 |
def getForLimitAndOffset(self, limit, offset=0): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
297 |
"""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
|
298 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
299 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
300 |
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
|
301 |
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
|
302 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
303 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
304 |
query = self._model.all() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
305 |
return query.fetch(limit, offset) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
306 |
|
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
307 |
def getForFields(self, properties, unique=False, limit=1000, offset=0): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
308 |
"""Returns all entities that have the specified properties. |
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
309 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
310 |
Args: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
311 |
properties: the properties that the entity should have |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
312 |
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
|
313 |
limit: max amount of entities to return |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
314 |
offset: optional number of results to skip first; default zero. |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
315 |
""" |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
316 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
317 |
if not properties: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
318 |
raise Error("Properties did not contain any values") |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
319 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
320 |
format_text = '%(key)s = :%(key)s' |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
321 |
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
|
322 |
joined_pairs = ' AND '.join(msg_pairs) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
323 |
condition = 'WHERE %s' % joined_pairs |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
324 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
325 |
query = self._model.gql(condition, **properties) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
326 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
327 |
if unique: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
328 |
return query.get() |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
329 |
|
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
|
330 |
return query.fetch(limit, offset) |
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
331 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
332 |
def updateModelProperties(self, model, model_properties): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
333 |
"""Update existing model entity using supplied model properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
334 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
335 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
336 |
model: a model entity |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
337 |
model_properties: keyword arguments that correspond to model entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
338 |
properties and their values |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
339 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
340 |
Returns: |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
341 |
The original model entity with any supplied properties changed. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
342 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
343 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
344 |
def update(): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
345 |
return self._unsafeUpdateModelProperties(model, model_properties) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
346 |
|
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
347 |
entity = db.run_in_transaction(update) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
348 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
349 |
# call the _onUpdate method |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
350 |
self._onUpdate(entity) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
351 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
352 |
return entity |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
353 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
354 |
def _silentUpdateModelProperties(self, model, model_properties): |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
355 |
"""Update existing model entity without calling _onUpdate. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
356 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
357 |
Args: |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
358 |
model: a model entity |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
359 |
model_properties: keyword arguments that correspond to model entity |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
360 |
properties and their values |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
361 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
362 |
Returns: |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
363 |
The original model entity with any supplied properties changed. |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
364 |
""" |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
365 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
366 |
def update(): |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
367 |
return self._unsafeUpdateModelProperties(model, model_properties) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
368 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
369 |
return db.run_in_transaction(update) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
370 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
371 |
def _unsafeUpdateModelProperties(self, model, model_properties): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
372 |
"""See updateModelProperties. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
373 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
374 |
Like updateModelProperties(), but not run within a transaction. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
375 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
376 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
377 |
properties = model.properties() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
378 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
379 |
for prop in properties.values(): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
380 |
name = prop.name |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
381 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
382 |
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
|
383 |
value = model_properties[prop.name] |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
384 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
385 |
if self._updateField(model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
386 |
prop.__set__(model, value) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
387 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
388 |
model.put() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
389 |
return model |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
390 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
391 |
def updateOrCreateFromKeyName(self, properties, key_name): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
392 |
"""Update existing entity, or create new one with supplied properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
393 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
394 |
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
|
395 |
properties: dict with entity properties and their values |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
396 |
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
|
397 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
398 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
399 |
the entity corresponding to the key_name, with any supplied |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
400 |
properties changed, or a new entity now associated with the |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
401 |
supplied key_name and properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
402 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
403 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
404 |
entity = self.getFromKeyName(key_name) |
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
405 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
406 |
create_entity = not entity |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
407 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
408 |
if create_entity: |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
409 |
# entity did not exist, so create one in a transaction |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
410 |
entity = self._model.get_or_insert(key_name, **properties) |
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
411 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
412 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
413 |
# 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
|
414 |
# 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
|
415 |
# in a transaction |
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
416 |
entity = self._silentUpdateModelProperties(entity, properties) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
417 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
418 |
if create_entity: |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
419 |
# a new entity has been created call _onCreate |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
420 |
self._onCreate(entity) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
421 |
else: |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
422 |
# the entity has been updated call _onUpdate |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
423 |
self._onUpdate(entity) |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
424 |
|
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
425 |
return entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
426 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
427 |
def updateOrCreateFromFields(self, properties, fields): |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
428 |
"""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
|
429 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
430 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
431 |
# 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
|
432 |
key_name = self.getKeyNameForFields(fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
433 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
434 |
return self.updateOrCreateFromKeyName(properties, key_name) |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
435 |
|
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
436 |
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
|
437 |
"""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
|
438 |
|
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
|
439 |
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
|
440 |
entity: an existing entity in datastore |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
441 |
""" |
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
|
442 |
|
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
443 |
return True |
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
444 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
445 |
def delete(self, entity): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
446 |
"""Delete existing entity from datastore. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
447 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
448 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
449 |
entity: an existing entity in datastore |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
450 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
451 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
452 |
entity.delete() |
608
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
453 |
# entity has been deleted call _onDelete |
77bffba4e946
Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
606
diff
changeset
|
454 |
self._onDelete(entity) |