Restore the ability to have content type facets when searching pages with an empty query (#11243)

pull/11245/head
Sage Abdullah 2023-11-16 10:12:20 +00:00 zatwierdzone przez Matt Westcott
rodzic c9b3d9e15b
commit 3af26aa30e
4 zmienionych plików z 91 dodań i 14 usunięć

Wyświetl plik

@ -52,6 +52,7 @@ Changelog
* Fix: Fix performance regression on reports from calling `decorate_paginated_queryset` before pagination / filtering (Alex Tomkins)
* Fix: Make searching on specific fields work correctly on Elasticsearch when boost is in use (Matt Westcott)
* Fix: Prevent snippet permission post-migrate hook from failing on multiple database configurations (Joe Tsoi)
* Fix: Reinstate ability to filter on page type when searching on an empty query (Sage Abdullah)
* Docs: Fix code example for `{% picture ... as ... %}` template tag (Rezyapkin)

Wyświetl plik

@ -25,6 +25,7 @@ depth: 1
* Fix performance regression on reports from calling `decorate_paginated_queryset` before pagination / filtering (Alex Tomkins)
* Make searching on specific fields work correctly on Elasticsearch when boost is in use (Matt Westcott)
* Prevent snippet permission post-migrate hook from failing on multiple database configurations (Joe Tsoi)
* Reinstate ability to filter on page type when searching on an empty query (Sage Abdullah)
### Documentation

Wyświetl plik

@ -6,7 +6,7 @@ from django.test import TransactionTestCase
from django.urls import reverse
from wagtail.models import Page
from wagtail.test.testapp.models import SimplePage, SingleEventPage
from wagtail.test.testapp.models import EventIndex, SimplePage, SingleEventPage
from wagtail.test.utils import WagtailTestUtils
from wagtail.test.utils.timestamps import local_datetime
@ -212,3 +212,79 @@ class TestPageSearch(WagtailTestUtils, TransactionTestCase):
# Incorrect content_type
response = self.get({"content_type": "demosite.standardpage.error"})
self.assertEqual(response.status_code, 404)
def test_empty_search_renders_content_type_facets(self):
root_page = Page.objects.get(id=2)
event_index = EventIndex(
title="ALL THE EVENTS",
intro="It's just a nod to the canon",
)
root_page.add_child(instance=event_index)
params = [{"q": ""}, {}]
url = reverse("wagtailadmin_pages:search")
for param in params:
with self.subTest(param=param):
response = self.get(param)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailadmin/pages/search.html")
self.assertEqual(response.context["query_string"], "")
self.assertContains(response, "Page types")
self.assertContains(response, "All (3)")
# The test fixture contains the root page and the welcome page
# with the base page type
self.assertContains(response, "Page (2)")
self.assertContains(response, "ALL THE EVENTS")
self.assertContains(response, "Event index (1)")
self.assertContains(
response,
f"{url}?q=&content_type=tests.eventindex",
)
def test_empty_search_with_content_type_filter(self):
root_page = Page.objects.get(id=2)
event_index = EventIndex(
title="ALL THE EVENTS",
intro="It's just a nod to the canon",
)
new_event = SingleEventPage(
title="Lunar event",
location="the moon",
audience="public",
cost="free",
date_from="2001-01-01",
latest_revision_created_at=local_datetime(2016, 1, 1),
)
root_page.add_child(instance=event_index)
root_page.add_child(instance=new_event)
params = [
{"q": "", "content_type": "tests.singleeventpage"},
{"content_type": "tests.singleeventpage"},
]
url = reverse("wagtailadmin_pages:search")
for param in params:
with self.subTest(param=param):
response = self.get(param)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailadmin/pages/search.html")
self.assertEqual(response.context["query_string"], "")
self.assertContains(response, "Page types")
self.assertContains(response, "All (4)")
# The test fixture contains the root page and the welcome page
# with the base page type
self.assertContains(response, "Page (2)")
self.assertContains(response, "Single event page (1)")
self.assertContains(response, "Lunar event")
self.assertNotContains(response, "ALL THE EVENTS")
self.assertContains(response, "Event index (1)")
self.assertContains(
response,
f"{url}?q=&content_type=tests.eventindex",
)

Wyświetl plik

@ -143,20 +143,19 @@ class BaseSearchView(PermissionCheckedMixin, BaseListingView):
if self.selected_content_type:
pages = pages.filter(content_type=self.selected_content_type)
if self.q:
# Parse query and filter
pages, self.all_pages = page_filter_search(
self.q, pages, self.all_pages, self.ordering
)
# Parse query and filter
pages, self.all_pages = page_filter_search(
self.q, pages, self.all_pages, self.ordering
)
# Facets
if pages.supports_facet:
self.content_types = [
(ContentType.objects.get(id=content_type_id), count)
for content_type_id, count in self.all_pages.facet(
"content_type_id"
).items()
]
# Facets
if pages.supports_facet:
self.content_types = [
(ContentType.objects.get(id=content_type_id), count)
for content_type_id, count in self.all_pages.facet(
"content_type_id"
).items()
]
return pages