mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 04:02:52 -06:00
Fixes #7209: Allow unlimited API results when MAX_PAGE_SIZE is disabled
This commit is contained in:
parent
0e8c6ee522
commit
9226302742
@ -11,6 +11,7 @@
|
|||||||
* [#7188](https://github.com/netbox-community/netbox/issues/7188) - Fix issue where select fields with `null_option` did not render or send the null option
|
* [#7188](https://github.com/netbox-community/netbox/issues/7188) - Fix issue where select fields with `null_option` did not render or send the null option
|
||||||
* [#7189](https://github.com/netbox-community/netbox/issues/7189) - Set connection factory for django-redis when Sentinel is in use
|
* [#7189](https://github.com/netbox-community/netbox/issues/7189) - Set connection factory for django-redis when Sentinel is in use
|
||||||
* [#7193](https://github.com/netbox-community/netbox/issues/7193) - Fix prefix (flat) template issue when viewing child prefixes with prefixes available
|
* [#7193](https://github.com/netbox-community/netbox/issues/7193) - Fix prefix (flat) template issue when viewing child prefixes with prefixes available
|
||||||
|
* [#7209](https://github.com/netbox-community/netbox/issues/7209) - Allow unlimited API results when `MAX_PAGE_SIZE` is disabled
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -34,13 +34,22 @@ class OptionalLimitOffsetPagination(LimitOffsetPagination):
|
|||||||
return list(queryset[self.offset:])
|
return list(queryset[self.offset:])
|
||||||
|
|
||||||
def get_limit(self, request):
|
def get_limit(self, request):
|
||||||
limit = super().get_limit(request)
|
if self.limit_query_param:
|
||||||
|
try:
|
||||||
|
limit = int(request.query_params[self.limit_query_param])
|
||||||
|
if limit < 0:
|
||||||
|
raise ValueError()
|
||||||
|
# Enforce maximum page size, if defined
|
||||||
|
if settings.MAX_PAGE_SIZE:
|
||||||
|
if limit == 0:
|
||||||
|
return settings.MAX_PAGE_SIZE
|
||||||
|
else:
|
||||||
|
return min(limit, settings.MAX_PAGE_SIZE)
|
||||||
|
return limit
|
||||||
|
except (KeyError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
# Enforce maximum page size
|
return self.default_limit
|
||||||
if settings.MAX_PAGE_SIZE:
|
|
||||||
limit = min(limit, settings.MAX_PAGE_SIZE)
|
|
||||||
|
|
||||||
return limit
|
|
||||||
|
|
||||||
def get_next_link(self):
|
def get_next_link(self):
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase, override_settings
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
@ -122,6 +123,54 @@ class WritableNestedSerializerTest(APITestCase):
|
|||||||
self.assertEqual(VLAN.objects.count(), 0)
|
self.assertEqual(VLAN.objects.count(), 0)
|
||||||
|
|
||||||
|
|
||||||
|
class APIPaginationTestCase(APITestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
cls.url = reverse('dcim-api:site-list')
|
||||||
|
|
||||||
|
# Create a large number of Sites for testing
|
||||||
|
Site.objects.bulk_create([
|
||||||
|
Site(name=f'Site {i}', slug=f'site-{i}') for i in range(1, 101)
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_default_page_size(self):
|
||||||
|
response = self.client.get(self.url, format='json', **self.header)
|
||||||
|
page_size = settings.PAGINATE_COUNT
|
||||||
|
self.assertLess(page_size, 100, "Default page size not sufficient for data set")
|
||||||
|
|
||||||
|
self.assertEqual(response.data['count'], 100)
|
||||||
|
self.assertTrue(response.data['next'].endswith(f'?limit={page_size}&offset={page_size}'))
|
||||||
|
self.assertIsNone(response.data['previous'])
|
||||||
|
self.assertEqual(len(response.data['results']), page_size)
|
||||||
|
|
||||||
|
def test_custom_page_size(self):
|
||||||
|
response = self.client.get(f'{self.url}?limit=10', format='json', **self.header)
|
||||||
|
|
||||||
|
self.assertEqual(response.data['count'], 100)
|
||||||
|
self.assertTrue(response.data['next'].endswith(f'?limit=10&offset=10'))
|
||||||
|
self.assertIsNone(response.data['previous'])
|
||||||
|
self.assertEqual(len(response.data['results']), 10)
|
||||||
|
|
||||||
|
@override_settings(MAX_PAGE_SIZE=20)
|
||||||
|
def test_max_page_size(self):
|
||||||
|
response = self.client.get(f'{self.url}?limit=0', format='json', **self.header)
|
||||||
|
|
||||||
|
self.assertEqual(response.data['count'], 100)
|
||||||
|
self.assertTrue(response.data['next'].endswith(f'?limit=20&offset=20'))
|
||||||
|
self.assertIsNone(response.data['previous'])
|
||||||
|
self.assertEqual(len(response.data['results']), 20)
|
||||||
|
|
||||||
|
@override_settings(MAX_PAGE_SIZE=0)
|
||||||
|
def test_max_page_size_disabled(self):
|
||||||
|
response = self.client.get(f'{self.url}?limit=0', format='json', **self.header)
|
||||||
|
|
||||||
|
self.assertEqual(response.data['count'], 100)
|
||||||
|
self.assertIsNone(response.data['next'])
|
||||||
|
self.assertIsNone(response.data['previous'])
|
||||||
|
self.assertEqual(len(response.data['results']), 100)
|
||||||
|
|
||||||
|
|
||||||
class APIDocsTestCase(TestCase):
|
class APIDocsTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user