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