Explorar o código

Fix query search in filtered image admin listing

- fixes #8029
- Fix Search Images within a Collection and an empty search query param - `admin/images/?collection_id=1&q=1`
Paritosh Kabra %!s(int64=3) %!d(string=hai) anos
pai
achega
63e0181f93

+ 1 - 0
CHANGELOG.txt

@@ -120,6 +120,7 @@ Changelog
  * Fix: Ensure consistent sidebar icon position whether expanded or collapsed (Scott Cranfill)
  * Fix: Avoid redirects import error if the file had lots of columns (Jaap Roes)
  * Fix: Resolve accessibility and styling issues with the expanding status panel (Sage Abdullah)
+ * Fix: Avoid 503 `AttributeError` when an empty search param `q=` is combined with other filters in the Images index view (Paritosh Kabra)
 
 
 3.0.1 (16.06.2022)

+ 1 - 0
CONTRIBUTORS.rst

@@ -614,6 +614,7 @@ Contributors
 * Stefano Silvestri
 * Alexander Rogovskyy
 * Dominik Lech
+* Paritosh Kabra
 
 
 Translators

+ 1 - 0
docs/releases/4.0.md

@@ -150,6 +150,7 @@ In Wagtail 2.16, we introduced support for Windows High Contrast mode (WHCM). Th
  * Ensure consistent sidebar icon position whether expanded or collapsed (Scott Cranfill)
  * Avoid redirects import error if the file had lots of columns (Jaap Roes)
  * Resolve accessibility and styling issues with the expanding status panel (Sage Abdullah)
+ * Avoid 503 `AttributeError` when an empty search param `q=` is combined with other filters in the Images index view (Paritosh Kabra)
 
 
 ## Upgrade considerations

+ 28 - 0
wagtail/images/tests/test_admin_views.py

@@ -44,6 +44,34 @@ class TestImageIndexView(TestCase, WagtailTestUtils):
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.context["query_string"], "Hello")
 
+    def test_collection_query_search(self):
+        root_collection = Collection.get_first_root_node()
+        child_collection = [
+            root_collection.add_child(name="Baker Collection"),
+            root_collection.add_child(name="Other Collection"),
+        ]
+        title_list = ["Baker", "Other"]
+        answer_list = []
+        for i in range(10):
+            self.image = Image.objects.create(
+                title=f"{title_list[i%2]} {i}",
+                file=get_test_image_file(size=(1, 1)),
+                collection=child_collection[i % 2],
+            )
+            if i % 2 == 0:
+                answer_list.append(self.image)
+        response = self.get({"q": "Baker", "collection_id": child_collection[0].id})
+        status_code = response.status_code
+        query_string = response.context["query_string"]
+        response_list = response.context["images"].object_list
+        response_body = response.content.decode("utf-8")
+
+        self.assertEqual(status_code, 200)
+        self.assertEqual(query_string, "Baker")
+        self.assertCountEqual(answer_list, response_list)
+        for i in range(0, 10, 2):
+            self.assertIn("Baker %i" % i, response_body)
+
     def test_pagination(self):
         pages = ["0", "1", "-1", "9999", "Not a page"]
         for page in pages:

+ 11 - 11
wagtail/images/views/images.py

@@ -87,17 +87,6 @@ class BaseListingView(TemplateView):
             .prefetch_renditions("max-165x165")
         )
 
-        # Search
-        query_string = None
-        if "q" in self.request.GET:
-            self.form = SearchForm(self.request.GET, placeholder=_("Search images"))
-            if self.form.is_valid():
-                query_string = self.form.cleaned_data["q"]
-
-                images = images.search(query_string)
-        else:
-            self.form = SearchForm(placeholder=_("Search images"))
-
         # Filter by collection
         self.current_collection = None
         collection_id = self.request.GET.get("collection_id")
@@ -116,6 +105,17 @@ class BaseListingView(TemplateView):
             except (AttributeError):
                 self.current_tag = None
 
+        # Search
+        query_string = None
+        if "q" in self.request.GET:
+            self.form = SearchForm(self.request.GET, placeholder=_("Search images"))
+            if self.form.is_valid():
+                query_string = self.form.cleaned_data["q"]
+
+                images = images.search(query_string)
+        else:
+            self.form = SearchForm(placeholder=_("Search images"))
+
         entries_per_page = self.get_num_entries_per_page()
         paginator = Paginator(images, per_page=entries_per_page)
         images = paginator.get_page(self.request.GET.get("p"))