author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 18:17:32 +0100 | |
branch | tls-sprint |
changeset 683 | 4f4a34346e9b |
parent 661 | 4f61eb8a96b7 |
child 685 | 2538262ffc29 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract views and templates classes for CubicWeb web client |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
472
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from cStringIO import StringIO |
|
11 |
||
12 |
from logilab.mtconverter import html_escape |
|
13 |
||
14 |
from cubicweb import NotAnEntity, NoSelectableObject |
|
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
633
diff
changeset
|
15 |
from cubicweb.selectors import (yes, match_user_groups, implements, |
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
633
diff
changeset
|
16 |
nonempty_rset, none_rset) |
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
633
diff
changeset
|
17 |
from cubicweb.selectors import require_group_compat, accepts_compat |
0 | 18 |
from cubicweb.common.registerers import accepts_registerer, priority_registerer |
655 | 19 |
from cubicweb.common.appobject import AppRsetObject |
0 | 20 |
from cubicweb.common.utils import UStringIO, HTMLStream |
21 |
||
22 |
_ = unicode |
|
23 |
||
633 | 24 |
|
0 | 25 |
# robots control |
26 |
NOINDEX = u'<meta name="ROBOTS" content="NOINDEX" />' |
|
27 |
NOFOLLOW = u'<meta name="ROBOTS" content="NOFOLLOW" />' |
|
28 |
||
29 |
CW_XHTML_EXTENSIONS = '''[ |
|
30 |
<!ATTLIST html xmlns:cubicweb CDATA #FIXED \'http://www.logilab.org/2008/cubicweb\' > |
|
31 |
||
32 |
<!ENTITY % coreattrs |
|
522
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
33 |
"id ID #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
34 |
class CDATA #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
35 |
style CDATA #IMPLIED |
0 | 36 |
title CDATA #IMPLIED |
37 |
||
38 |
cubicweb:sortvalue CDATA #IMPLIED |
|
39 |
cubicweb:target CDATA #IMPLIED |
|
40 |
cubicweb:limit CDATA #IMPLIED |
|
41 |
cubicweb:type CDATA #IMPLIED |
|
42 |
cubicweb:loadtype CDATA #IMPLIED |
|
43 |
cubicweb:wdgtype CDATA #IMPLIED |
|
44 |
cubicweb:initfunc CDATA #IMPLIED |
|
45 |
cubicweb:inputid CDATA #IMPLIED |
|
46 |
cubicweb:tindex CDATA #IMPLIED |
|
47 |
cubicweb:inputname CDATA #IMPLIED |
|
48 |
cubicweb:value CDATA #IMPLIED |
|
49 |
cubicweb:required CDATA #IMPLIED |
|
50 |
cubicweb:accesskey CDATA #IMPLIED |
|
51 |
cubicweb:maxlength CDATA #IMPLIED |
|
52 |
cubicweb:variables CDATA #IMPLIED |
|
53 |
cubicweb:displayactions CDATA #IMPLIED |
|
54 |
cubicweb:fallbackvid CDATA #IMPLIED |
|
55 |
cubicweb:vid CDATA #IMPLIED |
|
56 |
cubicweb:rql CDATA #IMPLIED |
|
57 |
cubicweb:actualrql CDATA #IMPLIED |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
58 |
cubicweb:rooteid CDATA #IMPLIED |
0 | 59 |
cubicweb:dataurl CDATA #IMPLIED |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
60 |
cubicweb:size CDATA #IMPLIED |
0 | 61 |
cubicweb:tlunit CDATA #IMPLIED |
62 |
cubicweb:loadurl CDATA #IMPLIED |
|
63 |
cubicweb:uselabel CDATA #IMPLIED |
|
472 | 64 |
cubicweb:facetargs CDATA #IMPLIED |
65 |
cubicweb:facetName CDATA #IMPLIED |
|
0 | 66 |
"> ] ''' |
67 |
||
68 |
TRANSITIONAL_DOCTYPE = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" %s>\n' |
|
69 |
||
70 |
STRICT_DOCTYPE = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" %s>\n' |
|
71 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
72 |
# base view object ############################################################ |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
73 |
|
0 | 74 |
class View(AppRsetObject): |
75 |
"""abstract view class, used as base for every renderable object such |
|
76 |
as views, templates, some components...web |
|
77 |
||
78 |
A view is instantiated to render a [part of a] result set. View |
|
79 |
subclasses may be parametred using the following class attributes: |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
80 |
|
0 | 81 |
* `templatable` indicates if the view may be embeded in a main |
82 |
template or if it has to be rendered standalone (i.e. XML for |
|
83 |
instance) |
|
84 |
* if the view is not templatable, it should set the `content_type` class |
|
85 |
attribute to the correct MIME type (text/xhtml by default) |
|
86 |
* the `category` attribute may be used in the interface to regroup related |
|
87 |
objects together |
|
88 |
||
89 |
At instantiation time, the standard `req`, `rset`, and `cursor` |
|
90 |
attributes are added and the `w` attribute will be set at rendering |
|
91 |
time to a write function to use. |
|
92 |
""" |
|
632 | 93 |
__registerer__ = priority_registerer |
0 | 94 |
__registry__ = 'views' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
95 |
|
0 | 96 |
templatable = True |
97 |
need_navigation = True |
|
98 |
# content_type = 'application/xhtml+xml' # text/xhtml' |
|
99 |
binary = False |
|
100 |
add_to_breadcrumbs = True |
|
101 |
category = 'view' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
102 |
|
683 | 103 |
def __init__(self, req=None, rset=None): |
0 | 104 |
super(View, self).__init__(req, rset) |
105 |
self.w = None |
|
106 |
||
107 |
@property |
|
108 |
def content_type(self): |
|
109 |
if self.req.xhtml_browser(): |
|
110 |
return 'application/xhtml+xml' |
|
111 |
return 'text/html' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
112 |
|
0 | 113 |
def set_stream(self, w=None): |
114 |
if self.w is not None: |
|
115 |
return |
|
116 |
if w is None: |
|
117 |
if self.binary: |
|
118 |
self._stream = stream = StringIO() |
|
119 |
else: |
|
120 |
self._stream = stream = UStringIO() |
|
121 |
w = stream.write |
|
122 |
else: |
|
123 |
stream = None |
|
124 |
self.w = w |
|
125 |
return stream |
|
126 |
||
127 |
# main view interface ##################################################### |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
128 |
|
0 | 129 |
def dispatch(self, w=None, **context): |
130 |
"""called to render a view object for a result set. |
|
131 |
||
132 |
This method is a dispatched to an actual method selected |
|
133 |
according to optional row and col parameters, which are locating |
|
134 |
a particular row or cell in the result set: |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
135 |
|
0 | 136 |
* if row [and col] are specified, `cell_call` is called |
137 |
* if none of them is supplied, the view is considered to apply on |
|
138 |
the whole result set (which may be None in this case), `call` is |
|
139 |
called |
|
140 |
""" |
|
141 |
row, col = context.get('row'), context.get('col') |
|
142 |
if row is not None: |
|
143 |
context.setdefault('col', 0) |
|
144 |
view_func = self.cell_call |
|
145 |
else: |
|
146 |
view_func = self.call |
|
147 |
stream = self.set_stream(w) |
|
148 |
# stream = self.set_stream(context) |
|
149 |
view_func(**context) |
|
150 |
# return stream content if we have created it |
|
151 |
if stream is not None: |
|
152 |
return self._stream.getvalue() |
|
153 |
||
154 |
# should default .call() method add a <div classs="section"> around each |
|
155 |
# rset item |
|
156 |
add_div_section = True |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
157 |
|
0 | 158 |
def call(self, **kwargs): |
159 |
"""the view is called for an entire result set, by default loop |
|
160 |
other rows of the result set and call the same view on the |
|
161 |
particular row |
|
162 |
||
163 |
Views applicable on None result sets have to override this method |
|
164 |
""" |
|
165 |
rset = self.rset |
|
166 |
if rset is None: |
|
167 |
raise NotImplementedError, self |
|
168 |
wrap = self.templatable and len(rset) > 1 and self.add_div_section |
|
169 |
for i in xrange(len(rset)): |
|
170 |
if wrap: |
|
171 |
self.w(u'<div class="section">') |
|
172 |
self.wview(self.id, rset, row=i, **kwargs) |
|
173 |
if wrap: |
|
174 |
self.w(u"</div>") |
|
175 |
||
176 |
def cell_call(self, row, col, **kwargs): |
|
177 |
"""the view is called for a particular result set cell""" |
|
178 |
raise NotImplementedError, self |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
179 |
|
0 | 180 |
def linkable(self): |
181 |
"""return True if the view may be linked in a menu |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
182 |
|
0 | 183 |
by default views without title are not meant to be displayed |
184 |
""" |
|
185 |
if not getattr(self, 'title', None): |
|
186 |
return False |
|
187 |
return True |
|
188 |
||
189 |
def is_primary(self): |
|
190 |
return self.id == 'primary' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
191 |
|
0 | 192 |
def url(self): |
193 |
"""return the url associated with this view. Should not be |
|
194 |
necessary for non linkable views, but a default implementation |
|
195 |
is provided anyway. |
|
196 |
""" |
|
197 |
try: |
|
198 |
return self.build_url(vid=self.id, rql=self.req.form['rql']) |
|
199 |
except KeyError: |
|
200 |
return self.build_url(vid=self.id) |
|
201 |
||
202 |
def set_request_content_type(self): |
|
203 |
"""set the content type returned by this view""" |
|
204 |
self.req.set_content_type(self.content_type) |
|
205 |
||
206 |
# view utilities ########################################################## |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
207 |
|
0 | 208 |
def view(self, __vid, rset, __fallback_vid=None, **kwargs): |
209 |
"""shortcut to self.vreg.render method avoiding to pass self.req""" |
|
210 |
try: |
|
211 |
view = self.vreg.select_view(__vid, self.req, rset, **kwargs) |
|
212 |
except NoSelectableObject: |
|
213 |
if __fallback_vid is None: |
|
214 |
raise |
|
215 |
view = self.vreg.select_view(__fallback_vid, self.req, rset, **kwargs) |
|
355
89ad20af9e4c
oops, these were committed unintentionnally
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
354
diff
changeset
|
216 |
return view.dispatch(**kwargs) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
217 |
|
0 | 218 |
def wview(self, __vid, rset, __fallback_vid=None, **kwargs): |
219 |
"""shortcut to self.view method automatically passing self.w as argument |
|
220 |
""" |
|
221 |
self.view(__vid, rset, __fallback_vid, w=self.w, **kwargs) |
|
222 |
||
223 |
def whead(self, data): |
|
224 |
self.req.html_headers.write(data) |
|
225 |
||
226 |
def wdata(self, data): |
|
227 |
"""simple helper that escapes `data` and writes into `self.w`""" |
|
228 |
self.w(html_escape(data)) |
|
229 |
||
230 |
def action(self, actionid, row=0): |
|
231 |
"""shortcut to get action object with id `actionid`""" |
|
232 |
return self.vreg.select_action(actionid, self.req, self.rset, |
|
233 |
row=row) |
|
234 |
||
235 |
def action_url(self, actionid, label=None, row=0): |
|
236 |
"""simple method to be able to display `actionid` as a link anywhere |
|
237 |
""" |
|
238 |
action = self.vreg.select_action(actionid, self.req, self.rset, |
|
239 |
row=row) |
|
240 |
if action: |
|
241 |
label = label or self.req._(action.title) |
|
242 |
return u'<a href="%s">%s</a>' % (html_escape(action.url()), label) |
|
243 |
return u'' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
244 |
|
0 | 245 |
def html_headers(self): |
246 |
"""return a list of html headers (eg something to be inserted between |
|
247 |
<head> and </head> of the returned page |
|
248 |
||
249 |
by default return a meta tag to disable robot indexation of the page |
|
250 |
""" |
|
251 |
return [NOINDEX] |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
252 |
|
0 | 253 |
def page_title(self): |
254 |
"""returns a title according to the result set - used for the |
|
255 |
title in the HTML header |
|
256 |
""" |
|
257 |
vtitle = self.req.form.get('vtitle') |
|
258 |
if vtitle: |
|
259 |
return self.req._(vtitle) |
|
260 |
# class defined title will only be used if the resulting title doesn't |
|
261 |
# seem clear enough |
|
262 |
vtitle = getattr(self, 'title', None) or u'' |
|
263 |
if vtitle: |
|
264 |
vtitle = self.req._(vtitle) |
|
265 |
rset = self.rset |
|
266 |
if rset and rset.rowcount: |
|
267 |
if rset.rowcount == 1: |
|
268 |
try: |
|
269 |
entity = self.complete_entity(0) |
|
270 |
# use long_title to get context information if any |
|
271 |
clabel = entity.dc_long_title() |
|
272 |
except NotAnEntity: |
|
273 |
clabel = display_name(self.req, rset.description[0][0]) |
|
274 |
clabel = u'%s (%s)' % (clabel, vtitle) |
|
275 |
else : |
|
276 |
etypes = rset.column_types(0) |
|
277 |
if len(etypes) == 1: |
|
278 |
etype = iter(etypes).next() |
|
279 |
clabel = display_name(self.req, etype, 'plural') |
|
280 |
else : |
|
281 |
clabel = u'#[*] (%s)' % vtitle |
|
282 |
else: |
|
283 |
clabel = vtitle |
|
284 |
return u'%s (%s)' % (clabel, self.req.property_value('ui.site-title')) |
|
285 |
||
286 |
def output_url_builder( self, name, url, args ): |
|
287 |
self.w(u'<script language="JavaScript"><!--\n' \ |
|
288 |
u'function %s( %s ) {\n' % (name, ','.join(args) ) ) |
|
289 |
url_parts = url.split("%s") |
|
290 |
self.w(u' url="%s"' % url_parts[0] ) |
|
291 |
for arg, part in zip(args, url_parts[1:]): |
|
292 |
self.w(u'+str(%s)' % arg ) |
|
293 |
if part: |
|
294 |
self.w(u'+"%s"' % part) |
|
295 |
self.w('\n document.window.href=url;\n') |
|
296 |
self.w('}\n-->\n</script>\n') |
|
297 |
||
298 |
def create_url(self, etype, **kwargs): |
|
299 |
""" return the url of the entity creation form for a given entity type""" |
|
300 |
return self.req.build_url('add/%s'%etype, **kwargs) |
|
633 | 301 |
|
0 | 302 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True): |
303 |
""" read-only field """ |
|
304 |
if w is None: |
|
305 |
w = self.w |
|
306 |
if row: |
|
307 |
w(u'<div class="row">') |
|
308 |
if show_label: |
|
309 |
if tr: |
|
310 |
label = display_name(self.req, label) |
|
311 |
w(u'<span class="label">%s</span>' % label) |
|
312 |
w(u'<div class="field">%s</div>' % value) |
|
313 |
if row: |
|
314 |
w(u'</div>') |
|
315 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
316 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
317 |
# concrete views base classes ################################################# |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
318 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
319 |
class EntityView(View): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
320 |
"""base class for views applying on an entity (i.e. uniform result set) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
321 |
""" |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
322 |
# XXX deprecate |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
323 |
__registerer__ = accepts_registerer |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
324 |
__selectors__ = (implements('Any'),) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
325 |
registered = accepts_compat(View.registered.im_func) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
326 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
327 |
category = 'entityview' |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
328 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
329 |
|
0 | 330 |
class StartupView(View): |
331 |
"""base class for views which doesn't need a particular result set |
|
332 |
to be displayed (so they can always be displayed !) |
|
333 |
""" |
|
334 |
__registerer__ = priority_registerer |
|
633 | 335 |
__selectors__ = (none_rset,) |
336 |
registered = require_group_compat(View.registered.im_func) |
|
337 |
||
0 | 338 |
category = 'startupview' |
633 | 339 |
|
0 | 340 |
def url(self): |
341 |
"""return the url associated with this view. We can omit rql here""" |
|
342 |
return self.build_url('view', vid=self.id) |
|
343 |
||
344 |
def html_headers(self): |
|
345 |
"""return a list of html headers (eg something to be inserted between |
|
346 |
<head> and </head> of the returned page |
|
347 |
||
348 |
by default startup views are indexed |
|
349 |
""" |
|
350 |
return [] |
|
351 |
||
352 |
||
353 |
class EntityStartupView(EntityView): |
|
354 |
"""base class for entity views which may also be applied to None |
|
355 |
result set (usually a default rql is provided by the view class) |
|
356 |
""" |
|
357 |
__registerer__ = accepts_registerer |
|
633 | 358 |
__selectors__ = ((none_rset | implements('Any')),) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
359 |
|
0 | 360 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
361 |
|
0 | 362 |
def __init__(self, req, rset): |
363 |
super(EntityStartupView, self).__init__(req, rset) |
|
364 |
if rset is None: |
|
365 |
# this instance is not in the "entityview" category |
|
366 |
self.category = 'startupview' |
|
367 |
||
368 |
def startup_rql(self): |
|
369 |
"""return some rql to be executedif the result set is None""" |
|
370 |
return self.default_rql |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
371 |
|
0 | 372 |
def call(self, **kwargs): |
373 |
"""override call to execute rql returned by the .startup_rql |
|
374 |
method if necessary |
|
375 |
""" |
|
376 |
if self.rset is None: |
|
377 |
self.rset = self.req.execute(self.startup_rql()) |
|
378 |
rset = self.rset |
|
379 |
for i in xrange(len(rset)): |
|
380 |
self.wview(self.id, rset, row=i, **kwargs) |
|
381 |
||
382 |
def url(self): |
|
383 |
"""return the url associated with this view. We can omit rql if we |
|
384 |
are on a result set on which we do not apply. |
|
385 |
""" |
|
386 |
if not self.__select__(self.req, self.rset): |
|
387 |
return self.build_url(vid=self.id) |
|
388 |
return super(EntityStartupView, self).url() |
|
389 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
390 |
|
0 | 391 |
class AnyRsetView(View): |
392 |
"""base class for views applying on any non empty result sets""" |
|
393 |
__registerer__ = priority_registerer |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
394 |
__selectors__ = (nonempty_rset,) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
395 |
|
0 | 396 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
397 |
|
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
398 |
def columns_labels(self, tr=True): |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
399 |
if tr: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
400 |
translate = display_name |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
401 |
else: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
402 |
translate = lambda req, val: val |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
403 |
rqlstdescr = self.rset.syntax_tree().get_description()[0] # XXX missing Union support |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
404 |
labels = [] |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
405 |
for colindex, attr in enumerate(rqlstdescr): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
406 |
# compute column header |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
407 |
if colindex == 0 or attr == 'Any': # find a better label |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
408 |
label = ','.join(translate(self.req, et) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
409 |
for et in self.rset.column_types(colindex)) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
410 |
else: |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
411 |
label = translate(self.req, attr) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
412 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
413 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
414 |
|
633 | 415 |
|
0 | 416 |
# concrete template base classes ############################################## |
417 |
||
418 |
class Template(View): |
|
419 |
"""a template is almost like a view, except that by default a template |
|
420 |
is only used globally (i.e. no result set adaptation) |
|
421 |
""" |
|
422 |
__registry__ = 'templates' |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
655
diff
changeset
|
423 |
# __registerer__ = priority_registerer |
633 | 424 |
__selectors__ = (yes,) |
0 | 425 |
|
633 | 426 |
registered = require_group_compat(View.registered.im_func) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
427 |
|
0 | 428 |
def template(self, oid, **kwargs): |
429 |
"""shortcut to self.registry.render method on the templates registry""" |
|
430 |
w = kwargs.pop('w', self.w) |
|
431 |
self.vreg.render('templates', oid, self.req, w=w, **kwargs) |
|
432 |
||
433 |
||
434 |
class MainTemplate(Template): |
|
435 |
"""main template are primary access point to render a full HTML page. |
|
436 |
There is usually at least a regular main template and a simple fallback |
|
437 |
one to display error if the first one failed |
|
438 |
""" |
|
439 |
base_doctype = STRICT_DOCTYPE |
|
440 |
||
441 |
@property |
|
442 |
def doctype(self): |
|
443 |
if self.req.xhtml_browser(): |
|
444 |
return self.base_doctype % CW_XHTML_EXTENSIONS |
|
445 |
return self.base_doctype % '' |
|
446 |
||
447 |
def set_stream(self, w=None, templatable=True): |
|
448 |
if templatable and self.w is not None: |
|
449 |
return |
|
450 |
||
451 |
if w is None: |
|
452 |
if self.binary: |
|
453 |
self._stream = stream = StringIO() |
|
454 |
elif not templatable: |
|
455 |
# not templatable means we're using a non-html view, we don't |
|
456 |
# want the HTMLStream stuff to interfere during data generation |
|
457 |
self._stream = stream = UStringIO() |
|
458 |
else: |
|
459 |
self._stream = stream = HTMLStream(self.req) |
|
460 |
w = stream.write |
|
461 |
else: |
|
462 |
stream = None |
|
463 |
self.w = w |
|
464 |
return stream |
|
465 |
||
466 |
def write_doctype(self, xmldecl=True): |
|
467 |
assert isinstance(self._stream, HTMLStream) |
|
468 |
self._stream.doctype = self.doctype |
|
469 |
if not xmldecl: |
|
470 |
self._stream.xmldecl = u'' |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
471 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
472 |
# concrete component base classes ############################################# |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
473 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
474 |
class ReloadableMixIn(object): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
475 |
"""simple mixin for reloadable parts of UI""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
476 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
477 |
def user_callback(self, cb, args, msg=None, nonify=False): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
478 |
"""register the given user callback and return an url to call it ready to be |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
479 |
inserted in html |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
480 |
""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
481 |
self.req.add_js('cubicweb.ajax.js') |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
482 |
if nonify: |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
483 |
_cb = cb |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
484 |
def cb(*args): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
485 |
_cb(*args) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
486 |
cbname = self.req.register_onetime_callback(cb, *args) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
487 |
return self.build_js(cbname, html_escape(msg or '')) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
488 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
489 |
def build_update_js_call(self, cbname, msg): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
490 |
rql = html_escape(self.rset.printable_rql()) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
491 |
return "javascript:userCallbackThenUpdateUI('%s', '%s', '%s', '%s', '%s', '%s')" % ( |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
492 |
cbname, self.id, rql, msg, self.__registry__, self.div_id()) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
493 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
494 |
def build_reload_js_call(self, cbname, msg): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
495 |
return "javascript:userCallbackThenReloadPage('%s', '%s')" % (cbname, msg) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
496 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
497 |
build_js = build_update_js_call # expect updatable component by default |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
498 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
499 |
def div_id(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
500 |
return '' |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
501 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
502 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
503 |
class Component(ReloadableMixIn, View): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
504 |
"""base class for components""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
505 |
__registry__ = 'components' |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
506 |
__registerer__ = yes_registerer |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
507 |
__selectors__ = (yes,) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
508 |
property_defs = { |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
509 |
_('visible'): dict(type='Boolean', default=True, |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
510 |
help=_('display the box or not')), |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
511 |
} |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
512 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
513 |
def div_class(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
514 |
return '%s %s' % (self.propval('htmlclass'), self.id) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
515 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
516 |
def div_id(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
517 |
return '%sComponent' % self.id |