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