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