author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Mon, 09 Mar 2009 18:52:48 +0100 | |
changeset 1021 | ce8094084165 |
parent 962 | 1cc3c240b2d5 |
child 985 | 6a25c58a1c23 |
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,) |
962
1cc3c240b2d5
accepts Any by default (not sure it's necessary though, anyway it will be reomved in 3.2
sylvain.thenault@logilab.fr
parents:
543
diff
changeset
|
305 |
accepts = ('Any',) |
0 | 306 |
category = 'entityview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
307 |
|
0 | 308 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True): |
309 |
""" read-only field """ |
|
310 |
if w is None: |
|
311 |
w = self.w |
|
312 |
if row: |
|
313 |
w(u'<div class="row">') |
|
314 |
if show_label: |
|
315 |
if tr: |
|
316 |
label = display_name(self.req, label) |
|
317 |
w(u'<span class="label">%s</span>' % label) |
|
318 |
w(u'<div class="field">%s</div>' % value) |
|
319 |
if row: |
|
320 |
w(u'</div>') |
|
321 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
322 |
|
0 | 323 |
class StartupView(View): |
324 |
"""base class for views which doesn't need a particular result set |
|
325 |
to be displayed (so they can always be displayed !) |
|
326 |
""" |
|
327 |
__registerer__ = priority_registerer |
|
361
5cd8bc047b52
update some selectors
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
355
diff
changeset
|
328 |
__selectors__ = (match_user_group, none_rset) |
0 | 329 |
require_groups = () |
330 |
category = 'startupview' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
331 |
|
0 | 332 |
def url(self): |
333 |
"""return the url associated with this view. We can omit rql here""" |
|
334 |
return self.build_url('view', vid=self.id) |
|
335 |
||
336 |
def html_headers(self): |
|
337 |
"""return a list of html headers (eg something to be inserted between |
|
338 |
<head> and </head> of the returned page |
|
339 |
||
340 |
by default startup views are indexed |
|
341 |
""" |
|
342 |
return [] |
|
343 |
||
344 |
||
345 |
class EntityStartupView(EntityView): |
|
346 |
"""base class for entity views which may also be applied to None |
|
347 |
result set (usually a default rql is provided by the view class) |
|
348 |
""" |
|
349 |
__registerer__ = accepts_registerer |
|
361
5cd8bc047b52
update some selectors
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
355
diff
changeset
|
350 |
__selectors__ = (chainfirst(none_rset, accept),) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
351 |
|
0 | 352 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
353 |
|
0 | 354 |
def __init__(self, req, rset): |
355 |
super(EntityStartupView, self).__init__(req, rset) |
|
356 |
if rset is None: |
|
357 |
# this instance is not in the "entityview" category |
|
358 |
self.category = 'startupview' |
|
359 |
||
360 |
def startup_rql(self): |
|
361 |
"""return some rql to be executedif the result set is None""" |
|
362 |
return self.default_rql |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
363 |
|
0 | 364 |
def call(self, **kwargs): |
365 |
"""override call to execute rql returned by the .startup_rql |
|
366 |
method if necessary |
|
367 |
""" |
|
368 |
if self.rset is None: |
|
369 |
self.rset = self.req.execute(self.startup_rql()) |
|
370 |
rset = self.rset |
|
371 |
for i in xrange(len(rset)): |
|
372 |
self.wview(self.id, rset, row=i, **kwargs) |
|
373 |
||
374 |
def url(self): |
|
375 |
"""return the url associated with this view. We can omit rql if we |
|
376 |
are on a result set on which we do not apply. |
|
377 |
""" |
|
378 |
if not self.__select__(self.req, self.rset): |
|
379 |
return self.build_url(vid=self.id) |
|
380 |
return super(EntityStartupView, self).url() |
|
381 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
382 |
|
0 | 383 |
class AnyRsetView(View): |
384 |
"""base class for views applying on any non empty result sets""" |
|
385 |
__registerer__ = priority_registerer |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
386 |
__selectors__ = (nonempty_rset,) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
387 |
|
0 | 388 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
389 |
|
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
390 |
def columns_labels(self, tr=True): |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
391 |
if tr: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
392 |
translate = display_name |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
393 |
else: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
394 |
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
|
395 |
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
|
396 |
labels = [] |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
397 |
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
|
398 |
# compute column header |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
399 |
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
|
400 |
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
|
401 |
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
|
402 |
else: |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
403 |
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
|
404 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
405 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
406 |
|
0 | 407 |
|
408 |
class EmptyRsetView(View): |
|
409 |
"""base class for views applying on any empty result sets""" |
|
410 |
__registerer__ = priority_registerer |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
411 |
__selectors__ = (empty_rset,) |
0 | 412 |
|
413 |
||
414 |
# concrete template base classes ############################################## |
|
415 |
||
416 |
class Template(View): |
|
417 |
"""a template is almost like a view, except that by default a template |
|
418 |
is only used globally (i.e. no result set adaptation) |
|
419 |
""" |
|
420 |
__registry__ = 'templates' |
|
421 |
__registerer__ = priority_registerer |
|
361
5cd8bc047b52
update some selectors
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
355
diff
changeset
|
422 |
__selectors__ = (match_user_group,) |
0 | 423 |
|
424 |
require_groups = () |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
425 |
|
0 | 426 |
def template(self, oid, **kwargs): |
427 |
"""shortcut to self.registry.render method on the templates registry""" |
|
428 |
w = kwargs.pop('w', self.w) |
|
429 |
self.vreg.render('templates', oid, self.req, w=w, **kwargs) |
|
430 |
||
431 |
||
432 |
class MainTemplate(Template): |
|
433 |
"""main template are primary access point to render a full HTML page. |
|
434 |
There is usually at least a regular main template and a simple fallback |
|
435 |
one to display error if the first one failed |
|
436 |
""" |
|
437 |
||
438 |
base_doctype = STRICT_DOCTYPE |
|
439 |
||
440 |
@property |
|
441 |
def doctype(self): |
|
442 |
if self.req.xhtml_browser(): |
|
443 |
return self.base_doctype % CW_XHTML_EXTENSIONS |
|
444 |
return self.base_doctype % '' |
|
445 |
||
446 |
def set_stream(self, w=None, templatable=True): |
|
447 |
if templatable and self.w is not None: |
|
448 |
return |
|
449 |
||
450 |
if w is None: |
|
451 |
if self.binary: |
|
452 |
self._stream = stream = StringIO() |
|
453 |
elif not templatable: |
|
454 |
# not templatable means we're using a non-html view, we don't |
|
455 |
# want the HTMLStream stuff to interfere during data generation |
|
456 |
self._stream = stream = UStringIO() |
|
457 |
else: |
|
458 |
self._stream = stream = HTMLStream(self.req) |
|
459 |
w = stream.write |
|
460 |
else: |
|
461 |
stream = None |
|
462 |
self.w = w |
|
463 |
return stream |
|
464 |
||
465 |
def write_doctype(self, xmldecl=True): |
|
466 |
assert isinstance(self._stream, HTMLStream) |
|
467 |
self._stream.doctype = self.doctype |
|
468 |
if not xmldecl: |
|
469 |
self._stream.xmldecl = u'' |
|
470 |
||
471 |
# viewable components base classes ############################################ |
|
472 |
||
473 |
class VComponent(ComponentMixIn, View): |
|
474 |
"""base class for displayable components""" |
|
475 |
property_defs = { |
|
476 |
'visible': dict(type='Boolean', default=True, |
|
477 |
help=_('display the component or not')),} |
|
478 |
||
479 |
class SingletonVComponent(VComponent): |
|
480 |
"""base class for displayable unique components""" |
|
481 |
__registerer__ = priority_registerer |