|
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """Basic views for python values (eg without any result set) |
|
19 """ |
|
20 __docformat__ = "restructuredtext en" |
|
21 |
|
22 from six import text_type |
|
23 from six.moves import range |
|
24 |
|
25 from cubicweb.view import View |
|
26 from cubicweb.predicates import match_kwargs |
|
27 from cubicweb.web.views import tableview |
|
28 |
|
29 |
|
30 class PyValTableColRenderer(tableview.AbstractColumnRenderer): |
|
31 """Default column renderer for :class:`PyValTableView`.""" |
|
32 def bind(self, view, colid): |
|
33 super(PyValTableColRenderer, self).bind(view, colid) |
|
34 self.header = view.headers[colid] if view.headers else None |
|
35 self.data = view.pyvalue |
|
36 |
|
37 def render_header(self, w): |
|
38 if self.header: |
|
39 w(self._cw._(self.header)) |
|
40 else: |
|
41 w(self.empty_cell_content) |
|
42 |
|
43 def render_cell(self, w, rownum): |
|
44 w(text_type(self.data[rownum][self.colid])) |
|
45 |
|
46 |
|
47 class PyValTableView(tableview.TableMixIn, View): |
|
48 """This table view is designed to be used a list of list of unicode values |
|
49 given as a mandatory `pyvalue` argument. Take care, content is NOT |
|
50 xml-escaped. |
|
51 |
|
52 It's configured through the following selection arguments. |
|
53 |
|
54 If `headers` is specified, it is expected to be a list of headers to be |
|
55 inserted as first row (in <thead>). |
|
56 |
|
57 `header_column_idx` may be used to specify a column index or a set of column |
|
58 indiced where values should be inserted inside <th> tag instead of <td>. |
|
59 |
|
60 `cssclass` is the CSS class used on the <table> tag, and default to |
|
61 'listing' (so that the table will look similar to those generated by the |
|
62 table view). |
|
63 """ |
|
64 __regid__ = 'pyvaltable' |
|
65 __select__ = match_kwargs('pyvalue') |
|
66 default_column_renderer_class = PyValTableColRenderer |
|
67 paginable = False # not supported |
|
68 headers = None |
|
69 cssclass = None |
|
70 domid = None |
|
71 |
|
72 def __init__(self, req, pyvalue, headers=None, cssclass=None, |
|
73 header_column_idx=None, **kwargs): |
|
74 super(PyValTableView, self).__init__(req, **kwargs) |
|
75 self.pyvalue = pyvalue |
|
76 if headers is not None: |
|
77 self.headers = headers |
|
78 elif self.headers: # headers set on a class attribute, translate |
|
79 self.headers = [self._cw._(header) for header in self.headers] |
|
80 if cssclass is not None: |
|
81 self.cssclass = cssclass |
|
82 self.header_column_idx = header_column_idx |
|
83 |
|
84 @property |
|
85 def layout_args(self): |
|
86 args = {} |
|
87 if self.cssclass: |
|
88 args['cssclass'] = self.cssclass |
|
89 if self.header_column_idx is not None: |
|
90 args['header_column_idx'] = self.header_column_idx |
|
91 return args |
|
92 |
|
93 # layout callbacks ######################################################### |
|
94 |
|
95 @property |
|
96 def table_size(self): |
|
97 """return the number of rows (header excluded) to be displayed""" |
|
98 return len(self.pyvalue) |
|
99 |
|
100 @property |
|
101 def has_headers(self): |
|
102 return self.headers |
|
103 |
|
104 def build_column_renderers(self): |
|
105 return [self.column_renderer(colid) |
|
106 for colid in range(len(self.pyvalue[0]))] |
|
107 |
|
108 def facets_form(self, mainvar=None): |
|
109 return None # not supported |
|
110 |
|
111 def table_actions(self): |
|
112 return [] # not supported |
|
113 |
|
114 |
|
115 class PyValListView(View): |
|
116 """display a list of values into an html list. |
|
117 |
|
118 Take care, content is NOT xml-escaped. |
|
119 """ |
|
120 __regid__ = 'pyvallist' |
|
121 __select__ = match_kwargs('pyvalue') |
|
122 |
|
123 def call(self, pyvalue): |
|
124 self.w(u'<ul>\n') |
|
125 for line in pyvalue: |
|
126 self.w(u'<li>%s</li>\n' % line) |
|
127 self.w(u'</ul>\n') |