author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Thu, 20 Nov 2008 22:51:07 +0000 | |
changeset 524 | 774b379e058c |
parent 512 | aae25d2b4464 |
child 533 | ba3309b2fd30 |
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): |
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 |
|
389
9b873166d7d5
Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
387
diff
changeset
|
42 |
class Logic: |
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 |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
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
|
47 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
|
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
|
49 |
def __init__(self, model, base_model=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
|
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 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
53 |
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
|
54 |
self._base_model = base_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 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
self._name = self.getModelClassName() |
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 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
def _updateField(self, model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
"""Hook called when a field is updated. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
method does not return a True value. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
return True |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
|
432
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
76 |
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
|
77 |
"""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
|
78 |
|
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
|
79 |
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
|
80 |
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
|
81 |
""" |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
82 |
|
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
|
83 |
# 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
|
84 |
key_field_names = self.getKeyFieldNames() |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
85 |
|
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
|
86 |
# 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
|
87 |
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
|
88 |
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
|
89 |
|
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
|
90 |
# 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
|
91 |
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
|
92 |
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
|
93 |
|
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
|
94 |
# 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
|
95 |
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
|
96 |
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
|
97 |
|
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
|
98 |
# 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
|
99 |
keyvalues = [] |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
|
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
|
103 |
# 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
|
104 |
return ":".join([self._name] + keyvalues) |
1851d67a1004
Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents:
429
diff
changeset
|
105 |
|
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
|
106 |
def getModelClassName(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
|
107 |
"""Returns model 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
|
108 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
109 |
return self._model.__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
|
110 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
111 |
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
|
112 |
"""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
|
113 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
114 |
return '%s.%s' % (self._model.__module__, |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
115 |
self.getModelClassName()) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
116 |
|
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
117 |
def getKeyValues(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
|
118 |
"""Exctracts the key values from entity and returns them. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
119 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
120 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
121 |
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
|
122 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
123 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
124 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
125 |
|
459
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
126 |
def getSuffixValues(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
|
127 |
"""Returns an array with the public values of the Key Fields. |
459
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
128 |
|
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
129 |
Defaults to getKeyValues |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
130 |
If any of the Key Fields should not be made public, this method |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
131 |
should return their public counterparts instead. |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
132 |
|
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
133 |
Args: |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
134 |
entity: the entity for which to construct the suffix |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
135 |
""" |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
136 |
|
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
137 |
return self.getKeyValues(entity) |
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
138 |
|
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
139 |
def getKeyValuesFromFields(self, fields): |
499
d22e4fe8e64b
Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
487
diff
changeset
|
140 |
"""Exctracts the key values from a dict and returns them. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
141 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
142 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
143 |
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
|
144 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
145 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
146 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
147 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
148 |
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
|
149 |
"""Returns an array with the names of the Key Fields. |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
150 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
151 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
152 |
raise NotImplementedError |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
153 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
154 |
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
|
155 |
"""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
|
156 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
157 |
Args: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
158 |
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
|
159 |
""" |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
160 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
161 |
if not entity: |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
162 |
return None |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
163 |
|
459
2cfcedaf7c16
Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
447
diff
changeset
|
164 |
key_values = self.getSuffixValues(entity) |
410
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
165 |
suffix = '/'.join(key_values) |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
166 |
|
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
167 |
return suffix |
2af7f84f4fc7
Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
407
diff
changeset
|
168 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
169 |
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
|
170 |
"""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
|
171 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
172 |
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
|
173 |
required translations/modifications performed. |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
174 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
175 |
Args: |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
176 |
dictionary: The arguments to massage |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
177 |
""" |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
178 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
179 |
keys = self.getKeyFieldNames() |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
180 |
values = self.getKeyValuesFromFields(dictionary) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
181 |
key_fields = dicts.zip(keys, values) |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
182 |
|
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
183 |
return key_fields |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
184 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
185 |
def getFromKeyName(self, key_name): |
487
1a7591ff0051
Fixed a typo in soc.logic.models.base
Sverre Rabbelier <srabbelier@gmail.com>
parents:
481
diff
changeset
|
186 |
""""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
|
187 |
- |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
188 |
- Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
189 |
- key_name: key name of entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
190 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
191 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
192 |
return self._model.get_by_key_name(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
193 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
194 |
def getFromFields(self, **kwargs): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
195 |
"""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
|
196 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
197 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
198 |
**kwargs: the fields of the entity that uniquely identifies it |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
199 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
200 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
201 |
key_name = self.getKeyNameForFields(kwargs) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
202 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
203 |
if key_name: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
204 |
entity = self._model.get_by_key_name(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
205 |
else: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
206 |
entity = None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
208 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
209 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
210 |
def getIfFields(self, fields): |
512
aae25d2b4464
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents:
499
diff
changeset
|
211 |
"""Returns entity for supplied link ID if one exists. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
212 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
Args: |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
214 |
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
|
215 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
216 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
217 |
* 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
|
218 |
* Entity for supplied 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 |
Raises: |
512
aae25d2b4464
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents:
499
diff
changeset
|
221 |
out_of_band.ErrorResponse if link ID is not false, but no entity |
aae25d2b4464
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents:
499
diff
changeset
|
222 |
with the supplied link ID exists in the Datastore. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
223 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
225 |
if not all(fields.values()): |
512
aae25d2b4464
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents:
499
diff
changeset
|
226 |
# exit without error, to let view know that link_id was not supplied |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
227 |
return None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
228 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
229 |
entity = self.getFromFields(**fields) |
308
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 |
if entity: |
512
aae25d2b4464
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents:
499
diff
changeset
|
232 |
# an entity exist for this link_id, so return that entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
233 |
return entity |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
234 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
235 |
format_text = ugettext_lazy('"%(key)s" is "%(value)s"') |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
236 |
|
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
237 |
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
|
238 |
for key, value in fields.iteritems()] |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
239 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
240 |
joined_pairs = ' and '.join(msg_pairs) |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
241 |
|
402
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
242 |
msg = ugettext_lazy( |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
243 |
'There is no "%(name)s" where %(pairs)s.') % { |
021e86368600
Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents:
399
diff
changeset
|
244 |
'name': self._name, 'pairs': joined_pairs} |
396
6af084bd290c
Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents:
390
diff
changeset
|
245 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
246 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
247 |
# 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
|
248 |
raise out_of_band.ErrorResponse(msg, status=404) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
249 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
250 |
def getKeyNameForFields(self, fields): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
251 |
"""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
|
252 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
253 |
Args: |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
254 |
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
|
255 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
256 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
257 |
if not all(fields.values()): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
258 |
return None |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
259 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
260 |
return self._keyName(**fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
261 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
262 |
def getForLimitAndOffset(self, limit, offset=0): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
263 |
"""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
|
264 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
265 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
269 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
270 |
query = self._model.all() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
271 |
return query.fetch(limit, offset) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
272 |
|
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
273 |
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
|
274 |
"""Returns all entities that have the specified properties. |
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
275 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
276 |
Args: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
277 |
properties: the properties that the entity should have |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
278 |
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
|
279 |
limit: max amount of entities to return |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
280 |
offset: optional number of results to skip first; default zero. |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
281 |
""" |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
282 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
283 |
if not properties: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
284 |
raise Error("Properties did not contain any values") |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
285 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
286 |
format_text = '%(key)s = :%(key)s' |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
287 |
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
|
288 |
joined_pairs = ' AND '.join(msg_pairs) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
289 |
condition = 'WHERE %s' % joined_pairs |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
290 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
291 |
query = self._model.gql(condition, **properties) |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
292 |
|
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
293 |
if unique: |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
294 |
return query.get() |
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
295 |
|
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
|
296 |
return query.fetch(limit, offset) |
433
001b981be45e
Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents:
432
diff
changeset
|
297 |
|
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
|
298 |
def buildTypedQueryString(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
|
299 |
"""Returns a GQL query string compatible with PolyModel. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
300 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
301 |
return ''.join(self._buildTypedQueryParts()) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
302 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
303 |
def _buildTypedQueryParts(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
|
304 |
if self._base_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
|
305 |
return [ |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
306 |
'SELECT * FROM ', self._base_model.__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
|
307 |
" WHERE inheritance_line = '", self.getFullModelClassName(), "'"] |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
308 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
309 |
return ['SELECT * FROM ', self._model.__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
|
310 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
311 |
def buildOrderedQueryString(self, order_by=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
|
312 |
"""Returns a an ordered GQL query string compatible with PolyModel. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
313 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
314 |
Args: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
315 |
order_by: optional field name by which to order the query results; |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
316 |
default is None, in which case no ORDER BY clause is placed in |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
317 |
the query 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
|
318 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
319 |
return ''.join(self._buildOrderedQueryParts(order_by=order_by)) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
320 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
321 |
def _buildOrderedQueryParts(self, order_by=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
|
322 |
query_str_parts = self._buildTypedQueryParts() |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
323 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
324 |
if order_by: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
325 |
query_str_parts.extend([' ORDER BY ', order_by]) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
326 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
327 |
return query_str_parts |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
328 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
329 |
def getEntitiesForLimitAndOffset(self, limit, offset=0, order_by=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
|
330 |
"""Returns entities for given offset and limit or None if not found. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
331 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
332 |
Args: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
333 |
limit: max amount of entities to return |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
334 |
offset: optional offset in entities list which defines first entity to |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
335 |
return; default is zero (first 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
|
336 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
337 |
query_string = self.buildOrderedQueryString(order_by=order_by) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
338 |
query = db.GqlQuery(query_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
|
339 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
340 |
return query.fetch(limit, offset) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
341 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
342 |
def getNearestEntities(self, fields_to_try): |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
343 |
"""Get entities just before and just after the described 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
|
344 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
345 |
Args: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
346 |
fields_to_try: ordered list of key/value pairs that "describe" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
347 |
the desired entity (which may not necessarily exist), where key is |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
348 |
the name of the field, and value is an instance of that field |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
349 |
used in the comparison; if value is None, that field is skipped |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
350 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
351 |
Returns: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
352 |
a two-tuple: ([nearest_entities], 'field_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
|
353 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
354 |
nearest_entities: list of entities being those just before and just |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
355 |
after the (possibly non-existent) entity identified by the first |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
356 |
of the supplied (non-None) fields |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
357 |
OR |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
358 |
possibly None if query had no results for the supplied field |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
359 |
that was used. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
360 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
361 |
# SELECT * FROM base_class WHERE inheritance_line = 'derived_class' |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
362 |
typed_query_parts = self._buildTypedQueryParts() |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
363 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
364 |
if self._base_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
|
365 |
typed_query_parts.append(' AND %s > :1') |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
366 |
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
|
367 |
typed_query_parts.append(' WHERE %s > :1') |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
368 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
369 |
typed_query_fmt = ''.join(typed_query_parts) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
370 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
371 |
for field, value in fields_to_try: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
372 |
if value is 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
|
373 |
# skip this not-supplied field |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
374 |
continue |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
375 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
376 |
query = db.GqlQuery(typed_query_fmt % field, value) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
377 |
return query.fetch(1), field |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
378 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
379 |
# all fields exhausted, and all had None values |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
380 |
return (None, 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
|
381 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
382 |
def findNearestEntitiesOffset(width, fields_to_try): |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
383 |
"""Finds offset of beginning of a range of entities around the nearest. |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
384 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
385 |
Args: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
386 |
width: the width of the "found" window around the nearest User found |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
387 |
fields_to_try: see getNearestEntities() |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
388 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
389 |
Returns: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
390 |
an offset into the list of entities that is width/2 less than the |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
391 |
offset of the first entity returned by getNearestEntities(), or zero |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
392 |
if that offset would be less than zero |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
393 |
OR |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
394 |
None if there are no nearest entities or the offset of the beginning of |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
395 |
the range cannot be found for some reason |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
396 |
""" |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
397 |
# find entity "nearest" to supplied fields |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
398 |
nearest_entities, field = self.getNearestEntities(fields_to_try) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
399 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
400 |
if not nearest_entities: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
401 |
# no "nearest" entity, so indicate that with 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
|
402 |
return 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
|
403 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
404 |
nearest_entity = nearest_entities[0] |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
405 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
406 |
# start search for beginning of nearest Users range at offset zero |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
407 |
offset = 0 |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
408 |
entities = self.getEntitiesForLimitAndOffset(width, offset=offset) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
409 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
410 |
while True: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
411 |
for entity in entities: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
412 |
if getattr(nearest_entity, field) == getattr(entity, field): |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
413 |
# nearest User found in current search range, so return a range start |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
414 |
return max(0, (offset - (width/2))) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
415 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
416 |
offset = offset + 1 |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
417 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
418 |
# nearest User was not in the current search range, so fetch the next set |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
419 |
entities = self.getEntitiesForLimitAndOffset(width, offset=offset) |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
420 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
421 |
if not entities: |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
422 |
# nearest User never found, so indicate that with 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
|
423 |
break |
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
424 |
|
94834a1e6c01
Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents:
459
diff
changeset
|
425 |
return 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
|
426 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
427 |
def updateModelProperties(self, model, model_properties): |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
428 |
"""Update existing model entity using supplied model properties. |
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 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
431 |
model: a model entity |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
432 |
model_properties: keyword arguments that correspond to model entity |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
433 |
properties and their values |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
434 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
435 |
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
|
436 |
The original model entity with any supplied properties changed. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
437 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
438 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
439 |
def update(): |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
440 |
return self._unsafeUpdateModelProperties(model, model_properties) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
441 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
442 |
return db.run_in_transaction(update) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
443 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
444 |
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
|
445 |
"""See updateModelProperties. |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
446 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
447 |
Like updateModelProperties(), but not run within a transaction. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
448 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
449 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
450 |
properties = model.properties() |
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 |
for prop in properties.values(): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
453 |
name = prop.name |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
454 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
455 |
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
|
456 |
value = model_properties[prop.name] |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
457 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
458 |
if self._updateField(model, name, value): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
459 |
prop.__set__(model, value) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
460 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
461 |
model.put() |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
462 |
return model |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
463 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
464 |
def updateOrCreateFromKeyName(self, properties, key_name): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
465 |
"""Update existing entity, or create new one with supplied properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
466 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
467 |
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
|
468 |
properties: dict with entity properties and their values |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
469 |
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
|
470 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
471 |
Returns: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
472 |
the entity corresponding to the key_name, with any supplied |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
473 |
properties changed, or a new entity now associated with the |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
474 |
supplied key_name and properties. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
475 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
476 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
477 |
entity = self.getFromKeyName(key_name) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
478 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
479 |
if not entity: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
480 |
# entity did not exist, so create one in a transaction |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
481 |
entity = self._model.get_or_insert(key_name, **properties) |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
482 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
483 |
# 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
|
484 |
# 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
|
485 |
# in a transaction |
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
486 |
return self.updateModelProperties(entity, properties) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
487 |
|
435
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
488 |
def updateOrCreateFromFields(self, properties, fields): |
829fe8302a8b
Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents:
433
diff
changeset
|
489 |
"""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
|
490 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
491 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
492 |
# 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
|
493 |
key_name = self.getKeyNameForFields(fields) |
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
494 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
495 |
return self.updateOrCreateFromKeyName(properties, key_name) |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
496 |
|
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
497 |
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
|
498 |
"""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
|
499 |
|
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
|
500 |
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
|
501 |
entity: an existing entity in datastore |
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
502 |
""" |
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
|
503 |
|
363
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
504 |
return True |
d35ffa6ca643
Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
354
diff
changeset
|
505 |
|
308
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
506 |
def delete(self, entity): |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
507 |
"""Delete existing entity from datastore. |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
508 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
509 |
Args: |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
510 |
entity: an existing entity in datastore |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
511 |
""" |
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
512 |
|
740090cd17c9
Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
513 |
entity.delete() |