Commit 38e94bb8 by Stephan Groß

added global and per resource on/off switch + updated docs

parent 5967f15f
...@@ -149,7 +149,8 @@ Should be mixed in with [MultipleObjectAPIView]. ...@@ -149,7 +149,8 @@ Should be mixed in with [MultipleObjectAPIView].
**Arguments**: **Arguments**:
* `page_size` - Hook to adjust page_size per request. * `allow_page_size_param` - Allows you to overwrite the global settings `ALLOW_PAGE_SIZE_PARAM` for a specific view.
* `page_size_param` - Allows you to customize the page_size parameter. Default is `page_size`.
## CreateModelMixin ## CreateModelMixin
......
...@@ -150,4 +150,10 @@ Default: `'accept'` ...@@ -150,4 +150,10 @@ Default: `'accept'`
Default: `'format'` Default: `'format'`
## ALLOW_PAGE_SIZE_PARAM
Allows you to globally pass a page size parameter for an individual request.
Default: `'True'`
[cite]: http://www.python.org/dev/peps/pep-0020/ [cite]: http://www.python.org/dev/peps/pep-0020/
...@@ -7,6 +7,7 @@ which allows mixin classes to be composed in interesting ways. ...@@ -7,6 +7,7 @@ which allows mixin classes to be composed in interesting ways.
from django.http import Http404 from django.http import Http404
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.settings import api_settings
class CreateModelMixin(object): class CreateModelMixin(object):
...@@ -32,6 +33,8 @@ class ListModelMixin(object): ...@@ -32,6 +33,8 @@ class ListModelMixin(object):
Should be mixed in with `MultipleObjectAPIView`. Should be mixed in with `MultipleObjectAPIView`.
""" """
empty_error = u"Empty list and '%(class_name)s.allow_empty' is False." empty_error = u"Empty list and '%(class_name)s.allow_empty' is False."
allow_page_size_param = api_settings.ALLOW_PAGE_SIZE_PARAM
page_size_param = 'page_size'
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
self.object_list = self.get_filtered_queryset() self.object_list = self.get_filtered_queryset()
...@@ -56,13 +59,14 @@ class ListModelMixin(object): ...@@ -56,13 +59,14 @@ class ListModelMixin(object):
return Response(serializer.data) return Response(serializer.data)
def get_paginate_by(self, queryset): def get_paginate_by(self, queryset):
page_size_param = self.request.QUERY_PARAMS.get('page_size') if self.allow_page_size_param:
if page_size_param: page_size_param = self.request.QUERY_PARAMS.get(self.page_size_param)
try: if page_size_param:
page_size = int(page_size_param) try:
return page_size page_size = int(page_size_param)
except ValueError: return page_size
pass except ValueError:
pass
return super(ListModelMixin, self).get_paginate_by(queryset) return super(ListModelMixin, self).get_paginate_by(queryset)
......
...@@ -66,7 +66,9 @@ DEFAULTS = { ...@@ -66,7 +66,9 @@ DEFAULTS = {
'URL_ACCEPT_OVERRIDE': 'accept', 'URL_ACCEPT_OVERRIDE': 'accept',
'URL_FORMAT_OVERRIDE': 'format', 'URL_FORMAT_OVERRIDE': 'format',
'FORMAT_SUFFIX_KWARG': 'format' 'FORMAT_SUFFIX_KWARG': 'format',
'ALLOW_PAGE_SIZE_PARAM': True
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment