[ADD] New settings 'ORDERING'. [ADD] Allow define a 'page_size' by view when using 'PageNumberPagination'. [ADD] Allow define a 'limit' by view when using 'LimitOffsetPagination'. [ADD] Allow define a 'ordering' by view when using 'CursorPagination'.

This commit is contained in:
Kelson da Costa Medeiros 2016-07-02 22:04:05 -03:00
parent 1d2fba906e
commit a723684c7f
2 changed files with 8 additions and 4 deletions

View File

@ -194,6 +194,7 @@ class PageNumberPagination(BasePagination):
Paginate a queryset if required, either returning a
page object, or `None` if pagination is not configured for this view.
"""
self.view_page_size = getattr(view, 'page_size', None)
page_size = self.get_page_size(request)
if not page_size:
return None
@ -237,7 +238,7 @@ class PageNumberPagination(BasePagination):
except (KeyError, ValueError):
pass
return self.page_size
return self.view_page_size or self.page_size
def get_next_link(self):
if not self.page.has_next():
@ -295,6 +296,7 @@ class LimitOffsetPagination(BasePagination):
template = 'rest_framework/pagination/numbers.html'
def paginate_queryset(self, queryset, request, view=None):
self.view_limit = getattr(view, 'limit', None)
self.limit = self.get_limit(request)
if self.limit is None:
return None
@ -325,7 +327,7 @@ class LimitOffsetPagination(BasePagination):
except (KeyError, ValueError):
pass
return self.default_limit
return self.view_limit or self.default_limit
def get_offset(self, request):
try:
@ -414,7 +416,7 @@ class CursorPagination(BasePagination):
cursor_query_param = 'cursor'
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
ordering = api_settings.ORDERING
template = 'rest_framework/pagination/previous_and_next.html'
# The offset in the cursor is used in situations where we have a
@ -429,6 +431,7 @@ class CursorPagination(BasePagination):
return None
self.base_url = request.build_absolute_uri()
self.view_ordering = getattr(view, 'ordering', None)
self.ordering = self.get_ordering(request, queryset, view)
self.cursor = self.decode_cursor(request)
@ -623,7 +626,7 @@ class CursorPagination(BasePagination):
else:
# The default case is to check for an `ordering` attribute
# on this pagination instance.
ordering = self.ordering
ordering = self.view_ordering or self.ordering
assert ordering is not None, (
'Using cursor pagination, but no ordering attribute was declared '
'on the pagination class.'

View File

@ -64,6 +64,7 @@ DEFAULTS = {
# Pagination
'PAGE_SIZE': None,
'ORDERING': '-created',
# Filtering
'SEARCH_PARAM': 'search',