mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Implement special "root" page value for child_of/decendant_of filters
This adds the ability to specify "root" to the child_of and decendant_of filters, which represents the homepage of the current site.
This commit is contained in:
parent
28a7bde212
commit
0a46c9680f
@ -114,6 +114,9 @@ class ChildOfFilter(BaseFilterBackend):
|
||||
Implements the ?child_of filter used to filter the results to only contain
|
||||
pages that are direct children of the specified page.
|
||||
"""
|
||||
def get_root_page(self, request):
|
||||
return Page.get_first_root_node()
|
||||
|
||||
def get_page_by_id(self, request, page_id):
|
||||
return Page.objects.get(id=page_id)
|
||||
|
||||
@ -125,7 +128,10 @@ class ChildOfFilter(BaseFilterBackend):
|
||||
|
||||
parent_page = self.get_page_by_id(request, parent_page_id)
|
||||
except (ValueError, AssertionError):
|
||||
raise BadRequestError("child_of must be a positive integer")
|
||||
if request.GET['child_of'] == 'root':
|
||||
parent_page = self.get_root_page(request)
|
||||
else:
|
||||
raise BadRequestError("child_of must be a positive integer")
|
||||
except Page.DoesNotExist:
|
||||
raise BadRequestError("parent page doesn't exist")
|
||||
|
||||
@ -140,6 +146,9 @@ class RestrictedChildOfFilter(ChildOfFilter):
|
||||
A restricted version of ChildOfFilter that only allows pages in the current
|
||||
site to be specified.
|
||||
"""
|
||||
def get_root_page(self, request):
|
||||
return request.site.root_page
|
||||
|
||||
def get_page_by_id(self, request, page_id):
|
||||
site_pages = pages_for_site(request.site)
|
||||
return site_pages.get(id=page_id)
|
||||
@ -150,6 +159,9 @@ class DescendantOfFilter(BaseFilterBackend):
|
||||
Implements the ?decendant_of filter which limits the set of pages to a
|
||||
particular branch of the page tree.
|
||||
"""
|
||||
def get_root_page(self, request):
|
||||
return Page.get_first_root_node()
|
||||
|
||||
def get_page_by_id(self, request, page_id):
|
||||
return Page.objects.get(id=page_id)
|
||||
|
||||
@ -163,7 +175,10 @@ class DescendantOfFilter(BaseFilterBackend):
|
||||
|
||||
parent_page = self.get_page_by_id(request, parent_page_id)
|
||||
except (ValueError, AssertionError):
|
||||
raise BadRequestError("descendant_of must be a positive integer")
|
||||
if request.GET['descendant_of'] == 'root':
|
||||
parent_page = self.get_root_page(request)
|
||||
else:
|
||||
raise BadRequestError("descendant_of must be a positive integer")
|
||||
except Page.DoesNotExist:
|
||||
raise BadRequestError("ancestor page doesn't exist")
|
||||
|
||||
@ -177,6 +192,9 @@ class RestrictedDescendantOfFilter(DescendantOfFilter):
|
||||
A restricted version of DecendantOfFilter that only allows pages in the current
|
||||
site to be specified.
|
||||
"""
|
||||
def get_root_page(self, request):
|
||||
return request.site.root_page
|
||||
|
||||
def get_page_by_id(self, request, page_id):
|
||||
site_pages = pages_for_site(request.site)
|
||||
return site_pages.get(id=page_id)
|
||||
|
@ -286,6 +286,14 @@ class TestPageListing(TestCase):
|
||||
page_id_list = self.get_page_id_list(content)
|
||||
self.assertEqual(page_id_list, [16, 18, 19])
|
||||
|
||||
def test_child_of_root(self):
|
||||
# "root" gets children of the homepage of the current site
|
||||
response = self.get_response(child_of='root')
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
|
||||
page_id_list = self.get_page_id_list(content)
|
||||
self.assertEqual(page_id_list, [4, 5, 6, 20, 12])
|
||||
|
||||
def test_child_of_with_type(self):
|
||||
response = self.get_response(type='demosite.EventPage', child_of=5)
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
@ -325,6 +333,15 @@ class TestPageListing(TestCase):
|
||||
page_id_list = self.get_page_id_list(content)
|
||||
self.assertEqual(page_id_list, [10, 15, 17, 21, 22, 23])
|
||||
|
||||
def test_descendant_of_root(self):
|
||||
# "root" gets decendants of the homepage of the current site
|
||||
# Basically returns every page except the homepage
|
||||
response = self.get_response(descendant_of='root')
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
|
||||
page_id_list = self.get_page_id_list(content)
|
||||
self.assertEqual(page_id_list, [4, 8, 9, 5, 16, 18, 19, 6, 10, 15, 17, 21, 22, 23, 20, 13, 14, 12])
|
||||
|
||||
def test_descendant_of_with_type(self):
|
||||
response = self.get_response(type='tests.EventPage', descendant_of=6)
|
||||
content = json.loads(response.content.decode('UTF-8'))
|
||||
|
Loading…
Reference in New Issue
Block a user