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