use PageSizePaginationMixin for CursorPagination

tests for custom page_size in CursorPagination
This commit is contained in:
homm 2015-06-12 18:57:46 +03:00
parent 07575eb46c
commit 02707aa92e
2 changed files with 25 additions and 5 deletions

View File

@ -487,20 +487,20 @@ class LimitOffsetPagination(BasePagination):
return template.render(context)
class CursorPagination(BasePagination):
class CursorPagination(PageSizePaginationMixin, BasePagination):
"""
The cursor pagination implementation is neccessarily complex.
For an overview of the position/offset style we use, see this post:
http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/
"""
cursor_query_param = 'cursor'
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
template = 'rest_framework/pagination/previous_and_next.html'
def paginate_queryset(self, queryset, request, view=None):
if self.page_size is None:
self.page_size = page_size = self.get_page_size(request)
if not page_size:
return None
self.base_url = request.build_absolute_uri()
@ -540,8 +540,8 @@ class CursorPagination(BasePagination):
# If we have an offset cursor then offset the entire page by that amount.
# We also always fetch an extra item in order to determine if there is a
# page following on from this one.
results = list(queryset[offset:offset + self.page_size + 1])
self.page = list(results[:self.page_size])
results = list(queryset[offset:offset + page_size + 1])
self.page = list(results[:page_size])
# Determine the position of the final item following the page.
if len(results) > len(self.page):

View File

@ -508,6 +508,8 @@ class TestCursorPagination:
class ExamplePagination(pagination.CursorPagination):
page_size = 5
page_size_query_param = 'page_size'
max_page_size = 20
ordering = 'created'
self.pagination = ExamplePagination()
@ -643,6 +645,24 @@ class TestCursorPagination:
assert isinstance(self.pagination.to_html(), type(''))
def test_page_size(self):
(previous, current, next, previous_url, next_url) = \
self.get_pages('/?page_size=10')
assert previous is None
assert current == [1, 1, 1, 1, 1, 1, 2, 3, 4, 4]
assert next == [4, 4, 5, 6, 7, 7, 7, 7, 7, 7]
assert 'page_size=10' in next_url
(previous, current, next, previous_url, next_url) = \
self.get_pages(next_url.replace('page_size=10', 'page_size=4'))
assert previous == [2, 3, 4, 4]
assert current == [4, 4, 5, 6]
assert next == [7, 7, 7, 7]
assert 'page_size=4' in previous_url
assert 'page_size=4' in next_url
def test_get_displayed_page_numbers():
"""