author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Feb 2010 12:16:52 +0100 | |
changeset 4476 | 14429e8c7590 |
parent 4267 | 3ea88c8e7d7f |
child 4853 | 5dd4a9a46266 |
permissions | -rw-r--r-- |
0 | 1 |
"""provide replacement classes for gae db module, so that a gae model can be |
2 |
used as base for a cubicweb application by simply replacing :: |
|
3 |
||
4 |
from google.appengine.ext import db |
|
5 |
||
6 |
by |
|
7 |
||
8 |
from cubicweb.goa import db |
|
9 |
||
10 |
The db.model api should be fully featured by replacement classes, with the |
|
11 |
following differences: |
|
12 |
||
13 |
* all methods returning `google.appengine.ext.db.Model` instance(s) will return |
|
14 |
`cubicweb.goa.db.Model` instance instead (though you should see almost no |
|
15 |
difference since those instances have the same api) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
16 |
|
0 | 17 |
* class methods returning model instance take a `req` as first argument, unless |
18 |
they are called through an instance, representing the current request |
|
19 |
(accessible through `self.req` on almost all objects) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
20 |
|
0 | 21 |
* XXX no instance.<modelname>_set attributes, use instance.reverse_<attr name> |
22 |
instead |
|
23 |
* XXX reference property always return a list of objects, not the instance |
|
24 |
* XXX name/collection_name argument of properties constructor are ignored |
|
25 |
* XXX ListProperty |
|
26 |
||
27 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
28 |
:copyright: 2008-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 29 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
30 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 31 |
""" |
32 |
__docformat__ = "restructuredtext en" |
|
33 |
||
34 |
from copy import deepcopy |
|
35 |
||
36 |
from logilab.common.decorators import cached, iclassmethod |
|
37 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
38 |
from cubicweb import Binary, entities |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
from cubicweb.req import RequestSessionBase |
0 | 40 |
from cubicweb.rset import ResultSet |
713
5adb6d8e5fa7
update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
41 |
from cubicweb.entity import metaentity |
0 | 42 |
from cubicweb.server.utils import crypt_password |
1131
544609e83317
pylint cleanup, no more need for mx datetime conversion
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
43 |
from cubicweb.goa import MODE |
0 | 44 |
from cubicweb.goa.dbinit import init_relations |
45 |
||
46 |
from google.appengine.api.datastore import Get, Put, Key, Entity, Query |
|
47 |
from google.appengine.api.datastore import NormalizeAndTypeCheck, RunInTransaction |
|
48 |
from google.appengine.api.datastore_types import Text, Blob |
|
49 |
from google.appengine.api.datastore_errors import BadKeyError |
|
50 |
||
51 |
# XXX remove this dependancy |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
52 |
from google.appengine.ext import db |
0 | 53 |
|
54 |
||
55 |
def rset_from_objs(req, objs, attrs=('eid',), rql=None, args=None): |
|
56 |
"""return a ResultSet instance for list of objects""" |
|
57 |
if objs is None: |
|
58 |
objs = () |
|
59 |
elif isinstance(objs, Entity): |
|
60 |
objs = (objs,) |
|
61 |
if rql is None: |
|
62 |
rql = 'Any X' |
|
63 |
rows = [] |
|
64 |
description = [] |
|
65 |
rset = ResultSet(rows, rql, args, description=description) |
|
66 |
vreg = req.vreg |
|
67 |
for i, obj in enumerate(objs): |
|
68 |
line = [] |
|
69 |
linedescr = [] |
|
70 |
eschema = vreg.schema.eschema(obj.kind()) |
|
71 |
for j, attr in enumerate(attrs): |
|
72 |
if attr == 'eid': |
|
73 |
value = obj.key() |
|
74 |
obj.row, obj.col = i, j |
|
75 |
descr = eschema.type |
|
76 |
value = str(value) |
|
77 |
else: |
|
78 |
value = obj[attr] |
|
79 |
descr = str(eschema.destination(attr)) |
|
80 |
line.append(value) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
81 |
linedescr.append(descr) |
0 | 82 |
rows.append(line) |
83 |
description.append(linedescr) |
|
84 |
for j, attr in enumerate(attrs): |
|
85 |
if attr == 'eid': |
|
86 |
entity = vreg.etype_class(eschema.type)(req, rset, i, j) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
87 |
rset._get_entity_cache_ = {(i, j): entity} |
0 | 88 |
rset.rowcount = len(rows) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
89 |
req.decorate_rset(rset) |
0 | 90 |
return rset |
91 |
||
92 |
||
93 |
def needrequest(wrapped): |
|
94 |
def wrapper(cls, *args, **kwargs): |
|
95 |
req = kwargs.pop('req', None) |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
96 |
if req is None and args and isinstance(args[0], RequestSessionBase): |
0 | 97 |
args = list(args) |
98 |
req = args.pop(0) |
|
99 |
if req is None: |
|
100 |
req = getattr(cls, 'req', None) |
|
101 |
if req is None: |
|
102 |
raise Exception('either call this method on an instance or ' |
|
103 |
'specify the req argument') |
|
104 |
return wrapped(cls, req, *args, **kwargs) |
|
105 |
return iclassmethod(wrapper) |
|
106 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
107 |
|
0 | 108 |
class gaedbmetaentity(metaentity): |
109 |
"""metaclass for goa.db.Model classes: filter entity / db model part, |
|
110 |
put aside the db model part for later creation of db model class. |
|
111 |
""" |
|
112 |
def __new__(mcs, name, bases, classdict): |
|
113 |
if not 'id' in classdict: |
|
114 |
classdict['id'] = name |
|
115 |
entitycls = super(gaedbmetaentity, mcs).__new__(mcs, name, bases, classdict) |
|
116 |
return entitycls |
|
117 |
||
118 |
||
119 |
TEST_MODELS = {} |
|
120 |
||
121 |
def extract_dbmodel(entitycls): |
|
122 |
if MODE == 'test' and entitycls in TEST_MODELS: |
|
123 |
dbclassdict = TEST_MODELS[entitycls] |
|
124 |
else: |
|
125 |
dbclassdict = {} |
|
126 |
for attr, value in entitycls.__dict__.items(): |
|
127 |
if isinstance(value, db.Property) or isinstance(value, ReferencePropertyStub): |
|
128 |
dbclassdict[attr] = value |
|
129 |
# don't remove attr from entitycls, this make tests fail, and it's anyway |
|
130 |
# overwritten by descriptor at class initialization time |
|
131 |
#delattr(entitycls, attr) |
|
132 |
if MODE == 'test': |
|
133 |
TEST_MODELS[entitycls] = dbclassdict |
|
134 |
dbclassdict = deepcopy(dbclassdict) |
|
135 |
for propname, prop in TEST_MODELS[entitycls].iteritems(): |
|
136 |
if getattr(prop, 'reference_class', None) is db._SELF_REFERENCE: |
|
137 |
dbclassdict[propname].reference_class = db._SELF_REFERENCE |
|
138 |
return dbclassdict |
|
139 |
||
140 |
||
141 |
class Model(entities.AnyEntity): |
|
142 |
id = 'Any' |
|
143 |
__metaclass__ = gaedbmetaentity |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
144 |
|
0 | 145 |
row = col = 0 |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
146 |
|
0 | 147 |
@classmethod |
148 |
def __initialize__(cls): |
|
149 |
super(Model, cls).__initialize__() |
|
150 |
cls._attributes = frozenset(rschema for rschema in cls.e_schema.subject_relations() |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
151 |
if rschema.final) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
152 |
|
0 | 153 |
def __init__(self, *args, **kwargs): |
154 |
# db.Model prototype: |
|
155 |
# __init__(self, parent=None, key_name=None, **kw) |
|
156 |
# |
|
157 |
# Entity prototype: |
|
158 |
# __init__(self, req, rset, row=None, col=0) |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
159 |
if args and isinstance(args[0], RequestSessionBase) or 'req' in kwargs: |
0 | 160 |
super(Model, self).__init__(*args, **kwargs) |
161 |
self._gaeinitargs = None |
|
162 |
else: |
|
163 |
super(Model, self).__init__(None, None) |
|
164 |
# if Model instances are given in kwargs, turn them into db model |
|
165 |
for key, val in kwargs.iteritems(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
166 |
if key in self.e_schema.subject_relations() and not self.e_schema.schema[key].final: |
0 | 167 |
if isinstance(kwargs, (list, tuple)): |
168 |
val = [isinstance(x, Model) and x._dbmodel or x for x in val] |
|
169 |
elif isinstance(val, Model): |
|
170 |
val = val._dbmodel |
|
171 |
kwargs[key] = val.key() |
|
172 |
self._gaeinitargs = (args, kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
173 |
|
0 | 174 |
def __repr__(self): |
175 |
return '<ModelEntity %s %s %s at %s>' % ( |
|
176 |
self.e_schema, self.eid, self.keys(), id(self)) |
|
177 |
||
178 |
def _cubicweb_to_datastore(self, attr, value): |
|
179 |
attr = attr[2:] # remove 's_' / 'o_' prefix |
|
180 |
if attr in self._attributes: |
|
181 |
tschema = self.e_schema.destination(attr) |
|
1131
544609e83317
pylint cleanup, no more need for mx datetime conversion
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
182 |
if tschema == 'String': |
0 | 183 |
if len(value) > 500: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
184 |
value = Text(value) |
0 | 185 |
elif tschema == 'Password': |
186 |
# if value is a Binary instance, this mean we got it |
|
187 |
# from a query result and so it is already encrypted |
|
188 |
if isinstance(value, Binary): |
|
189 |
value = value.getvalue() |
|
190 |
else: |
|
191 |
value = crypt_password(value) |
|
192 |
elif tschema == 'Bytes': |
|
193 |
if isinstance(value, Binary): |
|
194 |
value = value.getvalue() |
|
195 |
value = Blob(value) |
|
196 |
else: |
|
197 |
value = Key(value) |
|
198 |
return value |
|
199 |
||
200 |
def _to_gae_dict(self, convert=True): |
|
201 |
gaedict = {} |
|
202 |
for attr, value in self.iteritems(): |
|
203 |
attr = 's_' + attr |
|
204 |
if value is not None and convert: |
|
205 |
value = self._cubicweb_to_datastore(attr, value) |
|
206 |
gaedict[attr] = value |
|
207 |
return gaedict |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
208 |
|
0 | 209 |
def to_gae_model(self): |
210 |
dbmodel = self._dbmodel |
|
211 |
dbmodel.update(self._to_gae_dict()) |
|
212 |
return dbmodel |
|
213 |
||
214 |
@property |
|
215 |
@cached |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
216 |
def _dbmodel(self): |
0 | 217 |
if self.has_eid(): |
218 |
assert self._gaeinitargs is None |
|
219 |
try: |
|
220 |
return self.req.datastore_get(self.eid) |
|
221 |
except AttributeError: # self.req is not a server session |
|
222 |
return Get(self.eid) |
|
223 |
self.set_defaults() |
|
224 |
values = self._to_gae_dict(convert=False) |
|
225 |
parent = key_name = _app = None |
|
226 |
if self._gaeinitargs is not None: |
|
227 |
args, kwargs = self._gaeinitargs |
|
228 |
args = list(args) |
|
229 |
if args: |
|
230 |
parent = args.pop(0) |
|
231 |
if args: |
|
232 |
key_name = args.pop(0) |
|
233 |
if args: |
|
234 |
_app = args.pop(0) |
|
235 |
assert not args |
|
236 |
if 'parent' in kwargs: |
|
237 |
assert parent is None |
|
238 |
parent = kwargs.pop('parent') |
|
239 |
if 'key_name' in kwargs: |
|
240 |
assert key_name is None |
|
241 |
key_name = kwargs.pop('key_name') |
|
242 |
if '_app' in kwargs: |
|
243 |
assert _app is None |
|
244 |
_app = kwargs.pop('_app') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
245 |
|
0 | 246 |
for key, value in kwargs.iteritems(): |
247 |
if key in self._attributes: |
|
248 |
values['s_'+key] = value |
|
249 |
else: |
|
250 |
kwargs = None |
|
251 |
if key_name is None: |
|
252 |
key_name = self.db_key_name() |
|
253 |
if key_name is not None: |
|
254 |
key_name = 'key_' + key_name |
|
255 |
for key, value in values.iteritems(): |
|
256 |
if value is None: |
|
257 |
continue |
|
258 |
values[key] = self._cubicweb_to_datastore(key, value) |
|
259 |
entity = Entity(self.id, parent, _app, key_name) |
|
260 |
entity.update(values) |
|
261 |
init_relations(entity, self.e_schema) |
|
262 |
return entity |
|
263 |
||
264 |
def db_key_name(self): |
|
265 |
"""override this method to control datastore key name that should be |
|
266 |
used at entity creation. |
|
267 |
||
268 |
Note that if this function return something else than None, the returned |
|
269 |
value will be prefixed by 'key_' to build the actual key name. |
|
270 |
""" |
|
271 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
272 |
|
0 | 273 |
def metainformation(self): |
274 |
return {'type': self.id, 'source': {'uri': 'system'}, 'extid': None} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
275 |
|
0 | 276 |
def view(self, vid, __registry='views', **kwargs): |
277 |
"""shortcut to apply a view on this entity""" |
|
4267
3ea88c8e7d7f
fix syntax error
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
4252
diff
changeset
|
278 |
return self.vreg[__registry].render(vid, self.req, rset=self.rset, |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
279 |
row=self.row, col=self.col, **kwargs) |
0 | 280 |
|
281 |
@classmethod |
|
282 |
def _rest_attr_info(cls): |
|
283 |
mainattr, needcheck = super(Model, cls)._rest_attr_info() |
|
284 |
if needcheck: |
|
285 |
return 'eid', False |
|
286 |
return mainattr, needcheck |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
287 |
|
0 | 288 |
def get_value(self, name): |
289 |
try: |
|
290 |
value = self[name] |
|
291 |
except KeyError: |
|
292 |
if not self.has_eid(): |
|
293 |
return None |
|
294 |
value = self._dbmodel.get('s_'+name) |
|
295 |
if value is not None: |
|
296 |
if isinstance(value, Text): |
|
297 |
value = unicode(value) |
|
298 |
elif isinstance(value, Blob): |
|
299 |
value = Binary(str(value)) |
|
300 |
self[name] = value |
|
301 |
return value |
|
302 |
||
303 |
def has_eid(self): |
|
304 |
if self.eid is None: |
|
305 |
return False |
|
306 |
try: |
|
307 |
Key(self.eid) |
|
308 |
return True |
|
309 |
except BadKeyError: |
|
310 |
return False |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
311 |
|
0 | 312 |
def complete(self, skip_bytes=True): |
313 |
pass |
|
314 |
||
315 |
def unrelated(self, rtype, targettype, role='subject', limit=None, |
|
316 |
ordermethod=None): |
|
317 |
# XXX dumb implementation |
|
318 |
if limit is not None: |
|
319 |
objs = Query(str(targettype)).Get(limit) |
|
320 |
else: |
|
321 |
objs = Query(str(targettype)).Run() |
|
322 |
return rset_from_objs(self.req, objs, ('eid',), |
|
323 |
'Any X WHERE X is %s' % targettype) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
324 |
|
0 | 325 |
def key(self): |
326 |
return Key(self.eid) |
|
327 |
||
328 |
def put(self, req=None): |
|
329 |
if req is not None and self.req is None: |
|
330 |
self.req = req |
|
331 |
dbmodel = self.to_gae_model() |
|
332 |
key = Put(dbmodel) |
|
333 |
self.set_eid(str(key)) |
|
334 |
if self.req is not None and self.rset is None: |
|
335 |
self.rset = rset_from_objs(self.req, dbmodel, ('eid',), |
|
336 |
'Any X WHERE X eid %(x)s', {'x': self.eid}) |
|
337 |
self.row = self.col = 0 |
|
338 |
return dbmodel |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
339 |
|
0 | 340 |
@needrequest |
341 |
def get(cls, req, keys): |
|
342 |
# if check if this is a dict.key call |
|
343 |
if isinstance(cls, Model) and keys in cls._attributes: |
|
344 |
return super(Model, cls).get(keys) |
|
345 |
rset = rset_from_objs(req, Get(keys), ('eid',), |
|
346 |
'Any X WHERE X eid IN %(x)s', {'x': keys}) |
|
347 |
return list(rset.entities()) |
|
348 |
||
349 |
@needrequest |
|
350 |
def get_by_id(cls, req, ids, parent=None): |
|
351 |
if isinstance(parent, Model): |
|
352 |
parent = parent.key() |
|
353 |
ids, multiple = NormalizeAndTypeCheck(ids, (int, long)) |
|
354 |
keys = [Key.from_path(cls.kind(), id, parent=parent) |
|
355 |
for id in ids] |
|
356 |
rset = rset_from_objs(req, Get(keys)) |
|
357 |
return list(rset.entities()) |
|
358 |
||
359 |
@classmethod |
|
360 |
def get_by_key_name(cls, req, key_names, parent=None): |
|
361 |
if isinstance(parent, Model): |
|
362 |
parent = parent.key() |
|
363 |
key_names, multiple = NormalizeAndTypeCheck(key_names, basestring) |
|
364 |
keys = [Key.from_path(cls.kind(), name, parent=parent) |
|
365 |
for name in key_names] |
|
366 |
rset = rset_from_objs(req, Get(keys)) |
|
367 |
return list(rset.entities()) |
|
368 |
||
369 |
@classmethod |
|
370 |
def get_or_insert(cls, req, key_name, **kwds): |
|
371 |
def txn(): |
|
372 |
entity = cls.get_by_key_name(key_name, parent=kwds.get('parent')) |
|
373 |
if entity is None: |
|
374 |
entity = cls(key_name=key_name, **kwds) |
|
375 |
entity.put() |
|
376 |
return entity |
|
377 |
return RunInTransaction(txn) |
|
378 |
||
379 |
@classmethod |
|
380 |
def all(cls, req): |
|
381 |
rset = rset_from_objs(req, Query(cls.id).Run()) |
|
382 |
return list(rset.entities()) |
|
383 |
||
384 |
@classmethod |
|
385 |
def gql(cls, req, query_string, *args, **kwds): |
|
386 |
raise NotImplementedError('use rql') |
|
387 |
||
388 |
@classmethod |
|
389 |
def kind(cls): |
|
447 | 390 |
return cls.id |
0 | 391 |
|
392 |
@classmethod |
|
393 |
def properties(cls): |
|
394 |
raise NotImplementedError('use eschema') |
|
395 |
||
396 |
def dynamic_properties(self): |
|
397 |
raise NotImplementedError('use eschema') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
398 |
|
0 | 399 |
def is_saved(self): |
400 |
return self.has_eid() |
|
401 |
||
402 |
def parent(self): |
|
403 |
parent = self._dbmodel.parent() |
|
404 |
if not parent is None: |
|
405 |
rset = rset_from_objs(self.req, (parent,), ('eid',), |
|
406 |
'Any X WHERE X eid %(x)s', {'x': parent.key()}) |
|
407 |
parent = rset.get_entity(0, 0) |
|
408 |
return parent |
|
409 |
||
410 |
def parent_key(self): |
|
411 |
return self.parent().key() |
|
412 |
||
413 |
def to_xml(self): |
|
414 |
return self._dbmodel.ToXml() |
|
415 |
||
416 |
# hijack AnyEntity class |
|
417 |
entities.AnyEntity = Model |
|
418 |
||
419 |
BooleanProperty = db.BooleanProperty |
|
420 |
URLProperty = db.URLProperty |
|
421 |
DateProperty = db.DateProperty |
|
422 |
DateTimeProperty = db.DateTimeProperty |
|
423 |
TimeProperty = db.TimeProperty |
|
424 |
StringProperty = db.StringProperty |
|
425 |
TextProperty = db.TextProperty |
|
426 |
BlobProperty = db.BlobProperty |
|
427 |
IntegerProperty = db.IntegerProperty |
|
428 |
FloatProperty = db.FloatProperty |
|
429 |
ListProperty = db.ListProperty |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
430 |
SelfReferenceProperty = db.SelfReferenceProperty |
0 | 431 |
UserProperty = db.UserProperty |
432 |
||
433 |
||
434 |
class ReferencePropertyStub(object): |
|
435 |
def __init__(self, cls, args, kwargs): |
|
436 |
self.cls = cls |
|
437 |
self.args = args |
|
438 |
self.kwargs = kwargs |
|
439 |
self.required = False |
|
440 |
self.__dict__.update(kwargs) |
|
441 |
self.creation_counter = db.Property.creation_counter |
|
442 |
db.Property.creation_counter += 1 |
|
443 |
||
444 |
@property |
|
445 |
def data_type(self): |
|
446 |
class FakeDataType(object): |
|
447 |
@staticmethod |
|
448 |
def kind(): |
|
449 |
return self.cls.__name__ |
|
450 |
return FakeDataType |
|
451 |
||
452 |
def ReferenceProperty(cls, *args, **kwargs): |
|
453 |
if issubclass(cls, db.Model): |
|
454 |
cls = db.class_for_kind(cls.__name__) |
|
455 |
return db.ReferenceProperty(cls, *args, **kwargs) |
|
456 |
return ReferencePropertyStub(cls, args, kwargs) |