basic views for python values (eg without any result set)
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 28 May 2009 18:32:51 +0200
changeset 1987 d495e9ce9367
parent 1986 96c0e56cb0cf
child 1988 b5c5c088bb03
basic views for python values (eg without any result set)
selectors.py
web/test/unittest_views_pyviews.py
web/views/pyviews.py
--- a/selectors.py	Thu May 28 18:32:12 2009 +0200
+++ b/selectors.py	Thu May 28 18:32:51 2009 +0200
@@ -40,7 +40,6 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
-
 __docformat__ = "restructuredtext en"
 
 import logging
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/unittest_views_pyviews.py	Thu May 28 18:32:51 2009 +0200
@@ -0,0 +1,25 @@
+from logilab.common.testlib import unittest_main
+from cubicweb.devtools.apptest import EnvBasedTC
+
+class PyViewsTC(EnvBasedTC):
+
+    def test_pyvaltable(self):
+        content = self.vreg.view('pyvaltable', self.request(),
+                                 pyvalue=[[1, 'a'], [2, 'b']],
+                                 headers=['num', 'char'])
+        self.assertEquals(content.strip(), '''<table class="listing">
+<tr><th>num</th><th>char</th></tr>
+<tr><td>1</td><td>a</td></tr>
+<tr><td>2</td><td>b</td></tr>
+</table>''')
+
+    def test_pyvallist(self):
+        content = self.vreg.view('pyvallist', self.request(),
+                                 pyvalue=[1, 'a'])
+        self.assertEquals(content.strip(), '''<ul>
+<li>1</li>
+<li>a</li>
+</ul>''')
+
+if __name__ == '__main__':
+    unittest_main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/views/pyviews.py	Thu May 28 18:32:51 2009 +0200
@@ -0,0 +1,42 @@
+"""Views to display bare python values
+
+:organization: Logilab
+:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
+"""
+__docformat__ = "restructuredtext en"
+
+from cubicweb.view import View
+from cubicweb.selectors import match_kwargs
+
+class PyValTableView(View):
+    id = 'pyvaltable'
+    __select__ = match_kwargs('pyvalue')
+
+    def call(self, pyvalue, headers=None):
+        if headers is None:
+            headers = self.req.form.get('headers')
+        self.w(u'<table class="listing">\n')
+        if headers:
+            self.w(u'<tr>')
+            for header in headers:
+                self.w(u'<th>%s</th>' % header)
+            self.w(u'</tr>\n')
+        for row in pyvalue:
+            self.w(u'<tr>')
+            for cell in row:
+                self.w(u'<td>%s</td>' % cell)
+            self.w(u'</tr>\n')
+        self.w(u'</table>\n')
+
+
+class PyValListView(View):
+    id = 'pyvallist'
+    __select__ = match_kwargs('pyvalue')
+
+    def call(self, pyvalue):
+        self.w(u'<ul>\n')
+        for line in pyvalue:
+            self.w(u'<li>%s</li>\n' % line)
+        self.w(u'</ul>\n')