diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 35e6ab7c33..fdb3e3728d 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -18,6 +18,7 @@ Changelog * Added an 'attrs' property to image rendition objects to output src, width, height and alt attributes all in one go * Added 'construct_whitelister_element_rules' hook for customising the HTML whitelist used when saving rich text fields * Added 'in_menu' and 'not_in_menu' methods to PageQuerySet + * Added 'get_next_siblings' and 'get_prev_siblings' to Page * Fix: Animated GIFs are now coalesced before resizing * Fix: Wand backend clones images before modifying them * Fix: Admin breadcrumb now positioned correctly on mobile diff --git a/wagtail/tests/fixtures/test.json b/wagtail/tests/fixtures/test.json index 3df2ec9643..d66861f1bc 100644 --- a/wagtail/tests/fixtures/test.json +++ b/wagtail/tests/fixtures/test.json @@ -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": "
come celebrate the independence of Ameristralia
", + "cost": "Free" + } +}, + { "pk": 1, "model": "tests.formfield", diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index e98abbb640..e414475426 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -651,6 +651,12 @@ class Page(MP_Node, ClusterableModel, Indexed): def get_siblings(self, inclusive=True): return Page.objects.sibling_of(self, inclusive) + def get_next_siblings(self, inclusive=False): + return self.get_siblings(inclusive).filter(path__gte=self.path).order_by('path') + + def get_prev_siblings(self, inclusive=False): + return self.get_siblings(inclusive).filter(path__lte=self.path).order_by('-path') + def get_navigation_menu_items(): # Get all pages that appear in the navigation menu: ones which have children, diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 1bae57952c..24eabd8667 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -224,3 +224,24 @@ class TestMovePage(TestCase): christmas = events_index.get_children().get(slug='christmas') self.assertEqual(christmas.depth, 5) self.assertEqual(christmas.url_path, '/home/about-us/events/christmas/') + + +class TestPrevNextSiblings(TestCase): + fixtures = ['test.json'] + + def test_get_next_siblings(self): + christmas_event = Page.objects.get(url_path='/home/events/christmas/') + self.assertTrue(christmas_event.get_next_siblings().filter(url_path='/home/events/final-event/').exists()) + + def test_get_next_siblings_inclusive(self): + christmas_event = Page.objects.get(url_path='/home/events/christmas/') + + # First element must always be the current page + self.assertEqual(christmas_event.get_next_siblings(inclusive=True).first(), christmas_event) + + def test_get_prev_siblings(self): + final_event = Page.objects.get(url_path='/home/events/final-event/') + self.assertTrue(final_event.get_prev_siblings().filter(url_path='/home/events/christmas/').exists()) + + # First element must always be the current page + self.assertEqual(final_event.get_prev_siblings(inclusive=True).first(), final_event)