add next and previous published siblings to Page model

This commit is contained in:
Robert Clark 2014-06-06 14:40:05 -04:00
parent 9b5d4f26f7
commit de678ac8ba
3 changed files with 62 additions and 1 deletions

View file

@ -39,7 +39,7 @@
"model": "wagtailcore.page",
"fields": {
"title": "Events",
"numchild": 3,
"numchild": 4,
"show_in_menus": true,
"live": true,
"depth": 3,
@ -186,6 +186,34 @@
}
},
{
"pk": 9,
"model": "wagtailcore.page",
"fields": {
"title": "Ameristralia Day",
"numchild": 0,
"show_in_menus": true,
"live": true,
"depth": 4,
"content_type": ["tests", "eventpage"],
"path": "0001000100010004",
"url_path": "/home/events/final-event/",
"slug": "final-event",
"owner": 3
}
},
{
"pk": 9,
"model": "tests.eventpage",
"fields": {
"date_from": "2015-04-22",
"audience": "public",
"location": "Ameristralia",
"body": "<p>come celebrate the independence of Ameristralia</p>",
"cost": "Free"
}
},
{
"pk": 1,
"model": "tests.formfield",

View file

@ -649,6 +649,21 @@ class Page(MP_Node, ClusterableModel, Indexed):
def get_siblings(self, inclusive=True):
return Page.objects.sibling_of(self, inclusive)
def get_next_published_sibling(self):
next_sibling = self.get_next_sibling()
while next_sibling and not next_sibling.live:
next_sibling = next_sibling.get_next_sibling()
return next_sibling
def get_prev_published_sibling(self):
prev_sibling = self.get_prev_sibling()
while prev_sibling and not prev_sibling.live:
prev_sibling = prev_sibling.get_prev_sibling()
return prev_sibling
def get_navigation_menu_items():
# Get all pages that appear in the navigation menu: ones which have children,

View file

@ -254,3 +254,21 @@ class TestPageQuerySet(TestCase):
# Check that the homepage is in the results
homepage = Page.objects.get(url_path='/home/')
self.assertTrue(pages.filter(id=homepage.id).exists())
def test_published_next(self):
events_index = Page.objects.get(url_path='/home/events/')
current_page = Page.objects.descendant_of(events_index).live().first()
# All pages must be live
while current_page:
self.assertTrue(current_page.live)
current_page = current_page.get_next_published_sibling()
def test_published_prev(self):
events_index = Page.objects.get(url_path='/home/events/')
current_page = Page.objects.descendant_of(events_index).live().last()
# All pages must be live
while current_page:
self.assertTrue(current_page.live)
current_page = current_page.get_prev_published_sibling()