Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
316de3a8
Commit
316de3a8
authored
Aug 26, 2013
by
Alexander Akhmetov
Committed by
Alexander Akhmetov
Aug 26, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added max_paginate_by parameter
parent
2bcad32d
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
2 deletions
+55
-2
rest_framework/generics.py
+8
-2
rest_framework/settings.py
+1
-0
rest_framework/tests/test_pagination.py
+46
-0
No files found.
rest_framework/generics.py
View file @
316de3a8
...
@@ -56,6 +56,7 @@ class GenericAPIView(views.APIView):
...
@@ -56,6 +56,7 @@ class GenericAPIView(views.APIView):
# Pagination settings
# Pagination settings
paginate_by
=
api_settings
.
PAGINATE_BY
paginate_by
=
api_settings
.
PAGINATE_BY
paginate_by_param
=
api_settings
.
PAGINATE_BY_PARAM
paginate_by_param
=
api_settings
.
PAGINATE_BY_PARAM
max_paginate_by
=
api_settings
.
MAX_PAGINATE_BY
pagination_serializer_class
=
api_settings
.
DEFAULT_PAGINATION_SERIALIZER_CLASS
pagination_serializer_class
=
api_settings
.
DEFAULT_PAGINATION_SERIALIZER_CLASS
page_kwarg
=
'page'
page_kwarg
=
'page'
...
@@ -207,11 +208,16 @@ class GenericAPIView(views.APIView):
...
@@ -207,11 +208,16 @@ class GenericAPIView(views.APIView):
if
self
.
paginate_by_param
:
if
self
.
paginate_by_param
:
query_params
=
self
.
request
.
QUERY_PARAMS
query_params
=
self
.
request
.
QUERY_PARAMS
try
:
try
:
return
int
(
query_params
[
self
.
paginate_by_param
])
paginate_by_param
=
int
(
query_params
[
self
.
paginate_by_param
])
except
(
KeyError
,
ValueError
):
except
(
KeyError
,
ValueError
):
pass
pass
else
:
if
self
.
max_paginate_by
:
return
min
(
self
.
max_paginate_by
,
paginate_by_param
)
else
:
return
paginate_by_param
return
self
.
paginate_by
return
min
(
self
.
max_paginate_by
,
self
.
paginate_by
)
or
self
.
paginate_by
def
get_serializer_class
(
self
):
def
get_serializer_class
(
self
):
"""
"""
...
...
rest_framework/settings.py
View file @
316de3a8
...
@@ -68,6 +68,7 @@ DEFAULTS = {
...
@@ -68,6 +68,7 @@ DEFAULTS = {
# Pagination
# Pagination
'PAGINATE_BY'
:
None
,
'PAGINATE_BY'
:
None
,
'PAGINATE_BY_PARAM'
:
None
,
'PAGINATE_BY_PARAM'
:
None
,
'MAX_PAGINATE_BY'
:
None
,
# View configuration
# View configuration
'VIEW_NAME_FUNCTION'
:
'rest_framework.views.get_view_name'
,
'VIEW_NAME_FUNCTION'
:
'rest_framework.views.get_view_name'
,
...
...
rest_framework/tests/test_pagination.py
View file @
316de3a8
...
@@ -42,6 +42,16 @@ class PaginateByParamView(generics.ListAPIView):
...
@@ -42,6 +42,16 @@ class PaginateByParamView(generics.ListAPIView):
paginate_by_param
=
'page_size'
paginate_by_param
=
'page_size'
class
MaxPaginateByView
(
generics
.
ListAPIView
):
"""
View for testing custom max_paginate_by usage
"""
model
=
BasicModel
paginate_by
=
5
max_paginate_by
=
3
paginate_by_param
=
'page_size'
class
IntegrationTestPagination
(
TestCase
):
class
IntegrationTestPagination
(
TestCase
):
"""
"""
Integration tests for paginated list views.
Integration tests for paginated list views.
...
@@ -313,6 +323,42 @@ class TestCustomPaginateByParam(TestCase):
...
@@ -313,6 +323,42 @@ class TestCustomPaginateByParam(TestCase):
self
.
assertEqual
(
response
.
data
[
'results'
],
self
.
data
[:
5
])
self
.
assertEqual
(
response
.
data
[
'results'
],
self
.
data
[:
5
])
class
TestMaxPaginateByParam
(
TestCase
):
"""
Tests for list views with max_paginate_by kwarg
"""
def
setUp
(
self
):
"""
Create 13 BasicModel instances.
"""
for
i
in
range
(
13
):
BasicModel
(
text
=
i
)
.
save
()
self
.
objects
=
BasicModel
.
objects
self
.
data
=
[
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
}
for
obj
in
self
.
objects
.
all
()
]
self
.
view
=
MaxPaginateByView
.
as_view
()
def
test_max_paginate_by
(
self
):
"""
If max_paginate_by is set and it less than paginate_by, new kwarg should limit requests for review.
"""
request
=
factory
.
get
(
'/?page_size=10'
)
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
data
[
'count'
],
13
)
self
.
assertEqual
(
response
.
data
[
'results'
],
self
.
data
[:
3
])
def
test_max_paginate_by_without_page_size_param
(
self
):
"""
If max_paginate_by is set, new kwarg should limit requests for review.
"""
request
=
factory
.
get
(
'/'
)
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
data
[
'results'
],
self
.
data
[:
3
])
### Tests for context in pagination serializers
### Tests for context in pagination serializers
class
CustomField
(
serializers
.
Field
):
class
CustomField
(
serializers
.
Field
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment