Implement construct_explorer_page_queryset hook

pull/2219/merge
Matt Westcott 2016-02-09 18:30:25 +00:00 zatwierdzone przez Karl Hobley
rodzic d27df01815
commit 074911d504
3 zmienionych plików z 29 dodań i 1 usunięć

Wyświetl plik

@ -65,3 +65,13 @@ def register_custom_search_area():
classnames='icon icon-custom',
attrs={'is-custom': 'true'},
order=10000)
@hooks.register('construct_explorer_page_queryset')
def polite_pages_only(parent_page, pages, request):
# if the URL parameter polite_pages_only is set,
# only return pages with a slug that starts with 'hello'
if request.GET.get('polite_pages_only'):
pages = pages.filter(slug__startswith='hello')
return pages

Wyświetl plik

@ -146,6 +146,19 @@ class TestPageExplorer(TestCase, WagtailTestUtils):
# Pages must not be paginated
self.assertNotIsInstance(response.context['pages'], paginator.Page)
def test_construct_explorer_page_queryset_hook(self):
# testapp implements a construct_explorer_page_queryset hook
# that only returns pages with a slug starting with 'hello'
# when the 'polite_pages_only' URL parameter is set
response = self.client.get(
reverse('wagtailadmin_explore', args=(self.root_page.id, )),
{'polite_pages_only': 'yes_please'}
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailadmin/pages/index.html')
page_ids = [page.id for page in response.context['pages']]
self.assertEqual(page_ids, [self.child_page.id])
def make_pages(self):
for i in range(150):
self.root_page.add_child(instance=SimplePage(

Wyświetl plik

@ -68,10 +68,15 @@ def index(request, parent_page_id=None):
else:
pages = pages.order_by(ordering)
# Pagination
# Don't paginate if sorting by page order - all pages must be shown to
# allow drag-and-drop reordering
do_paginate = ordering != 'ord'
# allow hooks to modify the queryset
for hook in hooks.get_hooks('construct_explorer_page_queryset'):
pages = hook(parent_page, pages, request)
# Pagination
if do_paginate:
paginator, pages = paginate(request, pages, per_page=50)