Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #18150 - Get pagination limit with default 0 #18151

Merged
merged 9 commits into from
Dec 12, 2024
4 changes: 3 additions & 1 deletion netbox/netbox/api/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ def paginate_queryset(self, queryset, request, view=None):

def get_limit(self, request):
if self.limit_query_param:
MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE
if MAX_PAGE_SIZE:
MAX_PAGE_SIZE = max(MAX_PAGE_SIZE, self.default_limit)
try:
limit = int(request.query_params[self.limit_query_param])
if limit < 0:
raise ValueError()
# Enforce maximum page size, if defined
MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE
if MAX_PAGE_SIZE:
return MAX_PAGE_SIZE if limit == 0 else min(limit, MAX_PAGE_SIZE)
return limit
Expand Down
19 changes: 16 additions & 3 deletions netbox/utilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ def test_default_page_size(self):
self.assertIsNone(response.data['previous'])
self.assertEqual(len(response.data['results']), page_size)

@override_settings(MAX_PAGE_SIZE=30)
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={paginate_count}&offset={paginate_count}'))
self.assertIsNone(response.data['previous'])
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)

Expand All @@ -153,15 +166,15 @@ def test_custom_page_size(self):
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):
Expand Down
Loading