Implement search on explorer view

pull/11032/head
Matt Westcott 2023-10-11 00:09:03 +01:00
rodzic f5af3d5d82
commit de59c75d10
2 zmienionych plików z 47 dodań i 1 usunięć

Wyświetl plik

@ -391,6 +391,43 @@ class TestPageExplorer(WagtailTestUtils, TestCase):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailadmin/pages/index.html")
def test_search(self):
response = self.client.get(
reverse("wagtailadmin_explore", args=(self.root_page.id,)),
{"q": "old"},
)
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.old_page.id])
def test_search_results(self):
response = self.client.get(
reverse("wagtailadmin_explore_results", args=(self.root_page.id,)),
{"q": "old"},
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailadmin/pages/index_results.html")
page_ids = [page.id for page in response.context["pages"]]
self.assertEqual(page_ids, [self.old_page.id])
def test_search_searches_descendants(self):
response = self.client.get(reverse("wagtailadmin_explore_root"), {"q": "old"})
self.assertEqual(response.status_code, 200)
page_ids = [page.id for page in response.context["pages"]]
self.assertEqual(page_ids, [self.old_page.id])
# search results should not include pages outside parent_page's descendants
response = self.client.get(
reverse("wagtailadmin_explore", args=(self.new_page.id,)),
{"q": "old"},
)
self.assertEqual(response.status_code, 200)
page_ids = [page.id for page in response.context["pages"]]
self.assertEqual(page_ids, [])
class TestBreadcrumb(WagtailTestUtils, TestCase):
fixtures = ["test.json"]

Wyświetl plik

@ -134,7 +134,13 @@ class BaseIndexView(PermissionCheckedMixin, BaseListingView):
return ordering
def get_queryset(self):
pages = self.parent_page.get_children().prefetch_related(
is_searching = bool(self.query_string)
if is_searching:
pages = self.parent_page.get_descendants()
else:
pages = self.parent_page.get_children()
pages = pages.prefetch_related(
"content_type", "sites_rooted_here"
) & self.permission_policy.explorable_instances(self.request.user)
@ -173,6 +179,9 @@ class BaseIndexView(PermissionCheckedMixin, BaseListingView):
pages = pages.annotate_site_root_state().annotate_approved_schedule()
if is_searching:
pages = pages.autocomplete(self.query_string)
return pages
def get_paginate_by(self, queryset):