author | sylvain.thenault@logilab.fr |
Wed, 28 Jan 2009 16:28:44 +0100 | |
changeset 497 | 68d4211518c5 |
parent 469 | 365826a58664 |
child 513 | 907c18c01c60 |
permissions | -rw-r--r-- |
0 | 1 |
"""generic table view, including filtering abilities |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
5 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
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) |
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 |
facets = [facet for facet in self.vreg.possible_vobjects( |
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', |
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 |
filtered_variable=mainvar) |
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
|
39 |
if facet.get_widget()] |
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() |
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 |
if facets: |
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
|
42 |
self._generate_form(divid, baserql, facets, 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
|
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 |
|
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
|
49 |
def _generate_form(self, divid, baserql, facets, 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 |
""" |
|
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
|
53 |
wdgs = [facet.get_widget() for facet in facets] |
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
|
54 |
if not wdgs: |
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
|
55 |
return |
0 | 56 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js')) |
57 |
# drop False / None values from vidargs |
|
58 |
vidargs = dict((k, v) for k, v in vidargs.iteritems() if v) |
|
59 |
self.w(u'<form method="post" cubicweb:facetargs="%s" action="">' % |
|
60 |
html_escape(dumps([divid, 'table', False, vidargs]))) |
|
61 |
self.w(u'<fieldset id="%sForm" class="%s">' % (divid, hidden and 'hidden' or '')) |
|
62 |
self.w(u'<input type="hidden" name="divid" value="%s" />' % divid) |
|
63 |
filter_hiddens(self.w, facets=','.join(facet.id for facet in facets), baserql=baserql) |
|
64 |
self.w(u'<table class="filter">\n') |
|
65 |
self.w(u'<tr>\n') |
|
66 |
for facet in facets: |
|
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>') |
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
|
68 |
facet.get_widget().render(w=self.w) |
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
|
69 |
self.w(u'</td>\n') |
0 | 70 |
self.w(u'</tr>\n') |
71 |
self.w(u'</table>\n') |
|
72 |
self.w(u'</fieldset>\n') |
|
73 |
self.w(u'</form>\n') |
|
74 |
||
75 |
def main_var_index(self): |
|
76 |
"""returns the index of the first non-attribute variable among the RQL |
|
77 |
selected variables |
|
78 |
""" |
|
79 |
eschema = self.vreg.schema.eschema |
|
80 |
for i, etype in enumerate(self.rset.description[0]): |
|
81 |
try: |
|
82 |
if not eschema(etype).is_final(): |
|
83 |
return i |
|
84 |
except KeyError: # XXX possible? |
|
85 |
continue |
|
86 |
return None |
|
87 |
||
88 |
def displaycols(self, displaycols): |
|
89 |
if displaycols is None: |
|
90 |
if 'displaycols' in self.req.form: |
|
91 |
displaycols = [int(idx) for idx in self.req.form['displaycols']] |
|
92 |
else: |
|
93 |
displaycols = range(len(self.rset.syntax_tree().children[0].selection)) |
|
94 |
return displaycols |
|
95 |
||
96 |
def call(self, title=None, subvid=None, displayfilter=None, headers=None, |
|
469 | 97 |
displaycols=None, displayactions=None, actions=(), divid=None, |
0 | 98 |
cellvids=None, cellattrs=None): |
99 |
"""Dumps a table displaying a composite query |
|
100 |
||
101 |
:param title: title added before table |
|
102 |
:param subvid: cell view |
|
103 |
:param displayfilter: filter that selects rows to display |
|
104 |
:param headers: columns' titles |
|
105 |
""" |
|
106 |
rset = self.rset |
|
107 |
req = self.req |
|
108 |
req.add_js('jquery.tablesorter.js') |
|
109 |
req.add_css('cubicweb.tablesorter.css') |
|
110 |
rqlst = rset.syntax_tree() |
|
111 |
# get rql description first since the filter form may remove some |
|
112 |
# necessary information |
|
113 |
rqlstdescr = rqlst.get_description()[0] # XXX missing Union support |
|
114 |
mainindex = self.main_var_index() |
|
115 |
hidden = True |
|
116 |
if not subvid and 'subvid' in req.form: |
|
117 |
subvid = req.form.pop('subvid') |
|
469 | 118 |
divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(rset)) |
0 | 119 |
actions = list(actions) |
120 |
if mainindex is None: |
|
121 |
displayfilter, displayactions = False, False |
|
122 |
else: |
|
123 |
if displayfilter is None and 'displayfilter' in req.form: |
|
124 |
displayfilter = True |
|
125 |
if req.form['displayfilter'] == 'shown': |
|
126 |
hidden = False |
|
127 |
if displayactions is None and 'displayactions' in req.form: |
|
128 |
displayactions = True |
|
129 |
displaycols = self.displaycols(displaycols) |
|
130 |
fromformfilter = 'fromformfilter' in req.form |
|
131 |
# if fromformfilter is true, this is an ajax call and we only want to |
|
132 |
# replace the inner div, so don't regenerate everything under the if |
|
133 |
# below |
|
134 |
if not fromformfilter: |
|
135 |
div_class = 'section' |
|
136 |
self.w(u'<div class="%s">' % div_class) |
|
137 |
if not title and 'title' in req.form: |
|
138 |
title = req.form['title'] |
|
139 |
if title: |
|
140 |
self.w(u'<h2 class="tableTitle">%s</h2>\n' % title) |
|
141 |
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
|
142 |
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
|
143 |
displayactions) |
0 | 144 |
elif displayfilter: |
145 |
actions += self.show_hide_actions(divid, True) |
|
146 |
self.w(u'<div id="%s"' % divid) |
|
147 |
if displayactions: |
|
148 |
for action in self.vreg.possible_actions(req, self.rset).get('mainactions', ()): |
|
149 |
actions.append( (action.url(), req._(action.title), action.html_class(), None) ) |
|
150 |
self.w(u' cubicweb:displayactions="1">') # close <div tag |
|
151 |
else: |
|
152 |
self.w(u'>') # close <div tag |
|
153 |
# render actions menu |
|
154 |
if actions: |
|
155 |
self.render_actions(divid, actions) |
|
156 |
# render table |
|
157 |
table = TableWidget(self) |
|
158 |
for column in self.get_columns(rqlstdescr, displaycols, headers, subvid, |
|
159 |
cellvids, cellattrs, mainindex): |
|
160 |
table.append_column(column) |
|
161 |
table.render(self.w) |
|
162 |
self.w(u'</div>\n') |
|
163 |
if not fromformfilter: |
|
164 |
self.w(u'</div>\n') |
|
165 |
||
166 |
||
167 |
def show_hide_actions(self, divid, currentlydisplayed=False): |
|
168 |
showhide = u';'.join(toggle_action('%s%s' % (divid, what))[11:] |
|
169 |
for what in ('Form', 'Show', 'Hide', 'Actions')) |
|
170 |
showhide = 'javascript:' + showhide |
|
171 |
showlabel = self.req._('show filter form') |
|
172 |
hidelabel = self.req._('hide filter form') |
|
173 |
if currentlydisplayed: |
|
174 |
return [(showhide, showlabel, 'hidden', '%sShow' % divid), |
|
175 |
(showhide, hidelabel, None, '%sHide' % divid)] |
|
176 |
return [(showhide, showlabel, None, '%sShow' % divid), |
|
177 |
(showhide, hidelabel, 'hidden', '%sHide' % divid)] |
|
178 |
||
179 |
def render_actions(self, divid, actions): |
|
180 |
box = MenuWidget('', 'tableActionsBox', _class='', islist=False) |
|
181 |
label = '<img src="%s" alt="%s"/>' % ( |
|
182 |
self.req.datadir_url + 'liveclipboard-icon.png', |
|
183 |
html_escape(self.req._('action(s) on this selection'))) |
|
184 |
menu = PopupBoxMenu(label, isitem=False, link_class='actionsBox', |
|
185 |
ident='%sActions' % divid) |
|
186 |
box.append(menu) |
|
187 |
for url, label, klass, ident in actions: |
|
188 |
menu.append(BoxLink(url, label, klass, ident=ident, escape=True)) |
|
189 |
box.render(w=self.w) |
|
190 |
self.w(u'<div class="clear"/>') |
|
191 |
||
192 |
def get_columns(self, rqlstdescr, displaycols, headers, subvid, cellvids, |
|
193 |
cellattrs, mainindex): |
|
194 |
columns = [] |
|
195 |
for colindex, attr in enumerate(rqlstdescr): |
|
196 |
if colindex not in displaycols: |
|
197 |
continue |
|
198 |
# compute column header |
|
199 |
if headers is not None: |
|
200 |
label = headers[displaycols.index(colindex)] |
|
201 |
elif colindex == 0 or attr == 'Any': # find a better label |
|
202 |
label = ','.join(display_name(self.req, et) |
|
203 |
for et in self.rset.column_types(colindex)) |
|
204 |
else: |
|
205 |
label = display_name(self.req, attr) |
|
206 |
if colindex == mainindex: |
|
207 |
label += ' (%s)' % self.rset.rowcount |
|
208 |
column = TableColumn(label, colindex) |
|
209 |
coltype = self.rset.description[0][colindex] |
|
210 |
# compute column cell view (if coltype is None, it's a left outer |
|
211 |
# join, use the default non final subvid) |
|
212 |
if cellvids and colindex in cellvids: |
|
213 |
column.append_renderer(cellvids[colindex], colindex) |
|
214 |
elif coltype is not None and self.schema.eschema(coltype).is_final(): |
|
215 |
column.append_renderer(self.finalview, colindex) |
|
216 |
else: |
|
217 |
column.append_renderer(subvid or 'incontext', colindex) |
|
218 |
||
219 |
if cellattrs and colindex in cellattrs: |
|
220 |
for name, value in cellattrs[colindex].iteritems(): |
|
221 |
column.add_attr(name,value) |
|
222 |
# add column |
|
223 |
columns.append(column) |
|
224 |
return columns |
|
225 |
||
226 |
||
227 |
def render(self, cellvid, row, col, w): |
|
228 |
self.view('cell', self.rset, row=row, col=col, cellvid=cellvid, w=w) |
|
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) |
0 | 260 |
|
261 |
id = 'cell' |
|
262 |
accepts = ('Any',) |
|
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 |
||
331 |
class EditableInitiableTableView(InitialTableView): |
|
332 |
id = 'editable-initialtable' |
|
333 |
finalview = 'editable-final' |
|
334 |