[web] Allow propagating 'klass' parameter on tree views recursively
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 29 Jan 2015 16:40:08 +0100
changeset 10167 de62952550d9
parent 10166 0095961df66c
child 10168 136e6b655cdb
[web] Allow propagating 'klass' parameter on tree views recursively Closes #4881302
web/test/data/entities.py
web/test/data/schema.py
web/test/unittest_views_treeview.py
web/views/baseviews.py
web/views/treeview.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/data/entities.py	Thu Jan 29 16:40:08 2015 +0100
@@ -0,0 +1,31 @@
+# copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
+from cubicweb.predicates import is_instance
+from cubicweb.entities.adapters import ITreeAdapter
+from cubicweb.entities import AnyEntity, fetch_config
+
+
+class TreeNode(AnyEntity):
+    __regid__ = 'TreeNode'
+    fetch_attrs, cw_fetch_order = fetch_config(['name'])
+
+
+class ITreeNode(ITreeAdapter):
+    __select__ = is_instance('TreeNode')
+    tree_relation = 'parent'
--- a/web/test/data/schema.py	Wed Jan 28 11:59:06 2015 +0100
+++ b/web/test/data/schema.py	Thu Jan 29 16:40:08 2015 +0100
@@ -95,3 +95,8 @@
 
 # used by windmill for `test_edit_relation`
 from cubes.folder.schema import Folder
+
+
+class TreeNode(EntityType):
+    name = String(required=True)
+    parent = SubjectRelation('TreeNode', cardinality='?*')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/unittest_views_treeview.py	Thu Jan 29 16:40:08 2015 +0100
@@ -0,0 +1,51 @@
+# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
+from logilab.common.testlib import unittest_main
+from logilab.mtconverter import html_unescape
+
+from cubicweb.devtools.htmlparser import XMLValidator
+from cubicweb.devtools.testlib import CubicWebTC
+
+
+class TreeViewTC(CubicWebTC):
+
+    def test_treeview(self):
+        with self.admin_access.repo_cnx() as cnx:
+            ce = cnx.create_entity
+            root = ce('TreeNode', name=u'root')
+            node = ce('TreeNode', name=u'node1', parent=root)
+            ce('TreeNode', name=u'leaf1a', parent=node)
+            ce('TreeNode', name=u'leaf1b', parent=node)
+            node = ce('TreeNode', name=u'node2', parent=root)
+            ce('TreeNode', name=u'leaf2a', parent=node)
+            ce('TreeNode', name=u'leaf2b', parent=node)
+            root_eid = root.eid
+            cnx.commit()
+
+        with self.admin_access.web_request() as req:
+            root = req.entity_from_eid(root_eid)
+            valid = self.content_type_validators.get('text/html', XMLValidator)()
+            page = valid.parse_string(root.view('tree', klass='oh-my-class'))
+            uls = page.find_tag('ul', gettext=False)
+            for _, attrib in uls:
+                self.assertEqual(attrib['class'], 'oh-my-class')
+
+
+if __name__ == '__main__':
+    unittest_main()
--- a/web/views/baseviews.py	Wed Jan 28 11:59:06 2015 +0100
+++ b/web/views/baseviews.py	Thu Jan 29 16:40:08 2015 +0100
@@ -315,12 +315,12 @@
         else:
             self.w(u'<ul%s class="%s">\n' % (listid, klass or 'section'))
         for i in xrange(self.cw_rset.rowcount):
-            self.cell_call(row=i, col=0, vid=subvid, **kwargs)
+            self.cell_call(row=i, col=0, vid=subvid, klass=klass, **kwargs)
         self.w(u'</ul>\n')
         if title:
             self.w(u'</div>\n')
 
-    def cell_call(self, row, col=0, vid=None, **kwargs):
+    def cell_call(self, row, col=0, vid=None, klass=None, **kwargs):
         self.w(u'<li>')
         self.wview(self.item_vid, self.cw_rset, row=row, col=col, vid=vid, **kwargs)
         self.w(u'</li>\n')
--- a/web/views/treeview.py	Wed Jan 28 11:59:06 2015 +0100
+++ b/web/views/treeview.py	Thu Jan 29 16:40:08 2015 +0100
@@ -61,7 +61,7 @@
             done = set()
         super(BaseTreeView, self).call(done=done, **kwargs)
 
-    def cell_call(self, row, col=0, vid=None, done=None, maxlevel=None, **kwargs):
+    def cell_call(self, row, col=0, vid=None, done=None, maxlevel=None, klass=None, **kwargs):
         assert maxlevel is None or maxlevel > 0
         done, entity = _done_init(done, self, row, col)
         if done is None:
@@ -77,7 +77,7 @@
                 return
         relatedrset = entity.cw_adapt_to('ITree').children(entities=False)
         self.wview(self.__regid__, relatedrset, 'null', done=done,
-                   maxlevel=maxlevel, **kwargs)
+                   maxlevel=maxlevel, klass=klass, **kwargs)
         self.close_item(entity)
 
     def open_item(self, entity):