author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 17 Feb 2009 14:28:14 +0100 | |
branch | tls-sprint |
changeset 660 | 5233a9457f6b |
parent 655 | ca3c4992c7d1 |
child 661 | 4f61eb8a96b7 |
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 |
||
72 |
class View(AppRsetObject): |
|
73 |
"""abstract view class, used as base for every renderable object such |
|
74 |
as views, templates, some components...web |
|
75 |
||
76 |
A view is instantiated to render a [part of a] result set. View |
|
77 |
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
|
78 |
|
0 | 79 |
* `templatable` indicates if the view may be embeded in a main |
80 |
template or if it has to be rendered standalone (i.e. XML for |
|
81 |
instance) |
|
82 |
* if the view is not templatable, it should set the `content_type` class |
|
83 |
attribute to the correct MIME type (text/xhtml by default) |
|
84 |
* the `category` attribute may be used in the interface to regroup related |
|
85 |
objects together |
|
86 |
||
87 |
At instantiation time, the standard `req`, `rset`, and `cursor` |
|
88 |
attributes are added and the `w` attribute will be set at rendering |
|
89 |
time to a write function to use. |
|
90 |
""" |
|
632 | 91 |
__registerer__ = priority_registerer |
0 | 92 |
__registry__ = 'views' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
93 |
|
0 | 94 |
templatable = True |
95 |
need_navigation = True |
|
96 |
# content_type = 'application/xhtml+xml' # text/xhtml' |
|
97 |
binary = False |
|
98 |
add_to_breadcrumbs = True |
|
99 |
category = 'view' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
100 |
|
0 | 101 |
def __init__(self, req, rset): |
102 |
super(View, self).__init__(req, rset) |
|
103 |
self.w = None |
|
104 |
||
105 |
@property |
|
106 |
def content_type(self): |
|
107 |
if self.req.xhtml_browser(): |
|
108 |
return 'application/xhtml+xml' |
|
109 |
return 'text/html' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
110 |
|
0 | 111 |
def set_stream(self, w=None): |
112 |
if self.w is not None: |
|
113 |
return |
|
114 |
if w is None: |
|
115 |
if self.binary: |
|
116 |
self._stream = stream = StringIO() |
|
117 |
else: |
|
118 |
self._stream = stream = UStringIO() |
|
119 |
w = stream.write |
|
120 |
else: |
|
121 |
stream = None |
|
122 |
self.w = w |
|
123 |
return stream |
|
124 |
||
125 |
# main view interface ##################################################### |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
126 |
|
0 | 127 |
def dispatch(self, w=None, **context): |
128 |
"""called to render a view object for a result set. |
|
129 |
||
130 |
This method is a dispatched to an actual method selected |
|
131 |
according to optional row and col parameters, which are locating |
|
132 |
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
|
133 |
|
0 | 134 |
* if row [and col] are specified, `cell_call` is called |
135 |
* if none of them is supplied, the view is considered to apply on |
|
136 |
the whole result set (which may be None in this case), `call` is |
|
137 |
called |
|
138 |
""" |
|
139 |
row, col = context.get('row'), context.get('col') |
|
140 |
if row is not None: |
|
141 |
context.setdefault('col', 0) |
|
142 |
view_func = self.cell_call |
|
143 |
else: |
|
144 |
view_func = self.call |
|
145 |
stream = self.set_stream(w) |
|
146 |
# stream = self.set_stream(context) |
|
147 |
view_func(**context) |
|
148 |
# return stream content if we have created it |
|
149 |
if stream is not None: |
|
150 |
return self._stream.getvalue() |
|
151 |
||
152 |
# should default .call() method add a <div classs="section"> around each |
|
153 |
# rset item |
|
154 |
add_div_section = True |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
155 |
|
0 | 156 |
def call(self, **kwargs): |
157 |
"""the view is called for an entire result set, by default loop |
|
158 |
other rows of the result set and call the same view on the |
|
159 |
particular row |
|
160 |
||
161 |
Views applicable on None result sets have to override this method |
|
162 |
""" |
|
163 |
rset = self.rset |
|
164 |
if rset is None: |
|
165 |
raise NotImplementedError, self |
|
166 |
wrap = self.templatable and len(rset) > 1 and self.add_div_section |
|
167 |
for i in xrange(len(rset)): |
|
168 |
if wrap: |
|
169 |
self.w(u'<div class="section">') |
|
170 |
self.wview(self.id, rset, row=i, **kwargs) |
|
171 |
if wrap: |
|
172 |
self.w(u"</div>") |
|
173 |
||
174 |
def cell_call(self, row, col, **kwargs): |
|
175 |
"""the view is called for a particular result set cell""" |
|
176 |
raise NotImplementedError, self |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
177 |
|
0 | 178 |
def linkable(self): |
179 |
"""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
|
180 |
|
0 | 181 |
by default views without title are not meant to be displayed |
182 |
""" |
|
183 |
if not getattr(self, 'title', None): |
|
184 |
return False |
|
185 |
return True |
|
186 |
||
187 |
def is_primary(self): |
|
188 |
return self.id == 'primary' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
189 |
|
0 | 190 |
def url(self): |
191 |
"""return the url associated with this view. Should not be |
|
192 |
necessary for non linkable views, but a default implementation |
|
193 |
is provided anyway. |
|
194 |
""" |
|
195 |
try: |
|
196 |
return self.build_url(vid=self.id, rql=self.req.form['rql']) |
|
197 |
except KeyError: |
|
198 |
return self.build_url(vid=self.id) |
|
199 |
||
200 |
def set_request_content_type(self): |
|
201 |
"""set the content type returned by this view""" |
|
202 |
self.req.set_content_type(self.content_type) |
|
203 |
||
204 |
# view utilities ########################################################## |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
205 |
|
0 | 206 |
def view(self, __vid, rset, __fallback_vid=None, **kwargs): |
207 |
"""shortcut to self.vreg.render method avoiding to pass self.req""" |
|
208 |
try: |
|
209 |
view = self.vreg.select_view(__vid, self.req, rset, **kwargs) |
|
210 |
except NoSelectableObject: |
|
211 |
if __fallback_vid is None: |
|
212 |
raise |
|
213 |
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
|
214 |
return view.dispatch(**kwargs) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
215 |
|
0 | 216 |
def wview(self, __vid, rset, __fallback_vid=None, **kwargs): |
217 |
"""shortcut to self.view method automatically passing self.w as argument |
|
218 |
""" |
|
219 |
self.view(__vid, rset, __fallback_vid, w=self.w, **kwargs) |
|
220 |
||
221 |
def whead(self, data): |
|
222 |
self.req.html_headers.write(data) |
|
223 |
||
224 |
def wdata(self, data): |
|
225 |
"""simple helper that escapes `data` and writes into `self.w`""" |
|
226 |
self.w(html_escape(data)) |
|
227 |
||
228 |
def action(self, actionid, row=0): |
|
229 |
"""shortcut to get action object with id `actionid`""" |
|
230 |
return self.vreg.select_action(actionid, self.req, self.rset, |
|
231 |
row=row) |
|
232 |
||
233 |
def action_url(self, actionid, label=None, row=0): |
|
234 |
"""simple method to be able to display `actionid` as a link anywhere |
|
235 |
""" |
|
236 |
action = self.vreg.select_action(actionid, self.req, self.rset, |
|
237 |
row=row) |
|
238 |
if action: |
|
239 |
label = label or self.req._(action.title) |
|
240 |
return u'<a href="%s">%s</a>' % (html_escape(action.url()), label) |
|
241 |
return u'' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
242 |
|
0 | 243 |
def html_headers(self): |
244 |
"""return a list of html headers (eg something to be inserted between |
|
245 |
<head> and </head> of the returned page |
|
246 |
||
247 |
by default return a meta tag to disable robot indexation of the page |
|
248 |
""" |
|
249 |
return [NOINDEX] |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
250 |
|
0 | 251 |
def page_title(self): |
252 |
"""returns a title according to the result set - used for the |
|
253 |
title in the HTML header |
|
254 |
""" |
|
255 |
vtitle = self.req.form.get('vtitle') |
|
256 |
if vtitle: |
|
257 |
return self.req._(vtitle) |
|
258 |
# class defined title will only be used if the resulting title doesn't |
|
259 |
# seem clear enough |
|
260 |
vtitle = getattr(self, 'title', None) or u'' |
|
261 |
if vtitle: |
|
262 |
vtitle = self.req._(vtitle) |
|
263 |
rset = self.rset |
|
264 |
if rset and rset.rowcount: |
|
265 |
if rset.rowcount == 1: |
|
266 |
try: |
|
267 |
entity = self.complete_entity(0) |
|
268 |
# use long_title to get context information if any |
|
269 |
clabel = entity.dc_long_title() |
|
270 |
except NotAnEntity: |
|
271 |
clabel = display_name(self.req, rset.description[0][0]) |
|
272 |
clabel = u'%s (%s)' % (clabel, vtitle) |
|
273 |
else : |
|
274 |
etypes = rset.column_types(0) |
|
275 |
if len(etypes) == 1: |
|
276 |
etype = iter(etypes).next() |
|
277 |
clabel = display_name(self.req, etype, 'plural') |
|
278 |
else : |
|
279 |
clabel = u'#[*] (%s)' % vtitle |
|
280 |
else: |
|
281 |
clabel = vtitle |
|
282 |
return u'%s (%s)' % (clabel, self.req.property_value('ui.site-title')) |
|
283 |
||
284 |
def output_url_builder( self, name, url, args ): |
|
285 |
self.w(u'<script language="JavaScript"><!--\n' \ |
|
286 |
u'function %s( %s ) {\n' % (name, ','.join(args) ) ) |
|
287 |
url_parts = url.split("%s") |
|
288 |
self.w(u' url="%s"' % url_parts[0] ) |
|
289 |
for arg, part in zip(args, url_parts[1:]): |
|
290 |
self.w(u'+str(%s)' % arg ) |
|
291 |
if part: |
|
292 |
self.w(u'+"%s"' % part) |
|
293 |
self.w('\n document.window.href=url;\n') |
|
294 |
self.w('}\n-->\n</script>\n') |
|
295 |
||
296 |
def create_url(self, etype, **kwargs): |
|
297 |
""" return the url of the entity creation form for a given entity type""" |
|
298 |
return self.req.build_url('add/%s'%etype, **kwargs) |
|
633 | 299 |
|
0 | 300 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True): |
301 |
""" read-only field """ |
|
302 |
if w is None: |
|
303 |
w = self.w |
|
304 |
if row: |
|
305 |
w(u'<div class="row">') |
|
306 |
if show_label: |
|
307 |
if tr: |
|
308 |
label = display_name(self.req, label) |
|
309 |
w(u'<span class="label">%s</span>' % label) |
|
310 |
w(u'<div class="field">%s</div>' % value) |
|
311 |
if row: |
|
312 |
w(u'</div>') |
|
313 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
314 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
315 |
# concrete views base classes ################################################# |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
316 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
317 |
class EntityView(View): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
318 |
"""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
|
319 |
""" |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
320 |
# XXX deprecate |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
321 |
__registerer__ = accepts_registerer |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
322 |
__selectors__ = (implements('Any'),) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
323 |
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
|
324 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
325 |
category = 'entityview' |
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 |
|
0 | 328 |
class StartupView(View): |
329 |
"""base class for views which doesn't need a particular result set |
|
330 |
to be displayed (so they can always be displayed !) |
|
331 |
""" |
|
332 |
__registerer__ = priority_registerer |
|
633 | 333 |
__selectors__ = (none_rset,) |
334 |
registered = require_group_compat(View.registered.im_func) |
|
335 |
||
0 | 336 |
category = 'startupview' |
633 | 337 |
|
0 | 338 |
def url(self): |
339 |
"""return the url associated with this view. We can omit rql here""" |
|
340 |
return self.build_url('view', vid=self.id) |
|
341 |
||
342 |
def html_headers(self): |
|
343 |
"""return a list of html headers (eg something to be inserted between |
|
344 |
<head> and </head> of the returned page |
|
345 |
||
346 |
by default startup views are indexed |
|
347 |
""" |
|
348 |
return [] |
|
349 |
||
350 |
||
351 |
class EntityStartupView(EntityView): |
|
352 |
"""base class for entity views which may also be applied to None |
|
353 |
result set (usually a default rql is provided by the view class) |
|
354 |
""" |
|
355 |
__registerer__ = accepts_registerer |
|
633 | 356 |
__selectors__ = ((none_rset | implements('Any')),) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
357 |
|
0 | 358 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
359 |
|
0 | 360 |
def __init__(self, req, rset): |
361 |
super(EntityStartupView, self).__init__(req, rset) |
|
362 |
if rset is None: |
|
363 |
# this instance is not in the "entityview" category |
|
364 |
self.category = 'startupview' |
|
365 |
||
366 |
def startup_rql(self): |
|
367 |
"""return some rql to be executedif the result set is None""" |
|
368 |
return self.default_rql |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
369 |
|
0 | 370 |
def call(self, **kwargs): |
371 |
"""override call to execute rql returned by the .startup_rql |
|
372 |
method if necessary |
|
373 |
""" |
|
374 |
if self.rset is None: |
|
375 |
self.rset = self.req.execute(self.startup_rql()) |
|
376 |
rset = self.rset |
|
377 |
for i in xrange(len(rset)): |
|
378 |
self.wview(self.id, rset, row=i, **kwargs) |
|
379 |
||
380 |
def url(self): |
|
381 |
"""return the url associated with this view. We can omit rql if we |
|
382 |
are on a result set on which we do not apply. |
|
383 |
""" |
|
384 |
if not self.__select__(self.req, self.rset): |
|
385 |
return self.build_url(vid=self.id) |
|
386 |
return super(EntityStartupView, self).url() |
|
387 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
388 |
|
0 | 389 |
class AnyRsetView(View): |
390 |
"""base class for views applying on any non empty result sets""" |
|
391 |
__registerer__ = priority_registerer |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
392 |
__selectors__ = (nonempty_rset,) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
393 |
|
0 | 394 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
395 |
|
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
396 |
def columns_labels(self, tr=True): |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
397 |
if tr: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
398 |
translate = display_name |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
399 |
else: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
400 |
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
|
401 |
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
|
402 |
labels = [] |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
403 |
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
|
404 |
# compute column header |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
else: |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
409 |
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
|
410 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
411 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
412 |
|
633 | 413 |
|
0 | 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' |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
655
diff
changeset
|
421 |
# __registerer__ = priority_registerer |
633 | 422 |
__selectors__ = (yes,) |
0 | 423 |
|
633 | 424 |
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
|
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 |
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'' |