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
0795f739
Commit
0795f739
authored
May 10, 2016
by
José Padilla
Committed by
Tom Christie
May 10, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prevent raising exception when limit is 0 (#4098)
parent
5ac96851
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletions
+34
-1
rest_framework/pagination.py
+9
-1
tests/test_pagination.py
+25
-0
No files found.
rest_framework/pagination.py
View file @
0795f739
...
...
@@ -36,10 +36,11 @@ def _positive_int(integer_string, strict=False, cutoff=None):
def
_divide_with_ceil
(
a
,
b
):
"""
Returns 'a' divded by 'b', with any remainder rounded up.
Returns 'a' div
i
ded by 'b', with any remainder rounded up.
"""
if
a
%
b
:
return
(
a
//
b
)
+
1
return
a
//
b
...
...
@@ -358,7 +359,10 @@ class LimitOffsetPagination(BasePagination):
def
get_html_context
(
self
):
base_url
=
self
.
request
.
build_absolute_uri
()
if
self
.
limit
:
current
=
_divide_with_ceil
(
self
.
offset
,
self
.
limit
)
+
1
# The number of pages is a little bit fiddly.
# We need to sum both the number of pages from current offset to end
# plus the number of pages up to the current offset.
...
...
@@ -368,8 +372,12 @@ class LimitOffsetPagination(BasePagination):
_divide_with_ceil
(
self
.
count
-
self
.
offset
,
self
.
limit
)
+
_divide_with_ceil
(
self
.
offset
,
self
.
limit
)
)
if
final
<
1
:
final
=
1
else
:
current
=
1
final
=
1
if
current
>
final
:
current
=
final
...
...
tests/test_pagination.py
View file @
0795f739
...
...
@@ -505,6 +505,31 @@ class TestLimitOffset:
assert
content
.
get
(
'next'
)
==
next_url
assert
content
.
get
(
'previous'
)
==
prev_url
def
test_limit_zero
(
self
):
"""
A limit of 0 should return empty results.
"""
request
=
Request
(
factory
.
get
(
'/'
,
{
'limit'
:
0
,
'offset'
:
10
}))
queryset
=
self
.
paginate_queryset
(
request
)
context
=
self
.
get_html_context
()
content
=
self
.
get_paginated_content
(
queryset
)
assert
context
==
{
'previous_url'
:
'http://testserver/?limit=0&offset=10'
,
'page_links'
:
[
PageLink
(
url
=
'http://testserver/?limit=0'
,
number
=
1
,
is_active
=
True
,
is_break
=
False
)
],
'next_url'
:
'http://testserver/?limit=0&offset=10'
}
assert
queryset
==
[]
assert
content
.
get
(
'results'
)
==
[]
class
TestCursorPagination
:
"""
...
...
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