mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 05:04:31 +03:00
Merge branch 'max_paginate_by' of git://github.com/alexander-akhmetov/django-rest-framework into alexander-akhmetov-max_paginate_by
This commit is contained in:
commit
e1b54f2a2a
|
@ -56,6 +56,7 @@ class GenericAPIView(views.APIView):
|
|||
# Pagination settings
|
||||
paginate_by = api_settings.PAGINATE_BY
|
||||
paginate_by_param = api_settings.PAGINATE_BY_PARAM
|
||||
max_paginate_by = api_settings.MAX_PAGINATE_BY
|
||||
pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
|
||||
page_kwarg = 'page'
|
||||
|
||||
|
@ -207,11 +208,19 @@ class GenericAPIView(views.APIView):
|
|||
if self.paginate_by_param:
|
||||
query_params = self.request.QUERY_PARAMS
|
||||
try:
|
||||
return int(query_params[self.paginate_by_param])
|
||||
paginate_by_param = int(query_params[self.paginate_by_param])
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
else:
|
||||
if self.max_paginate_by is not None:
|
||||
return min(self.max_paginate_by, paginate_by_param)
|
||||
else:
|
||||
return paginate_by_param
|
||||
|
||||
return self.paginate_by
|
||||
if self.max_paginate_by:
|
||||
return min(self.max_paginate_by, self.paginate_by)
|
||||
else:
|
||||
return self.paginate_by
|
||||
|
||||
def get_serializer_class(self):
|
||||
"""
|
||||
|
|
|
@ -67,6 +67,7 @@ DEFAULTS = {
|
|||
# Pagination
|
||||
'PAGINATE_BY': None,
|
||||
'PAGINATE_BY_PARAM': None,
|
||||
'MAX_PAGINATE_BY': None,
|
||||
|
||||
# Authentication
|
||||
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
||||
|
|
|
@ -42,6 +42,16 @@ class PaginateByParamView(generics.ListAPIView):
|
|||
paginate_by_param = 'page_size'
|
||||
|
||||
|
||||
class MaxPaginateByView(generics.ListAPIView):
|
||||
"""
|
||||
View for testing custom max_paginate_by usage
|
||||
"""
|
||||
model = BasicModel
|
||||
paginate_by = 5
|
||||
max_paginate_by = 3
|
||||
paginate_by_param = 'page_size'
|
||||
|
||||
|
||||
class IntegrationTestPagination(TestCase):
|
||||
"""
|
||||
Integration tests for paginated list views.
|
||||
|
@ -313,6 +323,42 @@ class TestCustomPaginateByParam(TestCase):
|
|||
self.assertEqual(response.data['results'], self.data[:5])
|
||||
|
||||
|
||||
class TestMaxPaginateByParam(TestCase):
|
||||
"""
|
||||
Tests for list views with max_paginate_by kwarg
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Create 13 BasicModel instances.
|
||||
"""
|
||||
for i in range(13):
|
||||
BasicModel(text=i).save()
|
||||
self.objects = BasicModel.objects
|
||||
self.data = [
|
||||
{'id': obj.id, 'text': obj.text}
|
||||
for obj in self.objects.all()
|
||||
]
|
||||
self.view = MaxPaginateByView.as_view()
|
||||
|
||||
def test_max_paginate_by(self):
|
||||
"""
|
||||
If max_paginate_by is set and it less than paginate_by, new kwarg should limit requests for review.
|
||||
"""
|
||||
request = factory.get('/?page_size=10')
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.data['count'], 13)
|
||||
self.assertEqual(response.data['results'], self.data[:3])
|
||||
|
||||
def test_max_paginate_by_without_page_size_param(self):
|
||||
"""
|
||||
If max_paginate_by is set, new kwarg should limit requests for review.
|
||||
"""
|
||||
request = factory.get('/')
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.data['results'], self.data[:3])
|
||||
|
||||
|
||||
### Tests for context in pagination serializers
|
||||
|
||||
class CustomField(serializers.Field):
|
||||
|
|
Loading…
Reference in New Issue
Block a user