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