mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Merge pull request #3015 from strixcuriosus/limit-offset-pagination-link-urls
Include correct limits in LimitOffsetPagination link urls
This commit is contained in:
commit
6651432d59
|
@ -431,6 +431,8 @@ class LimitOffsetPagination(BasePagination):
|
|||
return None
|
||||
|
||||
url = self.request.build_absolute_uri()
|
||||
url = replace_query_param(url, self.limit_query_param, self.limit)
|
||||
|
||||
offset = self.offset + self.limit
|
||||
return replace_query_param(url, self.offset_query_param, offset)
|
||||
|
||||
|
@ -439,6 +441,7 @@ class LimitOffsetPagination(BasePagination):
|
|||
return None
|
||||
|
||||
url = self.request.build_absolute_uri()
|
||||
url = replace_query_param(url, self.limit_query_param, self.limit)
|
||||
|
||||
if self.offset - self.limit <= 0:
|
||||
return remove_query_param(url, self.offset_query_param)
|
||||
|
|
|
@ -287,6 +287,8 @@ class TestLimitOffset:
|
|||
def setup(self):
|
||||
class ExamplePagination(pagination.LimitOffsetPagination):
|
||||
default_limit = 10
|
||||
max_limit = 15
|
||||
|
||||
self.pagination = ExamplePagination()
|
||||
self.queryset = range(1, 101)
|
||||
|
||||
|
@ -442,7 +444,31 @@ class TestLimitOffset:
|
|||
"""
|
||||
request = Request(factory.get('/', {'limit': 'invalid', 'offset': 0}))
|
||||
queryset = self.paginate_queryset(request)
|
||||
content = self.get_paginated_content(queryset)
|
||||
next_limit = self.pagination.default_limit
|
||||
next_offset = self.pagination.default_limit
|
||||
next_url = 'http://testserver/?limit={0}&offset={1}'.format(next_limit, next_offset)
|
||||
assert queryset == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
assert content.get('next') == next_url
|
||||
|
||||
def test_max_limit(self):
|
||||
"""
|
||||
The limit defaults to the max_limit when there is a max_limit and the
|
||||
requested limit is greater than the max_limit
|
||||
"""
|
||||
offset = 50
|
||||
request = Request(factory.get('/', {'limit': '11235', 'offset': offset}))
|
||||
queryset = self.paginate_queryset(request)
|
||||
content = self.get_paginated_content(queryset)
|
||||
max_limit = self.pagination.max_limit
|
||||
next_offset = offset + max_limit
|
||||
prev_offset = offset - max_limit
|
||||
base_url = 'http://testserver/?limit={0}'.format(max_limit)
|
||||
next_url = base_url + '&offset={0}'.format(next_offset)
|
||||
prev_url = base_url + '&offset={0}'.format(prev_offset)
|
||||
assert queryset == list(range(51, 66))
|
||||
assert content.get('next') == next_url
|
||||
assert content.get('previous') == prev_url
|
||||
|
||||
|
||||
class TestCursorPagination:
|
||||
|
|
Loading…
Reference in New Issue
Block a user