0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-24 01:57:32 +01:00

Limit autocomplete results view to 10

Prevent performance issues when there are many tags available.
This commit is contained in:
Aayushman Singh 2024-11-11 11:50:32 +05:30 committed by Sage Abdullah
parent 5212061485
commit ce0601b77c
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
2 changed files with 19 additions and 1 deletions

View File

@ -374,6 +374,24 @@ class TestTagsAutocomplete(WagtailTestUtils, TestCase):
self.assertEqual(data, [])
def test_tags_autocomplete_limit(self):
tags = [Tag(name=f"Tag {i}", slug=f"tag-{i}") for i in range(15)]
Tag.objects.bulk_create(tags)
# Send a request to the autocomplete endpoint with a broad search term
response = self.client.get(
reverse("wagtailadmin_tag_autocomplete"), {"term": "Tag"}
)
# Confirm the response is successful
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
data = json.loads(response.content.decode("utf-8"))
# The results should be limited to avoid performance issues (#12415)
self.assertEqual(len(data), 10)
class TestMenuItem(WagtailTestUtils, TestCase):
def setUp(self):

View File

@ -19,7 +19,7 @@ def autocomplete(request, app_name=None, model_name=None):
term = request.GET.get("term", None)
if term:
tags = tag_model.objects.filter(name__istartswith=term).order_by("name")
tags = tag_model.objects.filter(name__istartswith=term).order_by("name")[:10]
else:
tags = tag_model.objects.none()