author | Florent <florent@secondweb.fr> |
Fri, 17 Apr 2009 08:51:16 +0200 | |
branch | tls-sprint |
changeset 1386 | 8ba1f0d36c7e |
parent 1282 | 272d8ec6f308 |
child 1398 | 5fe84a5f7035 |
permissions | -rw-r--r-- |
0 | 1 |
"""Base class for dynamically loaded objects manipulated in the web interface |
2 |
||
3 |
:organization: Logilab |
|
447 | 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 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
9 |
from datetime import datetime, timedelta |
0 | 10 |
|
11 |
from simplejson import dumps |
|
12 |
||
699 | 13 |
from logilab.common.decorators import classproperty |
0 | 14 |
from logilab.common.deprecation import obsolete |
447 | 15 |
|
16 |
from rql.nodes import VariableRef, SubQuery |
|
0 | 17 |
from rql.stmts import Union, Select |
18 |
||
1174 | 19 |
from cubicweb import Unauthorized, NoSelectableObject |
726
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
20 |
from cubicweb.vregistry import VObject, AndSelector |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
683
diff
changeset
|
21 |
from cubicweb.selectors import yes |
709 | 22 |
from cubicweb.utils import UStringIO, ustrftime |
0 | 23 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
24 |
ONESECOND = timedelta(0, 1, 0) |
0 | 25 |
|
26 |
class Cache(dict): |
|
27 |
def __init__(self): |
|
28 |
super(Cache, self).__init__() |
|
29 |
self.cache_creation_date = None |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
30 |
self.latest_cache_lookup = datetime.now() |
0 | 31 |
|
32 |
CACHE_REGISTRY = {} |
|
33 |
||
34 |
class AppRsetObject(VObject): |
|
35 |
"""This is the base class for CubicWeb application objects |
|
36 |
which are selected according to a request and result set. |
|
37 |
|
|
38 |
Classes are kept in the vregistry and instantiation is done at selection |
|
39 |
time. |
|
40 |
|
|
41 |
At registration time, the following attributes are set on the class: |
|
42 |
:vreg: |
|
43 |
the application's registry |
|
44 |
:schema: |
|
45 |
the application's schema |
|
46 |
:config: |
|
47 |
the application's configuration |
|
48 |
||
49 |
At instantiation time, the following attributes are set on the instance: |
|
50 |
:req: |
|
51 |
current request |
|
52 |
:rset: |
|
53 |
result set on which the object is applied |
|
54 |
""" |
|
805
6e99feeba28b
set a default selector on base app objects class
sylvain.thenault@logilab.fr
parents:
802
diff
changeset
|
55 |
__select__ = yes() |
0 | 56 |
|
57 |
@classmethod |
|
58 |
def registered(cls, vreg): |
|
721 | 59 |
super(AppRsetObject, cls).registered(vreg) |
0 | 60 |
cls.vreg = vreg |
61 |
cls.schema = vreg.schema |
|
62 |
cls.config = vreg.config |
|
63 |
cls.register_properties() |
|
64 |
return cls |
|
1282
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
65 |
|
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
66 |
@classmethod |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
67 |
def vreg_initialization_completed(cls): |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
68 |
pass |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1174
diff
changeset
|
69 |
|
0 | 70 |
@classmethod |
683 | 71 |
def selected(cls, *args, **kwargs): |
0 | 72 |
"""by default web app objects are usually instantiated on |
73 |
selection according to a request, a result set, and optional |
|
74 |
row and col |
|
75 |
""" |
|
875 | 76 |
assert len(args) <= 2 |
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
77 |
return cls(*args, **kwargs) |
0 | 78 |
|
79 |
# Eproperties definition: |
|
80 |
# key: id of the property (the actual EProperty key is build using |
|
81 |
# <registry name>.<obj id>.<property id> |
|
82 |
# value: tuple (property type, vocabfunc, default value, property description) |
|
83 |
# possible types are those used by `logilab.common.configuration` |
|
84 |
# |
|
85 |
# notice that when it exists multiple objects with the same id (adaptation, |
|
86 |
# overriding) only the first encountered definition is considered, so those |
|
87 |
# objects can't try to have different default values for instance. |
|
88 |
||
89 |
property_defs = {} |
|
90 |
||
91 |
@classmethod |
|
92 |
def register_properties(cls): |
|
93 |
for propid, pdef in cls.property_defs.items(): |
|
94 |
pdef = pdef.copy() # may be shared |
|
95 |
pdef['default'] = getattr(cls, propid, pdef['default']) |
|
96 |
pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide')) |
|
97 |
cls.vreg.register_property(cls.propkey(propid), **pdef) |
|
98 |
||
99 |
@classmethod |
|
100 |
def propkey(cls, propid): |
|
101 |
return '%s.%s.%s' % (cls.__registry__, cls.id, propid) |
|
699 | 102 |
|
103 |
@classproperty |
|
104 |
@obsolete('use __select__ and & or | operators') |
|
105 |
def __selectors__(cls): |
|
726
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
106 |
selector = cls.__select__ |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
107 |
if isinstance(selector, AndSelector): |
732
45c3414ac002
__selectors__ must return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
726
diff
changeset
|
108 |
return tuple(selector.selectors) |
726
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
109 |
if not isinstance(selector, tuple): |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
110 |
selector = (selector,) |
88a74f590986
improve __selectors__() so that it always return a tuple
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
721
diff
changeset
|
111 |
return selector |
699 | 112 |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
113 |
def __init__(self, req=None, rset=None, row=None, col=None, **extra): |
0 | 114 |
super(AppRsetObject, self).__init__() |
115 |
self.req = req |
|
116 |
self.rset = rset |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
117 |
self.row = row |
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
118 |
self.col = col |
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1130
diff
changeset
|
119 |
self.extra_kwargs = extra |
0 | 120 |
|
121 |
def get_cache(self, cachename): |
|
122 |
""" |
|
123 |
NOTE: cachename should be dotted names as in : |
|
124 |
- cubicweb.mycache |
|
125 |
- cubes.blog.mycache |
|
126 |
- etc. |
|
127 |
""" |
|
128 |
if cachename in CACHE_REGISTRY: |
|
129 |
cache = CACHE_REGISTRY[cachename] |
|
130 |
else: |
|
131 |
cache = Cache() |
|
132 |
CACHE_REGISTRY[cachename] = cache |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
133 |
_now = datetime.now() |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
134 |
if _now > cache.latest_cache_lookup + ONESECOND: |
0 | 135 |
ecache = self.req.execute('Any C,T WHERE C is ECache, C name %(name)s, C timestamp T', |
136 |
{'name':cachename}).get_entity(0,0) |
|
137 |
cache.latest_cache_lookup = _now |
|
138 |
if not ecache.valid(cache.cache_creation_date): |
|
1130
17ff4d4bfbd0
should call clear, not empth (thanks pylint)
sylvain.thenault@logilab.fr
parents:
1129
diff
changeset
|
139 |
cache.clear() |
0 | 140 |
cache.cache_creation_date = _now |
141 |
return cache |
|
142 |
||
143 |
def propval(self, propid): |
|
144 |
assert self.req |
|
145 |
return self.req.property_value(self.propkey(propid)) |
|
146 |
||
147 |
def limited_rql(self): |
|
148 |
"""return a printable rql for the result set associated to the object, |
|
149 |
with limit/offset correctly set according to maximum page size and |
|
150 |
currently displayed page when necessary |
|
151 |
""" |
|
152 |
# try to get page boundaries from the navigation component |
|
153 |
# XXX we should probably not have a ref to this component here (eg in |
|
154 |
# cubicweb.common) |
|
155 |
nav = self.vreg.select_component('navigation', self.req, self.rset) |
|
156 |
if nav: |
|
157 |
start, stop = nav.page_boundaries() |
|
158 |
rql = self._limit_offset_rql(stop - start, start) |
|
159 |
# result set may have be limited manually in which case navigation won't |
|
160 |
# apply |
|
161 |
elif self.rset.limited: |
|
162 |
rql = self._limit_offset_rql(*self.rset.limited) |
|
163 |
# navigation component doesn't apply and rset has not been limited, no |
|
164 |
# need to limit query |
|
165 |
else: |
|
166 |
rql = self.rset.printable_rql() |
|
167 |
return rql |
|
168 |
||
169 |
def _limit_offset_rql(self, limit, offset): |
|
170 |
rqlst = self.rset.syntax_tree() |
|
171 |
if len(rqlst.children) == 1: |
|
172 |
select = rqlst.children[0] |
|
173 |
olimit, ooffset = select.limit, select.offset |
|
174 |
select.limit, select.offset = limit, offset |
|
175 |
rql = rqlst.as_string(kwargs=self.rset.args) |
|
176 |
# restore original limit/offset |
|
177 |
select.limit, select.offset = olimit, ooffset |
|
178 |
else: |
|
179 |
newselect = Select() |
|
180 |
newselect.limit = limit |
|
181 |
newselect.offset = offset |
|
182 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
183 |
for i, vref in enumerate(rqlst.selection)] |
|
184 |
newselect.set_with([SubQuery(aliases, rqlst)], check=False) |
|
185 |
newunion = Union() |
|
186 |
newunion.append(newselect) |
|
187 |
rql = rqlst.as_string(kwargs=self.rset.args) |
|
188 |
rqlst.parent = None |
|
189 |
return rql |
|
1144
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
190 |
|
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
191 |
def view(self, __vid, rset=None, __fallback_vid=None, **kwargs): |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
192 |
"""shortcut to self.vreg.render method avoiding to pass self.req""" |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
193 |
try: |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
194 |
view = self.vreg.select_view(__vid, self.req, rset, **kwargs) |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
195 |
except NoSelectableObject: |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
196 |
if __fallback_vid is None: |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
197 |
raise |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
198 |
view = self.vreg.select_view(__fallback_vid, self.req, rset, **kwargs) |
654047cd0c30
move .view method on AppRsetObject, move initialize_varmaker method on View
sylvain.thenault@logilab.fr
parents:
1143
diff
changeset
|
199 |
return view.dispatch(**kwargs) |
0 | 200 |
|
201 |
# url generation methods ################################################## |
|
202 |
||
203 |
controller = 'view' |
|
204 |
||
205 |
def build_url(self, method=None, **kwargs): |
|
206 |
"""return an absolute URL using params dictionary key/values as URL |
|
207 |
parameters. Values are automatically URL quoted, and the |
|
208 |
publishing method to use may be specified or will be guessed. |
|
209 |
""" |
|
210 |
# XXX I (adim) think that if method is passed explicitly, we should |
|
211 |
# not try to process it and directly call req.build_url() |
|
212 |
if method is None: |
|
213 |
method = self.controller |
|
214 |
if method == 'view' and self.req.from_controller() == 'view' and \ |
|
215 |
not '_restpath' in kwargs: |
|
216 |
method = self.req.relative_path(includeparams=False) or 'view' |
|
217 |
return self.req.build_url(method, **kwargs) |
|
218 |
||
219 |
# various resources accessors ############################################# |
|
220 |
||
221 |
def entity(self, row, col=0): |
|
222 |
"""short cut to get an entity instance for a particular row/column |
|
223 |
(col default to 0) |
|
224 |
""" |
|
225 |
return self.rset.get_entity(row, col) |
|
226 |
||
227 |
def complete_entity(self, row, col=0, skip_bytes=True): |
|
228 |
"""short cut to get an completed entity instance for a particular |
|
229 |
row (all instance's attributes have been fetched) |
|
230 |
""" |
|
231 |
entity = self.entity(row, col) |
|
232 |
entity.complete(skip_bytes=skip_bytes) |
|
233 |
return entity |
|
234 |
||
235 |
def user_rql_callback(self, args, msg=None): |
|
236 |
"""register a user callback to execute some rql query and return an url |
|
237 |
to call it ready to be inserted in html |
|
238 |
""" |
|
239 |
def rqlexec(req, rql, args=None, key=None): |
|
240 |
req.execute(rql, args, key) |
|
241 |
return self.user_callback(rqlexec, args, msg) |
|
242 |
||
243 |
def user_callback(self, cb, args, msg=None, nonify=False): |
|
244 |
"""register the given user callback and return an url to call it ready to be |
|
245 |
inserted in html |
|
246 |
""" |
|
247 |
self.req.add_js('cubicweb.ajax.js') |
|
248 |
cbname = self.req.register_onetime_callback(cb, *args) |
|
249 |
msg = dumps(msg or '') |
|
250 |
return "javascript:userCallbackThenReloadPage('%s', %s)" % ( |
|
251 |
cbname, msg) |
|
252 |
||
253 |
# formating methods ####################################################### |
|
254 |
||
255 |
def tal_render(self, template, variables): |
|
256 |
"""render a precompiled page template with variables in the given |
|
257 |
dictionary as context |
|
258 |
""" |
|
259 |
from cubicweb.common.tal import CubicWebContext |
|
260 |
context = CubicWebContext() |
|
261 |
context.update({'self': self, 'rset': self.rset, '_' : self.req._, |
|
262 |
'req': self.req, 'user': self.req.user}) |
|
263 |
context.update(variables) |
|
264 |
output = UStringIO() |
|
265 |
template.expand(context, output) |
|
266 |
return output.getvalue() |
|
267 |
||
268 |
def format_date(self, date, date_format=None, time=False): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
269 |
"""return a string for a date time according to application's |
0 | 270 |
configuration |
271 |
""" |
|
272 |
if date: |
|
273 |
if date_format is None: |
|
274 |
if time: |
|
275 |
date_format = self.req.property_value('ui.datetime-format') |
|
276 |
else: |
|
277 |
date_format = self.req.property_value('ui.date-format') |
|
278 |
return ustrftime(date, date_format) |
|
279 |
return u'' |
|
280 |
||
281 |
def format_time(self, time): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
875
diff
changeset
|
282 |
"""return a string for a time according to application's |
0 | 283 |
configuration |
284 |
""" |
|
285 |
if time: |
|
286 |
return ustrftime(time, self.req.property_value('ui.time-format')) |
|
287 |
return u'' |
|
288 |
||
289 |
def format_float(self, num): |
|
290 |
"""return a string for floating point number according to application's |
|
291 |
configuration |
|
292 |
""" |
|
293 |
if num: |
|
294 |
return self.req.property_value('ui.float-format') % num |
|
295 |
return u'' |
|
296 |
||
297 |
# security related methods ################################################ |
|
298 |
||
299 |
def ensure_ro_rql(self, rql): |
|
300 |
"""raise an exception if the given rql is not a select query""" |
|
301 |
first = rql.split(' ', 1)[0].lower() |
|
302 |
if first in ('insert', 'set', 'delete'): |
|
303 |
raise Unauthorized(self.req._('only select queries are authorized')) |
|
304 |
||
305 |
||
306 |
class AppObject(AppRsetObject): |
|
307 |
"""base class for application objects which are not selected |
|
308 |
according to a result set, only by their identifier. |
|
309 |
|
|
310 |
Those objects may not have req, rset and cursor set. |
|
311 |
""" |
|
312 |
||
313 |
@classmethod |
|
314 |
def selected(cls, *args, **kwargs): |
|
315 |
"""by default web app objects are usually instantiated on |
|
316 |
selection |
|
317 |
""" |
|
318 |
return cls(*args, **kwargs) |
|
319 |
||
320 |
def __init__(self, req=None, rset=None, **kwargs): |
|
321 |
self.req = req |
|
322 |
self.rset = rset |
|
323 |
self.__dict__.update(kwargs) |