author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 17 Feb 2009 21:29:58 +0100 | |
branch | tls-sprint |
changeset 692 | 800592b8d39b |
parent 688 | cddfbdee0eb3 |
child 742 | 99115e029dca |
permissions | -rw-r--r-- |
0 | 1 |
"""generic table view, including filtering abilities |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
513
907c18c01c60
while EntityView has been used here?? (changeset 501 by laure)
sylvain.thenault@logilab.fr
parents:
497
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 |
""" |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
527
diff
changeset
|
8 |
from __future__ import with_statement |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
527
diff
changeset
|
9 |
|
0 | 10 |
__docformat__ = "restructuredtext en" |
11 |
||
12 |
from simplejson import dumps |
|
13 |
||
14 |
from logilab.mtconverter import html_escape |
|
15 |
||
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
688
diff
changeset
|
16 |
from cubicweb.selectors import nonempty_rset, match_form_params, accept_rset |
0 | 17 |
from cubicweb.common.utils import make_uid |
18 |
from cubicweb.common.uilib import toggle_action, limitsize, jsonize, htmlescape |
|
19 |
from cubicweb.common.view import EntityView, AnyRsetView |
|
20 |
from cubicweb.web.htmlwidgets import (TableWidget, TableColumn, MenuWidget, |
|
21 |
PopupBoxMenu, BoxLink) |
|
22 |
from cubicweb.web.facet import prepare_facets_rqlst, filter_hiddens |
|
23 |
||
24 |
class TableView(AnyRsetView): |
|
25 |
id = 'table' |
|
26 |
title = _('table') |
|
27 |
finalview = 'final' |
|
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
28 |
|
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
29 |
def form_filter(self, divid, displaycols, displayactions, displayfilter, |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
30 |
hidden=True): |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
31 |
rqlst = self.rset.syntax_tree() |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
32 |
# union not yet supported |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
33 |
if len(rqlst.children) != 1: |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
34 |
return () |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
35 |
rqlst.save_state() |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
36 |
mainvar, baserql = prepare_facets_rqlst(rqlst, self.rset.args) |
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
37 |
wdgs = [facet.get_widget() for facet in self.vreg.possible_vobjects( |
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
38 |
'facets', self.req, self.rset, context='tablefilter', |
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
39 |
filtered_variable=mainvar)] |
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
40 |
wdgs = [wdg for wdg in wdgs if wdg is not None] |
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
41 |
rqlst.recover() |
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
42 |
if wdgs: |
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
43 |
self._generate_form(divid, baserql, wdgs, hidden, |
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
44 |
vidargs={'displaycols': displaycols, |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
45 |
'displayactions': displayactions, |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
46 |
'displayfilter': displayfilter}) |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
47 |
return self.show_hide_actions(divid, not hidden) |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
48 |
return () |
0 | 49 |
|
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
50 |
def _generate_form(self, divid, baserql, fwidgets, hidden=True, vidargs={}): |
0 | 51 |
"""display a form to filter table's content. This should only |
52 |
occurs when a context eid is given |
|
53 |
""" |
|
54 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js')) |
|
55 |
# drop False / None values from vidargs |
|
56 |
vidargs = dict((k, v) for k, v in vidargs.iteritems() if v) |
|
57 |
self.w(u'<form method="post" cubicweb:facetargs="%s" action="">' % |
|
58 |
html_escape(dumps([divid, 'table', False, vidargs]))) |
|
59 |
self.w(u'<fieldset id="%sForm" class="%s">' % (divid, hidden and 'hidden' or '')) |
|
60 |
self.w(u'<input type="hidden" name="divid" value="%s" />' % divid) |
|
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
61 |
filter_hiddens(self.w, facets=','.join(wdg.facet.id for wdg in fwidgets), baserql=baserql) |
0 | 62 |
self.w(u'<table class="filter">\n') |
63 |
self.w(u'<tr>\n') |
|
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
64 |
for wdg in fwidgets: |
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
65 |
self.w(u'<td>') |
521
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
66 |
wdg.render(w=self.w) |
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
67 |
self.w(u'</td>\n') |
0 | 68 |
self.w(u'</tr>\n') |
69 |
self.w(u'</table>\n') |
|
70 |
self.w(u'</fieldset>\n') |
|
71 |
self.w(u'</form>\n') |
|
72 |
||
73 |
def main_var_index(self): |
|
74 |
"""returns the index of the first non-attribute variable among the RQL |
|
75 |
selected variables |
|
76 |
""" |
|
77 |
eschema = self.vreg.schema.eschema |
|
78 |
for i, etype in enumerate(self.rset.description[0]): |
|
79 |
try: |
|
80 |
if not eschema(etype).is_final(): |
|
81 |
return i |
|
82 |
except KeyError: # XXX possible? |
|
83 |
continue |
|
84 |
return None |
|
85 |
||
86 |
def displaycols(self, displaycols): |
|
87 |
if displaycols is None: |
|
88 |
if 'displaycols' in self.req.form: |
|
89 |
displaycols = [int(idx) for idx in self.req.form['displaycols']] |
|
90 |
else: |
|
91 |
displaycols = range(len(self.rset.syntax_tree().children[0].selection)) |
|
92 |
return displaycols |
|
93 |
||
94 |
def call(self, title=None, subvid=None, displayfilter=None, headers=None, |
|
469 | 95 |
displaycols=None, displayactions=None, actions=(), divid=None, |
0 | 96 |
cellvids=None, cellattrs=None): |
97 |
"""Dumps a table displaying a composite query |
|
98 |
||
99 |
:param title: title added before table |
|
100 |
:param subvid: cell view |
|
101 |
:param displayfilter: filter that selects rows to display |
|
102 |
:param headers: columns' titles |
|
103 |
""" |
|
104 |
rset = self.rset |
|
105 |
req = self.req |
|
106 |
req.add_js('jquery.tablesorter.js') |
|
107 |
req.add_css('cubicweb.tablesorter.css') |
|
108 |
rqlst = rset.syntax_tree() |
|
109 |
# get rql description first since the filter form may remove some |
|
110 |
# necessary information |
|
111 |
rqlstdescr = rqlst.get_description()[0] # XXX missing Union support |
|
112 |
mainindex = self.main_var_index() |
|
113 |
hidden = True |
|
114 |
if not subvid and 'subvid' in req.form: |
|
115 |
subvid = req.form.pop('subvid') |
|
469 | 116 |
divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(rset)) |
0 | 117 |
actions = list(actions) |
118 |
if mainindex is None: |
|
119 |
displayfilter, displayactions = False, False |
|
120 |
else: |
|
121 |
if displayfilter is None and 'displayfilter' in req.form: |
|
122 |
displayfilter = True |
|
123 |
if req.form['displayfilter'] == 'shown': |
|
124 |
hidden = False |
|
125 |
if displayactions is None and 'displayactions' in req.form: |
|
126 |
displayactions = True |
|
127 |
displaycols = self.displaycols(displaycols) |
|
128 |
fromformfilter = 'fromformfilter' in req.form |
|
129 |
# if fromformfilter is true, this is an ajax call and we only want to |
|
130 |
# replace the inner div, so don't regenerate everything under the if |
|
131 |
# below |
|
132 |
if not fromformfilter: |
|
133 |
div_class = 'section' |
|
134 |
self.w(u'<div class="%s">' % div_class) |
|
135 |
if not title and 'title' in req.form: |
|
136 |
title = req.form['title'] |
|
137 |
if title: |
|
138 |
self.w(u'<h2 class="tableTitle">%s</h2>\n' % title) |
|
139 |
if displayfilter: |
|
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
140 |
actions += self.form_filter(divid, displaycols, displayfilter, |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
141 |
displayactions) |
0 | 142 |
elif displayfilter: |
143 |
actions += self.show_hide_actions(divid, True) |
|
144 |
self.w(u'<div id="%s"' % divid) |
|
145 |
if displayactions: |
|
146 |
for action in self.vreg.possible_actions(req, self.rset).get('mainactions', ()): |
|
147 |
actions.append( (action.url(), req._(action.title), action.html_class(), None) ) |
|
148 |
self.w(u' cubicweb:displayactions="1">') # close <div tag |
|
149 |
else: |
|
150 |
self.w(u'>') # close <div tag |
|
151 |
# render actions menu |
|
152 |
if actions: |
|
153 |
self.render_actions(divid, actions) |
|
154 |
# render table |
|
155 |
table = TableWidget(self) |
|
156 |
for column in self.get_columns(rqlstdescr, displaycols, headers, subvid, |
|
157 |
cellvids, cellattrs, mainindex): |
|
158 |
table.append_column(column) |
|
159 |
table.render(self.w) |
|
160 |
self.w(u'</div>\n') |
|
161 |
if not fromformfilter: |
|
162 |
self.w(u'</div>\n') |
|
163 |
||
164 |
||
165 |
def show_hide_actions(self, divid, currentlydisplayed=False): |
|
166 |
showhide = u';'.join(toggle_action('%s%s' % (divid, what))[11:] |
|
167 |
for what in ('Form', 'Show', 'Hide', 'Actions')) |
|
168 |
showhide = 'javascript:' + showhide |
|
169 |
showlabel = self.req._('show filter form') |
|
170 |
hidelabel = self.req._('hide filter form') |
|
171 |
if currentlydisplayed: |
|
172 |
return [(showhide, showlabel, 'hidden', '%sShow' % divid), |
|
173 |
(showhide, hidelabel, None, '%sHide' % divid)] |
|
174 |
return [(showhide, showlabel, None, '%sShow' % divid), |
|
175 |
(showhide, hidelabel, 'hidden', '%sHide' % divid)] |
|
176 |
||
177 |
def render_actions(self, divid, actions): |
|
178 |
box = MenuWidget('', 'tableActionsBox', _class='', islist=False) |
|
179 |
label = '<img src="%s" alt="%s"/>' % ( |
|
180 |
self.req.datadir_url + 'liveclipboard-icon.png', |
|
181 |
html_escape(self.req._('action(s) on this selection'))) |
|
182 |
menu = PopupBoxMenu(label, isitem=False, link_class='actionsBox', |
|
183 |
ident='%sActions' % divid) |
|
184 |
box.append(menu) |
|
185 |
for url, label, klass, ident in actions: |
|
186 |
menu.append(BoxLink(url, label, klass, ident=ident, escape=True)) |
|
187 |
box.render(w=self.w) |
|
188 |
self.w(u'<div class="clear"/>') |
|
189 |
||
190 |
def get_columns(self, rqlstdescr, displaycols, headers, subvid, cellvids, |
|
191 |
cellattrs, mainindex): |
|
192 |
columns = [] |
|
193 |
for colindex, attr in enumerate(rqlstdescr): |
|
194 |
if colindex not in displaycols: |
|
195 |
continue |
|
196 |
# compute column header |
|
197 |
if headers is not None: |
|
198 |
label = headers[displaycols.index(colindex)] |
|
199 |
elif colindex == 0 or attr == 'Any': # find a better label |
|
200 |
label = ','.join(display_name(self.req, et) |
|
201 |
for et in self.rset.column_types(colindex)) |
|
202 |
else: |
|
203 |
label = display_name(self.req, attr) |
|
204 |
if colindex == mainindex: |
|
205 |
label += ' (%s)' % self.rset.rowcount |
|
206 |
column = TableColumn(label, colindex) |
|
207 |
coltype = self.rset.description[0][colindex] |
|
208 |
# compute column cell view (if coltype is None, it's a left outer |
|
209 |
# join, use the default non final subvid) |
|
210 |
if cellvids and colindex in cellvids: |
|
211 |
column.append_renderer(cellvids[colindex], colindex) |
|
212 |
elif coltype is not None and self.schema.eschema(coltype).is_final(): |
|
213 |
column.append_renderer(self.finalview, colindex) |
|
214 |
else: |
|
215 |
column.append_renderer(subvid or 'incontext', colindex) |
|
216 |
||
217 |
if cellattrs and colindex in cellattrs: |
|
218 |
for name, value in cellattrs[colindex].iteritems(): |
|
219 |
column.add_attr(name,value) |
|
220 |
# add column |
|
221 |
columns.append(column) |
|
222 |
return columns |
|
223 |
||
224 |
||
225 |
def render(self, cellvid, row, col, w): |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
527
diff
changeset
|
226 |
from cubicweb.selectors import traced_selection |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
527
diff
changeset
|
227 |
with traced_selection( ('cell',) ): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
527
diff
changeset
|
228 |
self.view('cell', self.rset, row=row, col=col, cellvid=cellvid, w=w) |
0 | 229 |
|
230 |
def get_rows(self): |
|
231 |
return self.rset |
|
232 |
||
233 |
@htmlescape |
|
234 |
@jsonize |
|
235 |
@limitsize(10) |
|
236 |
def sortvalue(self, row, col): |
|
237 |
# XXX it might be interesting to try to limit value's |
|
238 |
# length as much as possible (e.g. by returning the 10 |
|
239 |
# first characters of a string) |
|
240 |
val = self.rset[row][col] |
|
241 |
if val is None: |
|
242 |
return u'' |
|
243 |
etype = self.rset.description[row][col] |
|
244 |
if self.schema.eschema(etype).is_final(): |
|
245 |
entity, rtype = self.rset.related_entity(row, col) |
|
246 |
if entity is None: |
|
247 |
return val # remove_html_tags() ? |
|
248 |
return entity.sortvalue(rtype) |
|
249 |
entity = self.rset.get_entity(row, col) |
|
250 |
return entity.sortvalue() |
|
251 |
||
252 |
class EditableTableView(TableView): |
|
253 |
id = 'editable-table' |
|
254 |
finalview = 'editable-final' |
|
255 |
title = _('editable-table') |
|
256 |
||
257 |
||
258 |
class CellView(EntityView): |
|
431 | 259 |
__selectors__ = (nonempty_rset, accept_rset) |
688
cddfbdee0eb3
remove all accepts = ('Foo',) declaration and use __selectors__ = implements('Foo') instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
260 |
# XXX backport implements('Any') ?? |
0 | 261 |
|
262 |
id = 'cell' |
|
263 |
||
264 |
def cell_call(self, row, col, cellvid=None): |
|
265 |
""" |
|
266 |
:param row, col: indexes locating the cell value in view's result set |
|
267 |
:param cellvid: cell view (defaults to 'outofcontext') |
|
268 |
""" |
|
269 |
etype, val = self.rset.description[row][col], self.rset[row][col] |
|
270 |
if val is not None and not self.schema.eschema(etype).is_final(): |
|
271 |
e = self.rset.get_entity(row, col) |
|
272 |
e.view(cellvid or 'outofcontext', w=self.w) |
|
273 |
elif val is None: |
|
274 |
# This is usually caused by a left outer join and in that case, |
|
275 |
# regular views will most certainly fail if they don't have |
|
276 |
# a real eid |
|
277 |
self.wview('final', self.rset, row=row, col=col) |
|
278 |
else: |
|
279 |
self.wview(cellvid or 'final', self.rset, 'null', row=row, col=col) |
|
280 |
||
281 |
||
282 |
class InitialTableView(TableView): |
|
283 |
"""same display as table view but consider two rql queries : |
|
284 |
|
|
285 |
* the default query (ie `rql` form parameter), which is only used to select |
|
286 |
this view and to build the filter form. This query should have the same |
|
287 |
structure as the actual without actual restriction (but link to |
|
288 |
restriction variables) and usually with a limit for efficiency (limit set |
|
289 |
to 2 is advised) |
|
290 |
|
|
291 |
* the actual query (`actualrql` form parameter) whose results will be |
|
292 |
displayed with default restrictions set |
|
293 |
""" |
|
294 |
id = 'initialtable' |
|
431 | 295 |
__selectors__ = nonempty_rset, match_form_params |
0 | 296 |
form_params = ('actualrql',) |
297 |
# should not be displayed in possible view since it expects some specific |
|
298 |
# parameters |
|
299 |
title = None |
|
300 |
||
469 | 301 |
def call(self, title=None, subvid=None, headers=None, divid=None, |
0 | 302 |
displaycols=None, displayactions=None): |
303 |
"""Dumps a table displaying a composite query""" |
|
304 |
actrql = self.req.form['actualrql'] |
|
305 |
self.ensure_ro_rql(actrql) |
|
306 |
displaycols = self.displaycols(displaycols) |
|
307 |
if displayactions is None and 'displayactions' in self.req.form: |
|
308 |
displayactions = True |
|
469 | 309 |
if divid is None and 'divid' in self.req.form: |
310 |
divid = self.req.form['divid'] |
|
0 | 311 |
self.w(u'<div class="section">') |
312 |
if not title and 'title' in self.req.form: |
|
313 |
# pop title so it's not displayed by the table view as well |
|
314 |
title = self.req.form.pop('title') |
|
315 |
if title: |
|
316 |
self.w(u'<h2>%s</h2>\n' % title) |
|
317 |
mainindex = self.main_var_index() |
|
318 |
if mainindex is not None: |
|
497
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
319 |
actions = self.form_filter(divid, displaycols, displayactions, True) |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
320 |
else: |
68d4211518c5
refactor table views to share more code and to avoid empty filter table in some case
sylvain.thenault@logilab.fr
parents:
469
diff
changeset
|
321 |
actions = () |
0 | 322 |
if not subvid and 'subvid' in self.req.form: |
323 |
subvid = self.req.form.pop('subvid') |
|
324 |
self.view('table', self.req.execute(actrql), |
|
325 |
'noresult', w=self.w, displayfilter=False, subvid=subvid, |
|
326 |
displayactions=displayactions, displaycols=displaycols, |
|
469 | 327 |
actions=actions, headers=headers, divid=divid) |
0 | 328 |
self.w(u'</div>\n') |
329 |
||
330 |
||
527 | 331 |
class EditableInitialTableTableView(InitialTableView): |
0 | 332 |
id = 'editable-initialtable' |
333 |
finalview = 'editable-final' |
|
334 |