web/views/pyviews.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 21 Oct 2011 09:24:48 +0200
changeset 7980 c85bea091a6c
parent 7903 ac658ab4c7b7
child 7992 4ff9f25cb06e
permissions -rw-r--r--
[test] fix failures introduced by 7951:b7c825b00f64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4863
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6088
28f49a4fe702 [doc] docstring more explicit for pyviews
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5424
diff changeset
    18
"""Basic views for python values (eg without any result set)
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
"""
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
from cubicweb.view import View
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
from cubicweb.selectors import match_kwargs
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
6088
28f49a4fe702 [doc] docstring more explicit for pyviews
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5424
diff changeset
    25
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
class PyValTableView(View):
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    27
    """display a list of list of values into an HTML table.
6802
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    28
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    29
    Take care, content is NOT xml-escaped.
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    30
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    31
    If `headers` is specfied, it is expected to be a list of headers to be
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    32
    inserted as first row (in <thead>).
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    33
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    34
    If `colheaders` is True, the first column will be considered as an headers
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    35
    column an its values will be inserted inside <th> instead of <td>.
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    36
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    37
    `cssclass` is the CSS class used on the <table> tag, and default to
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    38
    'listing' (so that the table will look similar to those generated by the
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    39
    table view).
6802
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    40
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1987
diff changeset
    41
    __regid__ = 'pyvaltable'
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
    __select__ = match_kwargs('pyvalue')
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    44
    def call(self, pyvalue, headers=None, colheaders=False,
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    45
             cssclass='listing'):
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        if headers is None:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    47
            headers = self._cw.form.get('headers')
4863
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    48
        w = self.w
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    49
        w(u'<table class="%s">\n' % cssclass)
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
        if headers:
4863
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    51
            w(u'<thead>')
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    52
            w(u'<tr>')
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
            for header in headers:
4863
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    54
                w(u'<th>%s</th>' % header)
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    55
            w(u'</tr>\n')
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    56
            w(u'</thead>')
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    57
        w(u'<tbody>')
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
        for row in pyvalue:
4863
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    59
            w(u'<tr>')
7903
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    60
            if colheaders:
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    61
                w(u'<th>%s</th>' % row[0])
ac658ab4c7b7 [pyviews] new colheaders and cssclass arguments on pyvaltable (closes #1979718)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6802
diff changeset
    62
                row = row[1:]
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
            for cell in row:
4863
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    64
                w(u'<td>%s</td>' % cell)
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    65
            w(u'</tr>\n')
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    66
        w(u'</tbody>')
f6cf0fcd40ae [pyviews] add thead/tbody to pyvaltable view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    67
        w(u'</table>\n')
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
class PyValListView(View):
6802
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    71
    """display a list of values into an html list.
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    72
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    73
    Take care, content is NOT xml-escaped.
ff9059e144f9 [pyviews] document the fact that content is not xml-escaped (closes #1382038)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6088
diff changeset
    74
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1987
diff changeset
    75
    __regid__ = 'pyvallist'
1987
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
    __select__ = match_kwargs('pyvalue')
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
    def call(self, pyvalue):
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
        self.w(u'<ul>\n')
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
        for line in pyvalue:
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
            self.w(u'<li>%s</li>\n' % line)
d495e9ce9367 basic views for python values (eg without any result set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
        self.w(u'</ul>\n')