mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
make offset_cutoff customizable
This commit is contained in:
parent
6ff5385996
commit
d3cd207411
|
@ -454,6 +454,12 @@ class CursorPagination(BasePagination):
|
||||||
ordering = '-created'
|
ordering = '-created'
|
||||||
template = 'rest_framework/pagination/previous_and_next.html'
|
template = 'rest_framework/pagination/previous_and_next.html'
|
||||||
|
|
||||||
|
# The offset in the cursor is used in situations where we have a
|
||||||
|
# nearly-unique index. (Eg millisecond precision creation timestamps)
|
||||||
|
# We guard against malicious users attempting to cause expensive database
|
||||||
|
# queries, by having a hard cap on the maximum possible size of the offset.
|
||||||
|
offset_cutoff = 1000
|
||||||
|
|
||||||
def paginate_queryset(self, queryset, request, view=None):
|
def paginate_queryset(self, queryset, request, view=None):
|
||||||
if self.page_size is None:
|
if self.page_size is None:
|
||||||
return None
|
return None
|
||||||
|
@ -675,18 +681,12 @@ class CursorPagination(BasePagination):
|
||||||
if encoded is None:
|
if encoded is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# The offset in the cursor is used in situations where we have a
|
|
||||||
# nearly-unique index. (Eg millisecond precision creation timestamps)
|
|
||||||
# We guard against malicious users attempting to cause expensive database
|
|
||||||
# queries, by having a hard cap on the maximum possible size of the offset.
|
|
||||||
OFFSET_CUTOFF = 1000
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
querystring = b64decode(encoded.encode('ascii')).decode('ascii')
|
querystring = b64decode(encoded.encode('ascii')).decode('ascii')
|
||||||
tokens = urlparse.parse_qs(querystring, keep_blank_values=True)
|
tokens = urlparse.parse_qs(querystring, keep_blank_values=True)
|
||||||
|
|
||||||
offset = tokens.get('o', ['0'])[0]
|
offset = tokens.get('o', ['0'])[0]
|
||||||
offset = _positive_int(offset, cutoff=OFFSET_CUTOFF)
|
offset = _positive_int(offset, cutoff=self.offset_cutoff)
|
||||||
|
|
||||||
reverse = tokens.get('r', ['0'])[0]
|
reverse = tokens.get('r', ['0'])[0]
|
||||||
reverse = bool(int(reverse))
|
reverse = bool(int(reverse))
|
||||||
|
|
|
@ -6,7 +6,7 @@ import pytest
|
||||||
from rest_framework import (
|
from rest_framework import (
|
||||||
exceptions, filters, generics, pagination, serializers, status
|
exceptions, filters, generics, pagination, serializers, status
|
||||||
)
|
)
|
||||||
from rest_framework.pagination import PAGE_BREAK, PageLink
|
from rest_framework.pagination import PAGE_BREAK, PageLink, Cursor
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
|
|
||||||
|
@ -648,6 +648,16 @@ class TestCursorPagination:
|
||||||
|
|
||||||
assert isinstance(self.pagination.to_html(), type(''))
|
assert isinstance(self.pagination.to_html(), type(''))
|
||||||
|
|
||||||
|
def test_offset_cutoff(self):
|
||||||
|
(previous, current, next, previous_url, next_url) = self.get_pages('/')
|
||||||
|
|
||||||
|
next_url = self.pagination.encode_cursor(Cursor(1050, False, None))
|
||||||
|
(previous, current, next, previous_url, next_url) = self.get_pages(next_url)
|
||||||
|
|
||||||
|
assert previous == [7, 7, 7, 8, 9]
|
||||||
|
assert current == []
|
||||||
|
assert next is None
|
||||||
|
|
||||||
|
|
||||||
def test_get_displayed_page_numbers():
|
def test_get_displayed_page_numbers():
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user