Commits

Steve Losh committed 308c0c0 Merge

Merge pull request #9 from joshkersey/master

Previous/Next Sibling methods for Page

  • Participants
  • Parent commits 1107e04, d2044ae

Comments (0)

Files changed (2)

File stoat/models.py

     def nav_children(self):
         """Return a list of child Page objects."""
         return list(self.get_children().filter(show_in_nav=True))
+        
+    def nav_next_sibling(self):
+        """ Return the next sibling object, or None if it was the rightmost sibling."""
+        siblings = self.nav_siblings()
+        next_sibling = None
+        for i, sibling in enumerate(siblings):
+            if sibling == self and i < len(siblings) - 1:
+                next_sibling = siblings[i+1]
+        return next_sibling   
+        
+    def nav_prev_sibling(self):
+        """ Return the previous sibling object, or None if it was the leftmost sibling."""
+        siblings = self.nav_siblings()
+        prev_sibling = None
+        for i, sibling in enumerate(siblings):
+            if sibling == self and i > 0:
+                prev_sibling = siblings[i-1]
+        return prev_sibling 
 
     def nav_siblings_and_children(self):
         """Return a nested list of sibling/children Page objects (including this page)."""

File stoat/tests/test_navigation.py

         real_sibs = set(['/top-one/one-sub/'])
         test_sibs = set([p.url for p in page.nav_siblings()])
         self.assertTrue(real_sibs == test_sibs)
+        
+    def test_nav_next_sibling(self):
+        page = get(Page, url='/')
+        real_sibs = set(['/', '/top-one/', '/top-two/'])
+        real_next_sib = '/top-one/'
+        self.assertTrue(real_next_sib == page.nav_next_sibling().url)
+        
+        page = get(Page, url='/top-two/')
+        real_next_sib = None
+        self.assertTrue(real_next_sib == page.nav_next_sibling())
+        
+    def test_nav_prev_sibling(self):
+        page = get(Page, url='/')
+        real_sibs = set(['/', '/top-one/', '/top-two/'])
+        real_prev_sib = None
+        self.assertTrue(real_prev_sib == page.nav_prev_sibling())
+        
+        page = get(Page, url='/top-two/')
+        real_sibs = set(['/', '/top-one/', '/top-two/'])
+        real_prev_sib = '/top-one/'
+        self.assertTrue(real_prev_sib == page.nav_prev_sibling().url)
 
     def test_nav_children(self):
         page = get(Page, url='/')
         real_kids = set(['/top-one/one-sub/one-sub-sub/'])
         test_kids = set([p.url for p in page.nav_children()])
         self.assertTrue(real_kids == test_kids)
+        
     def test_nav_siblings_and_children(self):
         def test_siblings(saclist, urllist):
             urls = set([sib.url for sib, children in saclist])