author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 22 Apr 2010 19:48:04 +0000 | |
branch | stable |
changeset 5377 | 84d14ddfae13 |
parent 5312 | d2dbba898a96 |
child 5385 | b6e250dd7a7d |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""generic table view, including filtering abilities |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3842
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1730
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5312
diff
changeset
|
11 |
try: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5312
diff
changeset
|
12 |
from json import dumps |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5312
diff
changeset
|
13 |
except ImportError: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5312
diff
changeset
|
14 |
from simplejson import dumps |
0 | 15 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2226
diff
changeset
|
16 |
from logilab.mtconverter import xml_escape |
0 | 17 |
|
1132 | 18 |
from cubicweb.selectors import nonempty_rset, match_form_params |
762 | 19 |
from cubicweb.utils import make_uid |
20 |
from cubicweb.view import EntityView, AnyRsetView |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
21 |
from cubicweb import tags |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
22 |
from cubicweb.uilib import toggle_action, limitsize, htmlescape |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
23 |
from cubicweb.web import jsonize |
0 | 24 |
from cubicweb.web.htmlwidgets import (TableWidget, TableColumn, MenuWidget, |
762 | 25 |
PopupBoxMenu, BoxLink) |
0 | 26 |
from cubicweb.web.facet import prepare_facets_rqlst, filter_hiddens |
27 |
||
28 |
class TableView(AnyRsetView): |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
29 |
"""The table view accepts any non-empty rset. It uses |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
30 |
introspection on the result set to compute column names and the |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
31 |
proper way to display the cells. |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
32 |
It is however highly configurable and accepts a wealth of options. |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
33 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
34 |
__regid__ = 'table' |
0 | 35 |
title = _('table') |
36 |
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
|
37 |
|
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 |
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
|
39 |
hidden=True): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
40 |
rqlst = self.cw_rset.syntax_tree() |
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 |
# 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
|
42 |
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
|
43 |
return () |
4616
2f296c34aec4
fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
44 |
rqlst = rqlst.copy() |
2f296c34aec4
fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
45 |
self._cw.vreg.rqlhelper.annotate(rqlst) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
46 |
mainvar, baserql = prepare_facets_rqlst(rqlst, self.cw_rset.args) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
47 |
wdgs = [facet.get_widget() for facet in self._cw.vreg['facets'].poss_visible_objects( |
4618
412a9f1f5fd0
[facet] rqlst should be the select node
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4617
diff
changeset
|
48 |
self._cw, rset=self.cw_rset, rqlst=rqlst.children[0], 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
|
49 |
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
|
50 |
wdgs = [wdg for wdg in wdgs if wdg is not None] |
b3430af22580
fix table filter form: get_widget shouldn't be called once rqlst has been recovered
sylvain.thenault@logilab.fr
parents:
513
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
'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
|
55 |
'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
|
56 |
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
|
57 |
return () |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
58 |
|
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 |
def _generate_form(self, divid, baserql, fwidgets, hidden=True, vidargs={}): |
0 | 60 |
"""display a form to filter table's content. This should only |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
61 |
occur when a context eid is given |
0 | 62 |
""" |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
63 |
w = self.w |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
64 |
self._cw.add_css('cubicweb.facets.css') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
65 |
self._cw.add_js( ('cubicweb.ajax.js', 'cubicweb.facets.js')) |
0 | 66 |
# drop False / None values from vidargs |
67 |
vidargs = dict((k, v) for k, v in vidargs.iteritems() if v) |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
68 |
w(u'<form method="post" cubicweb:facetargs="%s" action="">' % |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
69 |
xml_escape(dumps([divid, 'table', False, vidargs]))) |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
70 |
w(u'<fieldset id="%sForm" class="%s">' % (divid, hidden and 'hidden' or '')) |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
71 |
w(u'<input type="hidden" name="divid" value="%s" />' % divid) |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
72 |
w(u'<input type="hidden" name="fromformfilter" value="1" />') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
73 |
filter_hiddens(w, facets=','.join(wdg.facet.__regid__ for wdg in fwidgets), |
3129
fab471bee6c1
[facets] duhh, necessary fromformfilter hidden was missing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3128
diff
changeset
|
74 |
baserql=baserql) |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
75 |
w(u'<table class="filter">\n') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
76 |
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
|
77 |
for wdg in fwidgets: |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
78 |
w(u'<td>') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
79 |
wdg.render(w=w) |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
80 |
w(u'</td>\n') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
81 |
w(u'</tr>\n') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
82 |
w(u'</table>\n') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
83 |
w(u'</fieldset>\n') |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
84 |
w(u'</form>\n') |
0 | 85 |
|
86 |
def main_var_index(self): |
|
87 |
"""returns the index of the first non-attribute variable among the RQL |
|
88 |
selected variables |
|
89 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
90 |
eschema = self._cw.vreg.schema.eschema |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
91 |
for i, etype in enumerate(self.cw_rset.description[0]): |
0 | 92 |
try: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3228
diff
changeset
|
93 |
if not eschema(etype).final: |
0 | 94 |
return i |
95 |
except KeyError: # XXX possible? |
|
96 |
continue |
|
97 |
return None |
|
98 |
||
99 |
def displaycols(self, displaycols): |
|
100 |
if displaycols is None: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
101 |
if 'displaycols' in self._cw.form: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
102 |
displaycols = [int(idx) for idx in self._cw.form['displaycols']] |
0 | 103 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
104 |
displaycols = range(len(self.cw_rset.syntax_tree().children[0].selection)) |
0 | 105 |
return displaycols |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
106 |
|
0 | 107 |
def call(self, title=None, subvid=None, displayfilter=None, headers=None, |
469 | 108 |
displaycols=None, displayactions=None, actions=(), divid=None, |
2226
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
109 |
cellvids=None, cellattrs=None, mainindex=None): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4618
diff
changeset
|
110 |
"""Produces a table displaying a composite query |
0 | 111 |
|
112 |
:param title: title added before table |
|
113 |
:param subvid: cell view |
|
114 |
:param displayfilter: filter that selects rows to display |
|
115 |
:param headers: columns' titles |
|
116 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
117 |
req = self._cw |
0 | 118 |
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
|
119 |
req.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css')) |
2226
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
120 |
# compute label first since the filter form may remove some necessary |
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
121 |
# information from the rql syntax tree |
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
122 |
if mainindex is None: |
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
123 |
mainindex = self.main_var_index() |
2206
009e2856afcb
columns_labels is an AnyRsetView method...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2204
diff
changeset
|
124 |
computed_labels = self.columns_labels(mainindex) |
0 | 125 |
hidden = True |
126 |
if not subvid and 'subvid' in req.form: |
|
127 |
subvid = req.form.pop('subvid') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
128 |
divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(self.cw_rset)) |
0 | 129 |
actions = list(actions) |
130 |
if mainindex is None: |
|
131 |
displayfilter, displayactions = False, False |
|
132 |
else: |
|
133 |
if displayfilter is None and 'displayfilter' in req.form: |
|
134 |
displayfilter = True |
|
135 |
if req.form['displayfilter'] == 'shown': |
|
136 |
hidden = False |
|
137 |
if displayactions is None and 'displayactions' in req.form: |
|
138 |
displayactions = True |
|
139 |
displaycols = self.displaycols(displaycols) |
|
140 |
fromformfilter = 'fromformfilter' in req.form |
|
141 |
# if fromformfilter is true, this is an ajax call and we only want to |
|
142 |
# replace the inner div, so don't regenerate everything under the if |
|
143 |
# below |
|
144 |
if not fromformfilter: |
|
3009 | 145 |
self.w(u'<div class="section">') |
0 | 146 |
if not title and 'title' in req.form: |
147 |
title = req.form['title'] |
|
148 |
if title: |
|
149 |
self.w(u'<h2 class="tableTitle">%s</h2>\n' % title) |
|
150 |
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
|
151 |
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
|
152 |
displayactions) |
0 | 153 |
elif displayfilter: |
154 |
actions += self.show_hide_actions(divid, True) |
|
155 |
self.w(u'<div id="%s"' % divid) |
|
156 |
if displayactions: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
157 |
actionsbycat = self._cw.vreg['actions'].possible_actions(req, self.cw_rset) |
3228
7b05b2709439
[actions] refactor: extract actual_actions from fill_menu to work with table filter form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3129
diff
changeset
|
158 |
for action in actionsbycat.get('mainactions', ()): |
7b05b2709439
[actions] refactor: extract actual_actions from fill_menu to work with table filter form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3129
diff
changeset
|
159 |
for action in action.actual_actions(): |
7b05b2709439
[actions] refactor: extract actual_actions from fill_menu to work with table filter form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3129
diff
changeset
|
160 |
actions.append( (action.url(), req._(action.title), |
7b05b2709439
[actions] refactor: extract actual_actions from fill_menu to work with table filter form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3129
diff
changeset
|
161 |
action.html_class(), None) ) |
0 | 162 |
self.w(u' cubicweb:displayactions="1">') # close <div tag |
163 |
else: |
|
164 |
self.w(u'>') # close <div tag |
|
165 |
# render actions menu |
|
166 |
if actions: |
|
167 |
self.render_actions(divid, actions) |
|
168 |
# render table |
|
169 |
table = TableWidget(self) |
|
2204
3472c5498478
use column_labels instead of rqlst.get_description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2115
diff
changeset
|
170 |
for column in self.get_columns(computed_labels, displaycols, headers, |
3472c5498478
use column_labels instead of rqlst.get_description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2115
diff
changeset
|
171 |
subvid, cellvids, cellattrs, mainindex): |
0 | 172 |
table.append_column(column) |
173 |
table.render(self.w) |
|
174 |
self.w(u'</div>\n') |
|
175 |
if not fromformfilter: |
|
176 |
self.w(u'</div>\n') |
|
177 |
||
178 |
def show_hide_actions(self, divid, currentlydisplayed=False): |
|
179 |
showhide = u';'.join(toggle_action('%s%s' % (divid, what))[11:] |
|
180 |
for what in ('Form', 'Show', 'Hide', 'Actions')) |
|
181 |
showhide = 'javascript:' + showhide |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
182 |
showlabel = self._cw._('show filter form') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
183 |
hidelabel = self._cw._('hide filter form') |
0 | 184 |
if currentlydisplayed: |
185 |
return [(showhide, showlabel, 'hidden', '%sShow' % divid), |
|
186 |
(showhide, hidelabel, None, '%sHide' % divid)] |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
187 |
return [(showhide, showlabel, None, '%sShow' % divid), |
0 | 188 |
(showhide, hidelabel, 'hidden', '%sHide' % divid)] |
189 |
||
190 |
def render_actions(self, divid, actions): |
|
191 |
box = MenuWidget('', 'tableActionsBox', _class='', islist=False) |
|
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
192 |
label = tags.img(src=self._cw.external_resource('PUCE_DOWN'), |
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
193 |
alt=xml_escape(self._cw._('action(s) on this selection'))) |
0 | 194 |
menu = PopupBoxMenu(label, isitem=False, link_class='actionsBox', |
195 |
ident='%sActions' % divid) |
|
196 |
box.append(menu) |
|
197 |
for url, label, klass, ident in actions: |
|
198 |
menu.append(BoxLink(url, label, klass, ident=ident, escape=True)) |
|
199 |
box.render(w=self.w) |
|
200 |
self.w(u'<div class="clear"/>') |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
201 |
|
2204
3472c5498478
use column_labels instead of rqlst.get_description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2115
diff
changeset
|
202 |
def get_columns(self, computed_labels, displaycols, headers, subvid, |
3472c5498478
use column_labels instead of rqlst.get_description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2115
diff
changeset
|
203 |
cellvids, cellattrs, mainindex): |
0 | 204 |
columns = [] |
3729
e4f20b74af51
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
205 |
eschema = self._cw.vreg.schema.eschema |
2204
3472c5498478
use column_labels instead of rqlst.get_description
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2115
diff
changeset
|
206 |
for colindex, label in enumerate(computed_labels): |
0 | 207 |
if colindex not in displaycols: |
208 |
continue |
|
209 |
# compute column header |
|
210 |
if headers is not None: |
|
211 |
label = headers[displaycols.index(colindex)] |
|
212 |
if colindex == mainindex: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
213 |
label += ' (%s)' % self.cw_rset.rowcount |
0 | 214 |
column = TableColumn(label, colindex) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
215 |
coltype = self.cw_rset.description[0][colindex] |
0 | 216 |
# compute column cell view (if coltype is None, it's a left outer |
217 |
# join, use the default non final subvid) |
|
218 |
if cellvids and colindex in cellvids: |
|
219 |
column.append_renderer(cellvids[colindex], colindex) |
|
3729
e4f20b74af51
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
220 |
elif coltype is not None and eschema(coltype).final: |
0 | 221 |
column.append_renderer(self.finalview, colindex) |
222 |
else: |
|
223 |
column.append_renderer(subvid or 'incontext', colindex) |
|
224 |
if cellattrs and colindex in cellattrs: |
|
225 |
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
|
226 |
column.add_attr(name, value) |
0 | 227 |
# add column |
228 |
columns.append(column) |
|
229 |
return columns |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
230 |
|
0 | 231 |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1635
diff
changeset
|
232 |
def render_cell(self, cellvid, row, col, w): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
233 |
self._cw.view('cell', self.cw_rset, row=row, col=col, cellvid=cellvid, w=w) |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
234 |
|
0 | 235 |
def get_rows(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
236 |
return self.cw_rset |
0 | 237 |
|
238 |
@htmlescape |
|
239 |
@jsonize |
|
240 |
@limitsize(10) |
|
241 |
def sortvalue(self, row, col): |
|
242 |
# XXX it might be interesting to try to limit value's |
|
243 |
# length as much as possible (e.g. by returning the 10 |
|
244 |
# first characters of a string) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
245 |
val = self.cw_rset[row][col] |
0 | 246 |
if val is None: |
247 |
return u'' |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
248 |
etype = self.cw_rset.description[row][col] |
3729
e4f20b74af51
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
249 |
if self._cw.vreg.schema.eschema(etype).final: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
250 |
entity, rtype = self.cw_rset.related_entity(row, col) |
0 | 251 |
if entity is None: |
252 |
return val # remove_html_tags() ? |
|
253 |
return entity.sortvalue(rtype) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
254 |
entity = self.cw_rset.get_entity(row, col) |
0 | 255 |
return entity.sortvalue() |
256 |
||
843 | 257 |
|
0 | 258 |
class EditableTableView(TableView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
259 |
__regid__ = 'editable-table' |
0 | 260 |
finalview = 'editable-final' |
261 |
title = _('editable-table') |
|
262 |
||
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
263 |
|
0 | 264 |
class CellView(EntityView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
265 |
__regid__ = 'cell' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
266 |
__select__ = nonempty_rset() |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
267 |
|
0 | 268 |
def cell_call(self, row, col, cellvid=None): |
269 |
""" |
|
270 |
:param row, col: indexes locating the cell value in view's result set |
|
271 |
:param cellvid: cell view (defaults to 'outofcontext') |
|
272 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
273 |
etype, val = self.cw_rset.description[row][col], self.cw_rset[row][col] |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4023
diff
changeset
|
274 |
if val is not None and not self._cw.vreg.schema.eschema(etype).final: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
275 |
e = self.cw_rset.get_entity(row, col) |
0 | 276 |
e.view(cellvid or 'outofcontext', w=self.w) |
277 |
elif val is None: |
|
278 |
# This is usually caused by a left outer join and in that case, |
|
279 |
# regular views will most certainly fail if they don't have |
|
280 |
# a real eid |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
281 |
self.wview('final', self.cw_rset, row=row, col=col) |
0 | 282 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
283 |
self.wview(cellvid or 'final', self.cw_rset, 'null', row=row, col=col) |
0 | 284 |
|
285 |
||
286 |
class InitialTableView(TableView): |
|
287 |
"""same display as table view but consider two rql queries : |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
288 |
|
0 | 289 |
* the default query (ie `rql` form parameter), which is only used to select |
290 |
this view and to build the filter form. This query should have the same |
|
291 |
structure as the actual without actual restriction (but link to |
|
292 |
restriction variables) and usually with a limit for efficiency (limit set |
|
293 |
to 2 is advised) |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
294 |
|
0 | 295 |
* the actual query (`actualrql` form parameter) whose results will be |
296 |
displayed with default restrictions set |
|
297 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
298 |
__regid__ = 'initialtable' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
299 |
__select__ = nonempty_rset() & match_form_params('actualrql') |
0 | 300 |
# should not be displayed in possible view since it expects some specific |
301 |
# parameters |
|
302 |
title = None |
|
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1370
diff
changeset
|
303 |
|
469 | 304 |
def call(self, title=None, subvid=None, headers=None, divid=None, |
2226
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
305 |
displaycols=None, displayactions=None, mainindex=None): |
0 | 306 |
"""Dumps a table displaying a composite query""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
307 |
actrql = self._cw.form['actualrql'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
308 |
self._cw.ensure_ro_rql(actrql) |
0 | 309 |
displaycols = self.displaycols(displaycols) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
310 |
if displayactions is None and 'displayactions' in self._cw.form: |
0 | 311 |
displayactions = True |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
312 |
if divid is None and 'divid' in self._cw.form: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
313 |
divid = self._cw.form['divid'] |
0 | 314 |
self.w(u'<div class="section">') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
315 |
if not title and 'title' in self._cw.form: |
0 | 316 |
# pop title so it's not displayed by the table view as well |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
317 |
title = self._cw.form.pop('title') |
0 | 318 |
if title: |
319 |
self.w(u'<h2>%s</h2>\n' % title) |
|
2226
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
320 |
if mainindex is None: |
f5216e540168
allow to specify mainindex
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2206
diff
changeset
|
321 |
mainindex = self.main_var_index() |
0 | 322 |
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
|
323 |
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
|
324 |
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
|
325 |
actions = () |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
326 |
if not subvid and 'subvid' in self._cw.form: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
327 |
subvid = self._cw.form.pop('subvid') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
328 |
self._cw.view('table', self._cw.execute(actrql), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
329 |
'noresult', w=self.w, displayfilter=False, subvid=subvid, |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
330 |
displayactions=displayactions, displaycols=displaycols, |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
331 |
actions=actions, headers=headers, divid=divid) |
0 | 332 |
self.w(u'</div>\n') |
333 |
||
334 |
||
527 | 335 |
class EditableInitialTableTableView(InitialTableView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
336 |
__regid__ = 'editable-initialtable' |
0 | 337 |
finalview = 'editable-final' |