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