mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 01:47:59 +03:00 
			
		
		
		
	Added max_paginate_by parameter
This commit is contained in:
		
							parent
							
								
									2bcad32dcb
								
							
						
					
					
						commit
						316de3a8a3
					
				| 
						 | 
					@ -56,6 +56,7 @@ class GenericAPIView(views.APIView):
 | 
				
			||||||
    # Pagination settings
 | 
					    # Pagination settings
 | 
				
			||||||
    paginate_by = api_settings.PAGINATE_BY
 | 
					    paginate_by = api_settings.PAGINATE_BY
 | 
				
			||||||
    paginate_by_param = api_settings.PAGINATE_BY_PARAM
 | 
					    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
 | 
					    pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
 | 
				
			||||||
    page_kwarg = 'page'
 | 
					    page_kwarg = 'page'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -207,11 +208,16 @@ class GenericAPIView(views.APIView):
 | 
				
			||||||
        if self.paginate_by_param:
 | 
					        if self.paginate_by_param:
 | 
				
			||||||
            query_params = self.request.QUERY_PARAMS
 | 
					            query_params = self.request.QUERY_PARAMS
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                return int(query_params[self.paginate_by_param])
 | 
					                paginate_by_param = int(query_params[self.paginate_by_param])
 | 
				
			||||||
            except (KeyError, ValueError):
 | 
					            except (KeyError, ValueError):
 | 
				
			||||||
                pass
 | 
					                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):
 | 
					    def get_serializer_class(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -68,6 +68,7 @@ DEFAULTS = {
 | 
				
			||||||
    # Pagination
 | 
					    # Pagination
 | 
				
			||||||
    'PAGINATE_BY': None,
 | 
					    'PAGINATE_BY': None,
 | 
				
			||||||
    'PAGINATE_BY_PARAM': None,
 | 
					    'PAGINATE_BY_PARAM': None,
 | 
				
			||||||
 | 
					    'MAX_PAGINATE_BY': None,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # View configuration
 | 
					    # View configuration
 | 
				
			||||||
    'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
 | 
					    'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,6 +42,16 @@ class PaginateByParamView(generics.ListAPIView):
 | 
				
			||||||
    paginate_by_param = 'page_size'
 | 
					    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):
 | 
					class IntegrationTestPagination(TestCase):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Integration tests for paginated list views.
 | 
					    Integration tests for paginated list views.
 | 
				
			||||||
| 
						 | 
					@ -313,6 +323,42 @@ class TestCustomPaginateByParam(TestCase):
 | 
				
			||||||
        self.assertEqual(response.data['results'], self.data[:5])
 | 
					        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
 | 
					### Tests for context in pagination serializers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CustomField(serializers.Field):
 | 
					class CustomField(serializers.Field):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user