kopia lustrzana https://github.com/wagtail/wagtail
Implement search on explorer view
rodzic
f5af3d5d82
commit
de59c75d10
|
@ -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"]
|
||||
|
|
|
@ -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):
|
||||
|
|
Ładowanie…
Reference in New Issue