mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-21 19:47:20 -06:00
Coerce MAX_PAGE_SIZE to be at least PAGINATE_COUNT
This commit is contained in:
parent
020386f8a1
commit
acecb3a81c
@ -39,10 +39,10 @@ class OptionalLimitOffsetPagination(LimitOffsetPagination):
|
||||
def get_limit(self, request):
|
||||
if self.limit_query_param:
|
||||
MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE
|
||||
if self.limit_query_param not in request.query_params:
|
||||
return min(self.default_limit, MAX_PAGE_SIZE)
|
||||
if MAX_PAGE_SIZE:
|
||||
MAX_PAGE_SIZE = max(MAX_PAGE_SIZE, self.default_limit)
|
||||
try:
|
||||
limit = int(request.query_params.get(self.limit_query_param, 0))
|
||||
limit = int(request.query_params[self.limit_query_param])
|
||||
if limit < 0:
|
||||
raise ValueError()
|
||||
# Enforce maximum page size, if defined
|
||||
|
@ -148,13 +148,14 @@ class APIPaginationTestCase(APITestCase):
|
||||
def test_default_page_size_with_small_max_page_size(self):
|
||||
response = self.client.get(self.url, format='json', **self.header)
|
||||
page_size = get_config().MAX_PAGE_SIZE
|
||||
paginate_count = get_config().PAGINATE_COUNT
|
||||
self.assertLess(page_size, 100, "Default page size not sufficient for data set")
|
||||
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 100)
|
||||
self.assertTrue(response.data['next'].endswith(f'?limit={page_size}&offset={page_size}'))
|
||||
self.assertTrue(response.data['next'].endswith(f'?limit={paginate_count}&offset={paginate_count}'))
|
||||
self.assertIsNone(response.data['previous'])
|
||||
self.assertEqual(len(response.data['results']), page_size)
|
||||
self.assertEqual(len(response.data['results']), paginate_count)
|
||||
|
||||
def test_custom_page_size(self):
|
||||
response = self.client.get(f'{self.url}?limit=10', format='json', **self.header)
|
||||
@ -165,15 +166,15 @@ class APIPaginationTestCase(APITestCase):
|
||||
self.assertIsNone(response.data['previous'])
|
||||
self.assertEqual(len(response.data['results']), 10)
|
||||
|
||||
@override_settings(MAX_PAGE_SIZE=20)
|
||||
@override_settings(MAX_PAGE_SIZE=80)
|
||||
def test_max_page_size(self):
|
||||
response = self.client.get(f'{self.url}?limit=0', format='json', **self.header)
|
||||
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 100)
|
||||
self.assertTrue(response.data['next'].endswith('?limit=20&offset=20'))
|
||||
self.assertTrue(response.data['next'].endswith('?limit=80&offset=80'))
|
||||
self.assertIsNone(response.data['previous'])
|
||||
self.assertEqual(len(response.data['results']), 20)
|
||||
self.assertEqual(len(response.data['results']), 80)
|
||||
|
||||
@override_settings(MAX_PAGE_SIZE=0)
|
||||
def test_max_page_size_disabled(self):
|
||||
|
Loading…
Reference in New Issue
Block a user