mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
make PageSizePaginationMixin from all this page_size stuff
This commit is contained in:
parent
e4b0273f76
commit
9a5373d41c
|
@ -201,7 +201,34 @@ class BasePagination(object):
|
||||||
raise NotImplementedError('to_html() must be implemented to display page controls.')
|
raise NotImplementedError('to_html() must be implemented to display page controls.')
|
||||||
|
|
||||||
|
|
||||||
class PageNumberPagination(BasePagination):
|
class BasePageSizePagination(BasePagination):
|
||||||
|
# The default page size.
|
||||||
|
# Defaults to `None`, meaning pagination is disabled.
|
||||||
|
page_size = api_settings.PAGE_SIZE
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
class PageNumberPagination(BasePageSizePagination):
|
||||||
"""
|
"""
|
||||||
A simple page number based style that supports page numbers as
|
A simple page number based style that supports page numbers as
|
||||||
query parameters. For example:
|
query parameters. For example:
|
||||||
|
@ -209,21 +236,10 @@ class PageNumberPagination(BasePagination):
|
||||||
http://api.example.org/accounts/?page=4
|
http://api.example.org/accounts/?page=4
|
||||||
http://api.example.org/accounts/?page=4&page_size=100
|
http://api.example.org/accounts/?page=4&page_size=100
|
||||||
"""
|
"""
|
||||||
# The default page size.
|
|
||||||
# Defaults to `None`, meaning pagination is disabled.
|
|
||||||
page_size = api_settings.PAGE_SIZE
|
|
||||||
|
|
||||||
# Client can control the page using this query parameter.
|
# Client can control the page using this query parameter.
|
||||||
page_query_param = 'page'
|
page_query_param = 'page'
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
last_page_strings = ('last',)
|
last_page_strings = ('last',)
|
||||||
|
|
||||||
template = 'rest_framework/pagination/numbers.html'
|
template = 'rest_framework/pagination/numbers.html'
|
||||||
|
@ -318,19 +334,6 @@ class PageNumberPagination(BasePagination):
|
||||||
('results', data)
|
('results', data)
|
||||||
]))
|
]))
|
||||||
|
|
||||||
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.page.has_next():
|
if not self.page.has_next():
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user