changeset 993:6dd0b0755b6a

disconnected levels now shown in index, fixes #75 todo: treat non-existent item listed in index.
author "Luis Henrique Fagundes <lhfagundes@gmail.com>"
date Fri, 28 Oct 2011 00:16:20 -0200
parents 854211821d1b
children 1d17af578b3b 76108d59b0f7
files MoinMoin/items/__init__.py MoinMoin/items/_tests/test_Item.py
diffstat 2 files changed, 58 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Wed Oct 26 23:20:57 2011 -0200
+++ b/MoinMoin/items/__init__.py	Fri Oct 28 00:16:20 2011 -0200
@@ -525,13 +525,34 @@
                  for rev in revs]
         return items
 
+    def _connect_levels(self, index):
+        new_index = []
+        last = self.name
+        for item in index:
+            name = item[0]
+
+            while not name.startswith(last):
+                last = last.rpartition('/')[0]
+
+            missing_layers = name.split('/')[last.count('/')+1:-1]
+
+            for layer in missing_layers:
+                last = '/'.join([last, layer])
+                new_index.append((last, last[len(self.name)+1:], u'application/x-nonexistent'))
+
+            last = item[0]
+            new_index.append(item)
+
+        return new_index
+
     def flat_index(self, startswith=None, selected_groups=None):
         """
-        creates an top level index of sub items of this item
+        creates a top level index of sub items of this item
         if startswith is set, filtering is done on the basis of starting letter of item name
         if selected_groups is set, items whose contentype belonging to the selected contenttype_groups, are filtered.
         """
         index = self.get_index()
+        index = self._connect_levels(index)
 
         all_ctypes = [[ctype for ctype, clabel in contenttypes]
                       for gname, contenttypes in CONTENTTYPE_GROUPS]
--- a/MoinMoin/items/_tests/test_Item.py	Wed Oct 26 23:20:57 2011 -0200
+++ b/MoinMoin/items/_tests/test_Item.py	Fri Oct 28 00:16:20 2011 -0200
@@ -94,6 +94,7 @@
                         ]
         flat_index = baseitem.flat_index()
         assert flat_index == [(u'Foo/ab', u'ab', u'text/plain;charset=utf-8'),
+                              (u'Foo/cd', u'cd', u'application/x-nonexistent'),
                               (u'Foo/gh', u'gh', u'text/plain;charset=utf-8'),
                               (u'Foo/ij', u'ij', u'text/plain;charset=utf-8'),
                               (u'Foo/mn', u'mn', u'image/jpeg'),
@@ -101,17 +102,52 @@
         # check index when startswith param is passed
         flat_index = baseitem.flat_index(startswith=u'a')
         assert flat_index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8')]
+
+        #check that virtual container item is returned with startswith
+        flat_index = baseitem.flat_index(startswith=u'c')
+        assert flat_index == [(u'Foo/cd', u'cd', u'application/x-nonexistent')]
+
         # check index when contenttype_groups is passed
         ctgroups = ["image items"]
         flat_index = baseitem.flat_index(selected_groups=ctgroups)
         assert flat_index == [(u'Foo/mn', u'mn', 'image/jpeg')]
+
+        # If we ask for text/plain type, should Foo/cd be returned?
+
+        # check detailed index
         detailed_index = baseitem.get_detailed_index(baseitem.flat_index())
         assert detailed_index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8', False),
+                                  (u'Foo/cd', u'cd', 'application/x-nonexistent', True),
                                   (u'Foo/gh', u'gh', 'text/plain;charset=utf-8', False),
                                   (u'Foo/ij', u'ij', 'text/plain;charset=utf-8', True),
                                   (u'Foo/mn', u'mn', 'image/jpeg', False),
                                   ]
 
+    def testIndexOnDisconnectedLevels(self):
+        # create a toplevel and some sub-items
+        basename = u'Bar'
+        for name in ['', '/ab', '/ab/cd/ef/gh', '/ij/kl/mn/op', '/ij/kl/rs']:
+            item = Item.create(basename + name)
+            item._save({CONTENTTYPE: u'text/plain;charset=utf-8'}, "foo")
+
+        baseitem = Item.create(basename)
+        index = baseitem.get_index()
+        index = baseitem._connect_levels(index)
+
+        assert index == [(u'Bar/ab', u'ab', u'text/plain;charset=utf-8'),
+                         (u'Bar/ab/cd', u'ab/cd', u'application/x-nonexistent'),
+                         (u'Bar/ab/cd/ef', u'ab/cd/ef', u'application/x-nonexistent'),
+                         (u'Bar/ab/cd/ef/gh', u'ab/cd/ef/gh', u'text/plain;charset=utf-8'),
+                         (u'Bar/ij', u'ij', u'application/x-nonexistent'),
+                         (u'Bar/ij/kl', u'ij/kl', u'application/x-nonexistent'),
+                         (u'Bar/ij/kl/mn', u'ij/kl/mn', u'application/x-nonexistent'),
+                         (u'Bar/ij/kl/mn/op', u'ij/kl/mn/op', u'text/plain;charset=utf-8'),
+                         (u'Bar/ij/kl/rs', u'ij/kl/rs', u'text/plain;charset=utf-8')]
+
+        flat_index = baseitem.flat_index()
+        assert flat_index == [(u'Bar/ab', u'ab', u'text/plain;charset=utf-8'),
+                              (u'Bar/ij', u'ij', u'application/x-nonexistent'),
+                             ]
 
     def test_meta_filter(self):
         name = u'Test_item'