-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MAX_PAGE_SIZE setting #9107
base: master
Are you sure you want to change the base?
Changes from all commits
a9d25af
1ada7c7
91695fb
b51141c
6250dc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,9 +169,6 @@ class PageNumberPagination(BasePagination): | |
http://api.example.org/accounts/?page=4 | ||
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 | ||
|
||
django_paginator_class = DjangoPaginator | ||
|
||
|
@@ -184,16 +181,33 @@ class PageNumberPagination(BasePagination): | |
page_size_query_param = None | ||
page_size_query_description = _('Number of results to return per page.') | ||
|
||
# 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',) | ||
|
||
template = 'rest_framework/pagination/numbers.html' | ||
|
||
invalid_page_message = _('Invalid page.') | ||
|
||
@property | ||
def page_size(self) -> int: | ||
"""Get default page size. | ||
|
||
Defaults to `None`, meaning pagination is disabled. | ||
|
||
""" | ||
return api_settings.PAGE_SIZE | ||
|
||
@property | ||
def max_page_size(self) -> int: | ||
"""Limit 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. | ||
Defaults to `None`, meaning page size is unlimited. | ||
It's recommended that you would set a limit to avoid api abuse. | ||
|
||
""" | ||
return api_settings.MAX_PAGE_SIZE | ||
|
||
def paginate_queryset(self, queryset, request, view=None): | ||
""" | ||
Paginate a queryset if required, either returning a | ||
|
@@ -377,14 +391,33 @@ class LimitOffsetPagination(BasePagination): | |
http://api.example.org/accounts/?limit=100 | ||
http://api.example.org/accounts/?offset=400&limit=100 | ||
""" | ||
default_limit = api_settings.PAGE_SIZE | ||
limit_query_param = 'limit' | ||
limit_query_description = _('Number of results to return per page.') | ||
offset_query_param = 'offset' | ||
offset_query_description = _('The initial index from which to return the results.') | ||
max_limit = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid changing the style here? max_limit = api_settings.MAX_PAGE_SIZE Perfer minimal impact footprints for PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's related to #8993 and #9107 (comment). Without it we can't properly test it, since this setting will be set during init. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm... not sure? If you'd like to take that approach then I'd probably suggest handling #8993 separately, as a precursor to this. |
||
template = 'rest_framework/pagination/numbers.html' | ||
|
||
@property | ||
def max_limit(self) -> int: | ||
"""Limit maximum 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. | ||
Defaults to `None`, meaning page size is unlimited. | ||
It's recommended that you would set a limit to avoid api abuse. | ||
|
||
""" | ||
return api_settings.MAX_PAGE_SIZE | ||
|
||
@property | ||
def default_limit(self) -> int: | ||
"""Get default page size. | ||
|
||
Defaults to `None`, meaning pagination is disabled. | ||
|
||
""" | ||
return api_settings.PAGE_SIZE | ||
|
||
def paginate_queryset(self, queryset, request, view=None): | ||
self.request = request | ||
self.limit = self.get_limit(request) | ||
|
@@ -588,7 +621,6 @@ class CursorPagination(BasePagination): | |
""" | ||
cursor_query_param = 'cursor' | ||
cursor_query_description = _('The pagination cursor value.') | ||
page_size = api_settings.PAGE_SIZE | ||
invalid_cursor_message = _('Invalid cursor') | ||
ordering = '-created' | ||
template = 'rest_framework/pagination/previous_and_next.html' | ||
|
@@ -598,16 +630,33 @@ class CursorPagination(BasePagination): | |
page_size_query_param = None | ||
page_size_query_description = _('Number of results to return per page.') | ||
|
||
# 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 | ||
|
||
# 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 | ||
|
||
@property | ||
def page_size(self) -> int: | ||
"""Get default page size. | ||
|
||
Defaults to `None`, meaning pagination is disabled. | ||
|
||
""" | ||
return api_settings.PAGE_SIZE | ||
|
||
@property | ||
def max_page_size(self) -> int: | ||
"""Limit 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. | ||
Defaults to `None`, meaning page size is unlimited. | ||
It's recommended that you would set a limit to avoid api abuse. | ||
|
||
""" | ||
return api_settings.MAX_PAGE_SIZE | ||
|
||
def paginate_queryset(self, queryset, request, view=None): | ||
self.request = request | ||
self.page_size = self.get_page_size(request) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ | |
|
||
# Pagination | ||
'PAGE_SIZE': None, | ||
"MAX_PAGE_SIZE": None, | ||
|
||
# Filtering | ||
'SEARCH_PARAM': 'search', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There shoud probably be a comment explaining why this i a property to avoid removing it in a future refactor?