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