author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 18 Feb 2009 17:04:18 +0100 | |
branch | tls-sprint |
changeset 817 | 12320ff8d6e4 |
parent 785 | e4f9fa3c891a |
child 816 | 9cd49a910fce |
permissions | -rw-r--r-- |
0 | 1 |
"""extend the generic VRegistry with some cubicweb specific stuff |
2 |
||
3 |
:organization: Logilab |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from warnings import warn |
|
10 |
||
11 |
from logilab.common.decorators import cached, clear_cache |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
12 |
from logilab.common.interface import extend |
0 | 13 |
|
14 |
from rql import RQLHelper |
|
15 |
||
16 |
from cubicweb import Binary, UnknownProperty |
|
17 |
from cubicweb.vregistry import VRegistry, ObjectNotFound, NoSelectableObject |
|
18 |
||
19 |
_ = unicode |
|
20 |
||
776 | 21 |
def use_interfaces(obj): |
22 |
from cubicweb.selectors import implements |
|
23 |
try: |
|
24 |
# XXX deprecated |
|
25 |
return sorted(obj.accepts_interfaces) |
|
26 |
except AttributeError: |
|
27 |
try: |
|
28 |
impl = obj.__select__.search_selector(implements) |
|
29 |
if impl: |
|
30 |
return sorted(impl.expected_ifaces) |
|
31 |
except AttributeError: |
|
32 |
pass # old-style vobject classes with no accepts_interfaces |
|
33 |
return () |
|
34 |
||
35 |
def expand_parent_classes(iface): |
|
36 |
res = [iface] |
|
37 |
for parent in iface.__bases__: |
|
38 |
res += expand_parent_classes(parent) |
|
39 |
return res |
|
0 | 40 |
|
41 |
||
42 |
class CubicWebRegistry(VRegistry): |
|
43 |
"""extend the generic VRegistry with some cubicweb specific stuff""" |
|
44 |
||
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
45 |
def __init__(self, config, debug=None, initlog=True): |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
46 |
if initlog: |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
47 |
# first init log service |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
48 |
config.init_log(debug=debug) |
0 | 49 |
super(CubicWebRegistry, self).__init__(config) |
50 |
self.schema = None |
|
51 |
self.reset() |
|
52 |
self.initialized = False |
|
53 |
||
54 |
def items(self): |
|
55 |
return [item for item in self._registries.items() |
|
56 |
if not item[0] in ('propertydefs', 'propertyvalues')] |
|
57 |
||
58 |
def values(self): |
|
59 |
return [value for key,value in self._registries.items() |
|
60 |
if not key in ('propertydefs', 'propertyvalues')] |
|
61 |
||
62 |
def reset(self): |
|
63 |
self._registries = {} |
|
64 |
self._lastmodifs = {} |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
65 |
self._needs_iface = {} |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
66 |
# two special registries, propertydefs which care all the property |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
67 |
# definitions, and propertyvals which contains values for those |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
68 |
# properties |
0 | 69 |
self._registries['propertydefs'] = {} |
70 |
self._registries['propertyvalues'] = self.eprop_values = {} |
|
71 |
for key, propdef in self.config.eproperty_definitions(): |
|
72 |
self.register_property(key, **propdef) |
|
73 |
||
74 |
def set_schema(self, schema): |
|
75 |
"""set application'schema and load application objects""" |
|
76 |
self.schema = schema |
|
77 |
clear_cache(self, 'rqlhelper') |
|
78 |
# now we can load application's web objects |
|
79 |
self.register_objects(self.config.vregistry_path()) |
|
80 |
||
81 |
def update_schema(self, schema): |
|
82 |
"""update .schema attribute on registered objects, necessary for some |
|
83 |
tests |
|
84 |
""" |
|
85 |
self.schema = schema |
|
86 |
for registry, regcontent in self._registries.items(): |
|
87 |
if registry in ('propertydefs', 'propertyvalues'): |
|
88 |
continue |
|
89 |
for objects in regcontent.values(): |
|
90 |
for obj in objects: |
|
91 |
obj.schema = schema |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
92 |
|
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
93 |
def register_if_interface_found(self, obj, ifaces, **kwargs): |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
94 |
"""register an object but remove it if no entity class implements one of |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
95 |
the given interfaces |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
96 |
""" |
785 | 97 |
self.register(obj, **kwargs) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
98 |
if not isinstance(ifaces, (tuple, list)): |
785 | 99 |
self._needs_iface[obj] = (ifaces,) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
100 |
else: |
785 | 101 |
self._needs_iface[obj] = ifaces |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
102 |
|
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
103 |
def register(self, obj, **kwargs): |
717 | 104 |
if kwargs.get('registryname', obj.__registry__) == 'etypes': |
105 |
kwargs['clear'] = True |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
106 |
super(CubicWebRegistry, self).register(obj, **kwargs) |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
107 |
# XXX bw compat |
777
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
108 |
ifaces = use_interfaces(obj) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
109 |
if ifaces: |
785 | 110 |
self._needs_iface[obj] = ifaces |
0 | 111 |
|
112 |
def register_objects(self, path, force_reload=None): |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
113 |
"""overriden to remove objects requiring a missing interface""" |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
114 |
if super(CubicWebRegistry, self).register_objects(path, force_reload): |
0 | 115 |
# clear etype cache if you don't want to run into deep weirdness |
116 |
clear_cache(self, 'etype_class') |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
117 |
# we may want to keep interface dependent objects (e.g.for i18n |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
118 |
# catalog generation) |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
119 |
if not self.config.cleanup_interface_sobjects: |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
120 |
return |
0 | 121 |
# remove vobjects that don't support any available interface |
122 |
interfaces = set() |
|
123 |
for classes in self.get('etypes', {}).values(): |
|
124 |
for cls in classes: |
|
777
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
125 |
for iface in cls.__implements__: |
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
126 |
interfaces.update(expand_parent_classes(iface)) |
785 | 127 |
interfaces.update(expand_parent_classes(cls)) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
128 |
for obj, ifaces in self._needs_iface.items(): |
785 | 129 |
ifaces = frozenset(isinstance(iface, basestring) and self.etype_class(iface) or iface |
130 |
for iface in ifaces) |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
131 |
if not ifaces & interfaces: |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
132 |
self.debug('kicking vobject %s (unsupported interface)', obj) |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
133 |
self.unregister(obj) |
777
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
134 |
|
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
135 |
|
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
136 |
|
0 | 137 |
def eid_rset(self, cursor, eid, etype=None): |
138 |
"""return a result set for the given eid without doing actual query |
|
139 |
(we have the eid, we can suppose it exists and user has access to the |
|
140 |
entity) |
|
141 |
""" |
|
142 |
msg = '.eid_rset is deprecated, use req.eid_rset' |
|
143 |
warn(msg, DeprecationWarning, stacklevel=2) |
|
144 |
try: |
|
145 |
return cursor.req.eid_rset(eid, etype) |
|
146 |
except AttributeError: |
|
147 |
# cursor is a session |
|
148 |
return cursor.eid_rset(eid, etype) |
|
149 |
||
150 |
@cached |
|
151 |
def etype_class(self, etype): |
|
152 |
"""return an entity class for the given entity type. |
|
153 |
Try to find out a specific class for this kind of entity or |
|
154 |
default to a dump of the class registered for 'Any' |
|
155 |
""" |
|
156 |
etype = str(etype) |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
157 |
if etype == 'Any': |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
158 |
return self.select(self.registry_objects('etypes', 'Any'), 'Any') |
0 | 159 |
eschema = self.schema.eschema(etype) |
160 |
baseschemas = [eschema] + eschema.ancestors() |
|
161 |
# browse ancestors from most specific to most generic and |
|
162 |
# try to find an associated custom entity class |
|
163 |
for baseschema in baseschemas: |
|
164 |
btype = str(baseschema) |
|
165 |
try: |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
166 |
cls = self.select(self.registry_objects('etypes', btype), etype) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
167 |
break |
0 | 168 |
except ObjectNotFound: |
169 |
pass |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
170 |
else: |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
171 |
# no entity class for any of the ancestors, fallback to the default |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
172 |
# one |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
173 |
cls = self.select(self.registry_objects('etypes', 'Any'), etype) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
174 |
# add class itself to the list of implemented interfaces, as well as the |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
175 |
# Any entity class so we can select according to class using the |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
176 |
# `implements` selector |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
177 |
extend(cls, cls) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
178 |
extend(cls, self.etype_class('Any')) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
179 |
return cls |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
180 |
|
0 | 181 |
def render(self, registry, oid, req, **context): |
182 |
"""select an object in a given registry and render it |
|
183 |
||
184 |
- registry: the registry's name |
|
185 |
- oid : the view to call |
|
186 |
- req : the HTTP request |
|
187 |
""" |
|
188 |
objclss = self.registry_objects(registry, oid) |
|
189 |
try: |
|
190 |
rset = context.pop('rset') |
|
191 |
except KeyError: |
|
192 |
rset = None |
|
193 |
selected = self.select(objclss, req, rset, **context) |
|
194 |
return selected.dispatch(**context) |
|
195 |
||
196 |
def main_template(self, req, oid='main', **context): |
|
197 |
"""display query by calling the given template (default to main), |
|
198 |
and returning the output as a string instead of requiring the [w]rite |
|
199 |
method as argument |
|
200 |
""" |
|
201 |
res = self.render('templates', oid, req, **context) |
|
202 |
if isinstance(res, unicode): |
|
203 |
return res.encode(req.encoding) |
|
204 |
assert isinstance(res, str) |
|
205 |
return res |
|
206 |
||
207 |
def possible_vobjects(self, registry, *args, **kwargs): |
|
208 |
"""return an ordered list of possible app objects in a given registry, |
|
209 |
supposing they support the 'visible' and 'order' properties (as most |
|
210 |
visualizable objects) |
|
211 |
""" |
|
212 |
return [x for x in sorted(self.possible_objects(registry, *args, **kwargs), |
|
213 |
key=lambda x: x.propval('order')) |
|
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
169
diff
changeset
|
214 |
if x.propval('visible')] |
0 | 215 |
|
216 |
def possible_actions(self, req, rset, **kwargs): |
|
217 |
if rset is None: |
|
218 |
actions = self.possible_vobjects('actions', req, rset) |
|
219 |
else: |
|
220 |
actions = rset.possible_actions() # cached implementation |
|
221 |
result = {} |
|
222 |
for action in actions: |
|
223 |
result.setdefault(action.category, []).append(action) |
|
224 |
return result |
|
225 |
||
226 |
def possible_views(self, req, rset, **kwargs): |
|
227 |
"""return an iterator on possible views for this result set |
|
228 |
||
229 |
views returned are classes, not instances |
|
230 |
""" |
|
231 |
for vid, views in self.registry('views').items(): |
|
232 |
if vid[0] == '_': |
|
233 |
continue |
|
234 |
try: |
|
235 |
view = self.select(views, req, rset, **kwargs) |
|
236 |
if view.linkable(): |
|
237 |
yield view |
|
238 |
except NoSelectableObject: |
|
239 |
continue |
|
395
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
240 |
except Exception: |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
241 |
self.exception('error while trying to list possible %s views for %s', |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
242 |
vid, rset) |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
243 |
|
0 | 244 |
def select_box(self, oid, *args, **kwargs): |
245 |
"""return the most specific view according to the result set""" |
|
246 |
try: |
|
247 |
return self.select_object('boxes', oid, *args, **kwargs) |
|
248 |
except NoSelectableObject: |
|
249 |
return |
|
250 |
||
251 |
def select_action(self, oid, *args, **kwargs): |
|
252 |
"""return the most specific view according to the result set""" |
|
253 |
try: |
|
254 |
return self.select_object('actions', oid, *args, **kwargs) |
|
255 |
except NoSelectableObject: |
|
256 |
return |
|
257 |
||
258 |
def select_component(self, cid, *args, **kwargs): |
|
259 |
"""return the most specific component according to the result set""" |
|
260 |
try: |
|
261 |
return self.select_object('components', cid, *args, **kwargs) |
|
262 |
except (NoSelectableObject, ObjectNotFound): |
|
263 |
return |
|
264 |
||
265 |
def select_view(self, __vid, req, rset, **kwargs): |
|
266 |
"""return the most specific view according to the result set""" |
|
267 |
views = self.registry_objects('views', __vid) |
|
268 |
return self.select(views, req, rset, **kwargs) |
|
269 |
||
270 |
||
271 |
# properties handling ##################################################### |
|
272 |
||
273 |
def user_property_keys(self, withsitewide=False): |
|
274 |
if withsitewide: |
|
275 |
return sorted(self['propertydefs']) |
|
276 |
return sorted(k for k, kd in self['propertydefs'].iteritems() |
|
277 |
if not kd['sitewide']) |
|
278 |
||
279 |
def register_property(self, key, type, help, default=None, vocabulary=None, |
|
280 |
sitewide=False): |
|
281 |
"""register a given property""" |
|
282 |
properties = self._registries['propertydefs'] |
|
283 |
assert type in YAMS_TO_PY |
|
284 |
properties[key] = {'type': type, 'vocabulary': vocabulary, |
|
285 |
'default': default, 'help': help, |
|
286 |
'sitewide': sitewide} |
|
287 |
||
288 |
def property_info(self, key): |
|
289 |
"""return dictionary containing description associated to the given |
|
290 |
property key (including type, defaut value, help and a site wide |
|
291 |
boolean) |
|
292 |
""" |
|
293 |
try: |
|
294 |
return self._registries['propertydefs'][key] |
|
295 |
except KeyError: |
|
296 |
if key.startswith('system.version.'): |
|
297 |
soft = key.split('.')[-1] |
|
298 |
return {'type': 'String', 'sitewide': True, |
|
299 |
'default': None, 'vocabulary': None, |
|
300 |
'help': _('%s software version of the database') % soft} |
|
301 |
raise UnknownProperty('unregistered property %r' % key) |
|
302 |
||
303 |
def property_value(self, key): |
|
304 |
try: |
|
305 |
return self._registries['propertyvalues'][key] |
|
306 |
except KeyError: |
|
307 |
return self._registries['propertydefs'][key]['default'] |
|
308 |
||
309 |
def typed_value(self, key, value): |
|
310 |
"""value is an unicode string, return it correctly typed. Let potential |
|
311 |
type error propagates. |
|
312 |
""" |
|
313 |
pdef = self.property_info(key) |
|
314 |
try: |
|
315 |
value = YAMS_TO_PY[pdef['type']](value) |
|
316 |
except (TypeError, ValueError): |
|
317 |
raise ValueError(_('bad value')) |
|
318 |
vocab = pdef['vocabulary'] |
|
319 |
if vocab is not None: |
|
320 |
if callable(vocab): |
|
321 |
vocab = vocab(key, None) # XXX need a req object |
|
322 |
if not value in vocab: |
|
323 |
raise ValueError(_('unauthorized value')) |
|
324 |
return value |
|
325 |
||
326 |
def init_properties(self, propvalues): |
|
327 |
"""init the property values registry using the given set of couple (key, value) |
|
328 |
""" |
|
329 |
self.initialized = True |
|
330 |
values = self._registries['propertyvalues'] |
|
331 |
for key, val in propvalues: |
|
332 |
try: |
|
333 |
values[key] = self.typed_value(key, val) |
|
334 |
except ValueError: |
|
335 |
self.warning('%s (you should probably delete that property ' |
|
336 |
'from the database)', ex) |
|
337 |
except UnknownProperty, ex: |
|
338 |
self.warning('%s (you should probably delete that property ' |
|
339 |
'from the database)', ex) |
|
340 |
||
341 |
||
342 |
def property_value_widget(self, propkey, req=None, **attrs): |
|
343 |
"""return widget according to key's type / vocab""" |
|
344 |
from cubicweb.web.widgets import StaticComboBoxWidget, widget_factory |
|
345 |
if req is None: |
|
346 |
tr = unicode |
|
347 |
else: |
|
348 |
tr = req._ |
|
349 |
try: |
|
350 |
pdef = self.property_info(propkey) |
|
351 |
except UnknownProperty, ex: |
|
352 |
self.warning('%s (you should probably delete that property ' |
|
353 |
'from the database)', ex) |
|
354 |
return widget_factory(self, 'EProperty', self.schema['value'], 'String', |
|
355 |
description=u'', **attrs) |
|
356 |
req.form['value'] = pdef['default'] # XXX hack to pass the default value |
|
357 |
vocab = pdef['vocabulary'] |
|
358 |
if vocab is not None: |
|
359 |
if callable(vocab): |
|
360 |
# list() just in case its a generator function |
|
361 |
vocabfunc = lambda e: list(vocab(propkey, req)) |
|
362 |
else: |
|
363 |
vocabfunc = lambda e: vocab |
|
364 |
w = StaticComboBoxWidget(self, 'EProperty', self.schema['value'], 'String', |
|
365 |
vocabfunc=vocabfunc, description=tr(pdef['help']), |
|
366 |
**attrs) |
|
367 |
else: |
|
368 |
w = widget_factory(self, 'EProperty', self.schema['value'], pdef['type'], |
|
369 |
description=tr(pdef['help']), **attrs) |
|
370 |
return w |
|
371 |
||
372 |
def parse(self, session, rql, args=None): |
|
373 |
rqlst = self.rqlhelper.parse(rql) |
|
374 |
def type_from_eid(eid, session=session): |
|
375 |
return session.describe(eid)[0] |
|
376 |
self.rqlhelper.compute_solutions(rqlst, {'eid': type_from_eid}, args) |
|
377 |
return rqlst |
|
378 |
||
379 |
@property |
|
380 |
@cached |
|
381 |
def rqlhelper(self): |
|
382 |
return RQLHelper(self.schema, |
|
383 |
special_relations={'eid': 'uid', 'has_text': 'fti'}) |
|
384 |
||
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
385 |
|
0 | 386 |
class MulCnxCubicWebRegistry(CubicWebRegistry): |
387 |
"""special registry to be used when an application has to deal with |
|
388 |
connections to differents repository. This class add some additional wrapper |
|
389 |
trying to hide buggy class attributes since classes are not designed to be |
|
390 |
shared. |
|
391 |
""" |
|
392 |
def etype_class(self, etype): |
|
393 |
"""return an entity class for the given entity type. |
|
394 |
Try to find out a specific class for this kind of entity or |
|
395 |
default to a dump of the class registered for 'Any' |
|
396 |
""" |
|
397 |
usercls = super(MulCnxCubicWebRegistry, self).etype_class(etype) |
|
398 |
usercls.e_schema = self.schema.eschema(etype) |
|
399 |
return usercls |
|
400 |
||
401 |
def select(self, vobjects, *args, **kwargs): |
|
402 |
"""return an instance of the most specific object according |
|
403 |
to parameters |
|
404 |
||
405 |
raise NoSelectableObject if not object apply |
|
406 |
""" |
|
407 |
for vobject in vobjects: |
|
408 |
vobject.vreg = self |
|
409 |
vobject.schema = self.schema |
|
410 |
vobject.config = self.config |
|
411 |
return super(MulCnxCubicWebRegistry, self).select(vobjects, *args, **kwargs) |
|
412 |
||
413 |
from mx.DateTime import DateTime, Time, DateTimeDelta |
|
414 |
||
415 |
YAMS_TO_PY = { |
|
416 |
'Boolean': bool, |
|
417 |
'String' : unicode, |
|
418 |
'Password': str, |
|
419 |
'Bytes': Binary, |
|
420 |
'Int': int, |
|
421 |
'Float': float, |
|
422 |
'Date': DateTime, |
|
423 |
'Datetime': DateTime, |
|
424 |
'Time': Time, |
|
425 |
'Interval': DateTimeDelta, |
|
426 |
} |
|
427 |