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