web/views/pyviews.py
brancholdstable
changeset 8462 a14b6562082b
parent 7992 4ff9f25cb06e
child 8190 2a3c1b787688
equal deleted inserted replaced
8231:1bb43e31032d 8462:a14b6562082b
    19 """
    19 """
    20 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    21 
    21 
    22 from cubicweb.view import View
    22 from cubicweb.view import View
    23 from cubicweb.selectors import match_kwargs
    23 from cubicweb.selectors import match_kwargs
       
    24 from cubicweb.web.views import tableview
    24 
    25 
    25 
    26 
    26 class PyValTableView(View):
    27 class PyValTableColRenderer(tableview.AbstractColumnRenderer):
    27     """display a list of list of values into an HTML table.
    28     """Default column renderer for :class:`PyValTableView`."""
       
    29     def bind(self, view, colid):
       
    30         super(PyValTableColRenderer, self).bind(view, colid)
       
    31         self.header = view.headers[colid] if view.headers else None
       
    32         self.data = view.pyvalue
    28 
    33 
    29     Take care, content is NOT xml-escaped.
    34     def render_header(self, w):
       
    35         if self.header:
       
    36             w(self._cw._(self.header))
       
    37         else:
       
    38             w(self.empty_cell_content)
    30 
    39 
    31     If `headers` is specfied, it is expected to be a list of headers to be
    40     def render_cell(self, w, rownum):
       
    41         w(unicode(self.data[rownum][self.colid]))
       
    42 
       
    43 
       
    44 class PyValTableView(tableview.TableMixIn, View):
       
    45     """This table view is designed to be used a list of list of unicode values
       
    46     given as a mandatory `pyvalue` argument. Take care, content is NOT
       
    47     xml-escaped.
       
    48 
       
    49     It's configured through the following selection arguments.
       
    50 
       
    51     If `headers` is specified, it is expected to be a list of headers to be
    32     inserted as first row (in <thead>).
    52     inserted as first row (in <thead>).
    33 
    53 
    34     If `colheaders` is True, the first column will be considered as an headers
    54     `header_column_idx` may be used to specify a column index or a set of column
    35     column an its values will be inserted inside <th> instead of <td>.
    55     indiced where values should be inserted inside <th> tag instead of <td>.
    36 
    56 
    37     `cssclass` is the CSS class used on the <table> tag, and default to
    57     `cssclass` is the CSS class used on the <table> tag, and default to
    38     'listing' (so that the table will look similar to those generated by the
    58     'listing' (so that the table will look similar to those generated by the
    39     table view).
    59     table view).
    40     """
    60     """
    41     __regid__ = 'pyvaltable'
    61     __regid__ = 'pyvaltable'
    42     __select__ = match_kwargs('pyvalue')
    62     __select__ = match_kwargs('pyvalue')
       
    63     default_column_renderer_class = PyValTableColRenderer
       
    64     paginable = False # not supported
       
    65     headers = None
       
    66     cssclass = None
       
    67     domid = None
    43 
    68 
    44     def call(self, pyvalue, headers=None, colheaders=False,
    69     def __init__(self, req, pyvalue, headers=None, cssclass=None,
    45              cssclass='listing'):
    70                  header_column_idx=None, **kwargs):
    46         if headers is None:
    71         super(PyValTableView, self).__init__(req, **kwargs)
    47             headers = self._cw.form.get('headers')
    72         self.pyvalue = pyvalue
    48         w = self.w
    73         if headers is not None:
    49         w(u'<table class="%s">\n' % cssclass)
    74             self.headers = headers
    50         if headers:
    75         elif self.headers: # headers set on a class attribute, translate
    51             w(u'<thead>')
    76             self.headers = [self._cw._(header) for header in self.headers]
    52             w(u'<tr>')
    77         if cssclass is not None:
    53             for header in headers:
    78             self.cssclass = cssclass
    54                 w(u'<th>%s</th>' % header)
    79         self.header_column_idx = header_column_idx
    55             w(u'</tr>\n')
    80 
    56             w(u'</thead>')
    81     @property
    57         w(u'<tbody>')
    82     def layout_args(self):
    58         for row in pyvalue:
    83         args = {}
    59             w(u'<tr>')
    84         if self.cssclass:
    60             if colheaders:
    85             args['cssclass'] = self.cssclass
    61                 w(u'<th>%s</th>' % row[0])
    86         if self.header_column_idx is not None:
    62                 row = row[1:]
    87             args['header_column_idx'] = self.header_column_idx
    63             for cell in row:
    88         return args
    64                 w(u'<td>%s</td>' % cell)
    89 
    65             w(u'</tr>\n')
    90     # layout callbacks #########################################################
    66         w(u'</tbody>')
    91 
    67         w(u'</table>\n')
    92     @property
       
    93     def table_size(self):
       
    94         """return the number of rows (header excluded) to be displayed"""
       
    95         return len(self.pyvalue)
       
    96 
       
    97     @property
       
    98     def has_headers(self):
       
    99         return self.headers
       
   100 
       
   101     def build_column_renderers(self):
       
   102         return [self.column_renderer(colid)
       
   103                 for colid in xrange(len(self.pyvalue[0]))]
       
   104 
       
   105     def facets_form(self, mainvar=None):
       
   106         return None # not supported
       
   107 
       
   108     def table_actions(self):
       
   109         return [] # not supported
    68 
   110 
    69 
   111 
    70 class PyValListView(View):
   112 class PyValListView(View):
    71     """display a list of values into an html list.
   113     """display a list of values into an html list.
    72 
   114