mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Merge branch 'cursor-limits' of https://github.com/uploadcare/django-rest-framework into uploadcare-cursor-limits
This commit is contained in:
commit
1d012aeb93
|
@ -449,13 +449,26 @@ class CursorPagination(BasePagination):
|
||||||
http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/
|
http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/
|
||||||
"""
|
"""
|
||||||
cursor_query_param = 'cursor'
|
cursor_query_param = 'cursor'
|
||||||
|
|
||||||
|
# The default page size.
|
||||||
|
# Defaults to `None`, meaning pagination is disabled.
|
||||||
page_size = api_settings.PAGE_SIZE
|
page_size = api_settings.PAGE_SIZE
|
||||||
|
|
||||||
|
# Client can control the page size using this query parameter.
|
||||||
|
# Default is 'None'. Set to eg 'page_size' to enable usage.
|
||||||
|
page_size_query_param = None
|
||||||
|
|
||||||
|
# Set to an integer to limit the maximum page size the client may request.
|
||||||
|
# Only relevant if 'page_size_query_param' has also been set.
|
||||||
|
max_page_size = None
|
||||||
|
|
||||||
invalid_cursor_message = _('Invalid cursor')
|
invalid_cursor_message = _('Invalid cursor')
|
||||||
ordering = '-created'
|
ordering = '-created'
|
||||||
template = 'rest_framework/pagination/previous_and_next.html'
|
template = 'rest_framework/pagination/previous_and_next.html'
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, request, view=None):
|
def paginate_queryset(self, queryset, request, view=None):
|
||||||
if self.page_size is None:
|
self.page_size = self.get_page_size(request)
|
||||||
|
if not self.page_size:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.base_url = request.build_absolute_uri()
|
self.base_url = request.build_absolute_uri()
|
||||||
|
@ -530,6 +543,19 @@ class CursorPagination(BasePagination):
|
||||||
|
|
||||||
return self.page
|
return self.page
|
||||||
|
|
||||||
|
def get_page_size(self, request):
|
||||||
|
if self.page_size_query_param:
|
||||||
|
try:
|
||||||
|
return _positive_int(
|
||||||
|
request.query_params[self.page_size_query_param],
|
||||||
|
strict=True,
|
||||||
|
cutoff=self.max_page_size
|
||||||
|
)
|
||||||
|
except (KeyError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return self.page_size
|
||||||
|
|
||||||
def get_next_link(self):
|
def get_next_link(self):
|
||||||
if not self.has_next:
|
if not self.has_next:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -513,6 +513,8 @@ class TestCursorPagination:
|
||||||
|
|
||||||
class ExamplePagination(pagination.CursorPagination):
|
class ExamplePagination(pagination.CursorPagination):
|
||||||
page_size = 5
|
page_size = 5
|
||||||
|
page_size_query_param = 'page_size'
|
||||||
|
max_page_size = 20
|
||||||
ordering = 'created'
|
ordering = 'created'
|
||||||
|
|
||||||
self.pagination = ExamplePagination()
|
self.pagination = ExamplePagination()
|
||||||
|
@ -648,6 +650,24 @@ class TestCursorPagination:
|
||||||
|
|
||||||
assert isinstance(self.pagination.to_html(), type(''))
|
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():
|
def test_get_displayed_page_numbers():
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user