Added max_paginate_by parameter

This commit is contained in:
Alexander Akhmetov 2013-08-26 20:05:36 +04:00 committed by Alexander Akhmetov
parent 2bcad32dcb
commit 316de3a8a3
3 changed files with 55 additions and 2 deletions

View File

@ -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,16 @@ 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:
return min(self.max_paginate_by, paginate_by_param)
else:
return paginate_by_param
return self.paginate_by
return min(self.max_paginate_by, self.paginate_by) or self.paginate_by
def get_serializer_class(self):
"""

View File

@ -68,6 +68,7 @@ DEFAULTS = {
# Pagination
'PAGINATE_BY': None,
'PAGINATE_BY_PARAM': None,
'MAX_PAGINATE_BY': None,
# View configuration
'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',

View File

@ -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):