Fixed #30722 -- Added default rate-limiting requests to admin's Select2 widget.

This commit is contained in:
Federico Jaramillo Martínez 2019-08-22 09:20:30 -05:00 committed by Mariusz Felisiak
parent 691def10a0
commit 8f6860863e
3 changed files with 15 additions and 0 deletions

View File

@ -410,6 +410,7 @@ class AutocompleteMixin:
attrs.setdefault('class', '')
attrs.update({
'data-ajax--cache': 'true',
'data-ajax--delay': 250,
'data-ajax--type': 'GET',
'data-ajax--url': self.get_url(),
'data-theme': 'admin-autocomplete',

View File

@ -1,4 +1,5 @@
import json
import time
from django.contrib import admin
from django.contrib.admin.tests import AdminSeleniumTestCase
@ -189,6 +190,12 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertEqual(len(results), PAGINATOR_SIZE + 11)
# Limit the results with the search field.
search.send_keys('Who')
# Ajax request is delayed.
self.assertTrue(result_container.is_displayed())
results = result_container.find_elements_by_css_selector('.select2-results__option')
self.assertEqual(len(results), PAGINATOR_SIZE + 12)
# Wait for ajax delay.
time.sleep(0.25)
self.assertTrue(result_container.is_displayed())
results = result_container.find_elements_by_css_selector('.select2-results__option')
self.assertEqual(len(results), 1)
@ -223,6 +230,12 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertEqual(len(results), 31)
# Limit the results with the search field.
search.send_keys('Who')
# Ajax request is delayed.
self.assertTrue(result_container.is_displayed())
results = result_container.find_elements_by_css_selector('.select2-results__option')
self.assertEqual(len(results), 32)
# Wait for ajax delay.
time.sleep(0.25)
self.assertTrue(result_container.is_displayed())
results = result_container.find_elements_by_css_selector('.select2-results__option')
self.assertEqual(len(results), 1)

View File

@ -52,6 +52,7 @@ class AutocompleteMixinTests(TestCase):
self.assertEqual(attrs, {
'class': 'my-class admin-autocomplete',
'data-ajax--cache': 'true',
'data-ajax--delay': 250,
'data-ajax--type': 'GET',
'data-ajax--url': '/admin_widgets/band/autocomplete/',
'data-theme': 'admin-autocomplete',