[ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 10 Jun 2010 16:28:06 +0200
changeset 5718 8d246203730a
parent 5717 3c281b6f16c6
child 5719 8450389f2073
[ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
entities/adapters.py
mixins.py
web/views/startup.py
web/views/treeview.py
--- a/entities/adapters.py	Thu Jun 10 16:28:04 2010 +0200
+++ b/entities/adapters.py	Thu Jun 10 16:28:06 2010 +0200
@@ -179,10 +179,16 @@
     __regid__ = 'ITree'
     __select__ = implements(ITree) # XXX for bw compat, else should be abstract
 
-    tree_relation = None
     child_role = 'subject'
     parent_role = 'object'
 
+    @property
+    def tree_relation(self):
+        warn('[3.9] tree_attribute is deprecated, define tree_relation on a custom '
+             'ITree for %s instead' % (self.entity.__class__),
+             DeprecationWarning)
+        return self.entity.tree_attribute
+
     @implements_adapter_compat('ITree')
     def children_rql(self):
         """returns RQL to get children
--- a/mixins.py	Thu Jun 10 16:28:04 2010 +0200
+++ b/mixins.py	Thu Jun 10 16:28:06 2010 +0200
@@ -194,7 +194,7 @@
     entity = view.cw_rset.get_entity(row, col)
     if entity.eid in done:
         msg = entity._cw._('loop in %(rel)s relation (%(eid)s)') % {
-            'rel': entity.tree_attribute,
+            'rel': entity.cw_adapt_to('ITree').tree_relation,
             'eid': entity.eid
             }
         return None, msg
@@ -216,7 +216,8 @@
             done = set()
         super(TreeViewMixIn, self).call(done=done, **kwargs)
 
-    def cell_call(self, row, col=0, vid=None, done=None, **kwargs):
+    def cell_call(self, row, col=0, vid=None, done=None, maxlevel=None, **kwargs):
+        assert maxlevel is None or maxlevel > 0
         done, entity = _done_init(done, self, row, col)
         if done is None:
             # entity is actually an error message
@@ -224,8 +225,14 @@
             return
         self.open_item(entity)
         entity.view(vid or self.item_vid, w=self.w, **kwargs)
+        if maxlevel is not None:
+            maxlevel -= 1
+            if maxlevel == 0:
+                self.close_item(entity)
+                return
         relatedrset = entity.children(entities=False)
-        self.wview(self.__regid__, relatedrset, 'null', done=done, **kwargs)
+        self.wview(self.__regid__, relatedrset, 'null', done=done,
+                   maxlevel=maxlevel, **kwargs)
         self.close_item(entity)
 
     def open_item(self, entity):
--- a/web/views/startup.py	Thu Jun 10 16:28:04 2010 +0200
+++ b/web/views/startup.py	Thu Jun 10 16:28:06 2010 +0200
@@ -79,7 +79,7 @@
 
     def folders(self):
         self.w(u'<h2>%s</h2>\n' % self._cw._('Browse by category'))
-        self._cw.vreg['views'].select('tree', self._cw).render(w=self.w)
+        self._cw.vreg['views'].select('tree', self._cw).render(w=self.w, maxlevel=1)
 
     def create_links(self):
         self.w(u'<ul class="createLink">')
--- a/web/views/treeview.py	Thu Jun 10 16:28:04 2010 +0200
+++ b/web/views/treeview.py	Thu Jun 10 16:28:06 2010 +0200
@@ -47,7 +47,8 @@
             done = set()
         super(BaseTreeView, self).call(done=done, **kwargs)
 
-    def cell_call(self, row, col=0, vid=None, done=None, **kwargs):
+    def cell_call(self, row, col=0, vid=None, done=None, maxlevel=None, **kwargs):
+        assert maxlevel is None or maxlevel > 0
         done, entity = _done_init(done, self, row, col)
         if done is None:
             # entity is actually an error message
@@ -55,8 +56,14 @@
             return
         self.open_item(entity)
         entity.view(vid or self.item_vid, w=self.w, **kwargs)
+        if maxlevel is not None:
+            maxlevel -= 1
+            if maxlevel == 0:
+                self.close_item(entity)
+                return
         relatedrset = entity.cw_adapt_to('ITree').children(entities=False)
-        self.wview(self.__regid__, relatedrset, 'null', done=done, **kwargs)
+        self.wview(self.__regid__, relatedrset, 'null', done=done,
+                   maxlevel=maxlevel, **kwargs)
         self.close_item(entity)
 
     def open_item(self, entity):