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
94b5f7a8
Commit
94b5f7a8
authored
Jan 22, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidy up cursor tests and make more comprehensive
parent
f1af603f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
132 deletions
+104
-132
rest_framework/pagination.py
+21
-5
tests/test_pagination.py
+83
-127
No files found.
rest_framework/pagination.py
View file @
94b5f7a8
...
@@ -171,6 +171,8 @@ class PageNumberPagination(BasePagination):
...
@@ -171,6 +171,8 @@ class PageNumberPagination(BasePagination):
template
=
'rest_framework/pagination/numbers.html'
template
=
'rest_framework/pagination/numbers.html'
invalid_page_message
=
_
(
'Invalid page "{page_number}": {message}.'
)
def
_handle_backwards_compat
(
self
,
view
):
def
_handle_backwards_compat
(
self
,
view
):
"""
"""
Prior to version 3.1, pagination was handled in the view, and the
Prior to version 3.1, pagination was handled in the view, and the
...
@@ -203,7 +205,7 @@ class PageNumberPagination(BasePagination):
...
@@ -203,7 +205,7 @@ class PageNumberPagination(BasePagination):
try
:
try
:
self
.
page
=
paginator
.
page
(
page_number
)
self
.
page
=
paginator
.
page
(
page_number
)
except
InvalidPage
as
exc
:
except
InvalidPage
as
exc
:
msg
=
_
(
'Invalid page "{page_number}": {message}.'
)
.
format
(
msg
=
self
.
invalid_page_message
.
format
(
page_number
=
page_number
,
message
=
six
.
text_type
(
exc
)
page_number
=
page_number
,
message
=
six
.
text_type
(
exc
)
)
)
raise
NotFound
(
msg
)
raise
NotFound
(
msg
)
...
@@ -386,8 +388,8 @@ Cursor = namedtuple('Cursor', ['offset', 'reverse', 'position'])
...
@@ -386,8 +388,8 @@ Cursor = namedtuple('Cursor', ['offset', 'reverse', 'position'])
def
decode_cursor
(
encoded
):
def
decode_cursor
(
encoded
):
tokens
=
urlparse
.
parse_qs
(
b64decode
(
encoded
),
keep_blank_values
=
True
)
try
:
try
:
tokens
=
urlparse
.
parse_qs
(
b64decode
(
encoded
),
keep_blank_values
=
True
)
offset
=
int
(
tokens
[
'offset'
][
0
])
offset
=
int
(
tokens
[
'offset'
][
0
])
reverse
=
bool
(
int
(
tokens
[
'reverse'
][
0
]))
reverse
=
bool
(
int
(
tokens
[
'reverse'
][
0
]))
position
=
tokens
[
'position'
][
0
]
position
=
tokens
[
'position'
][
0
]
...
@@ -411,7 +413,8 @@ class CursorPagination(BasePagination):
...
@@ -411,7 +413,8 @@ class CursorPagination(BasePagination):
# Support case where ordering is already negative
# Support case where ordering is already negative
# Support tuple orderings
# Support tuple orderings
cursor_query_param
=
'cursor'
cursor_query_param
=
'cursor'
page_size
=
5
page_size
=
api_settings
.
PAGINATE_BY
invalid_cursor_message
=
_
(
'Invalid cursor'
)
def
paginate_queryset
(
self
,
queryset
,
request
,
view
=
None
):
def
paginate_queryset
(
self
,
queryset
,
request
,
view
=
None
):
self
.
base_url
=
request
.
build_absolute_uri
()
self
.
base_url
=
request
.
build_absolute_uri
()
...
@@ -424,8 +427,9 @@ class CursorPagination(BasePagination):
...
@@ -424,8 +427,9 @@ class CursorPagination(BasePagination):
(
offset
,
reverse
,
current_position
)
=
(
0
,
False
,
''
)
(
offset
,
reverse
,
current_position
)
=
(
0
,
False
,
''
)
else
:
else
:
self
.
cursor
=
decode_cursor
(
encoded
)
self
.
cursor
=
decode_cursor
(
encoded
)
if
self
.
cursor
is
None
:
raise
NotFound
(
self
.
invalid_cursor_message
)
(
offset
,
reverse
,
current_position
)
=
self
.
cursor
(
offset
,
reverse
,
current_position
)
=
self
.
cursor
# TODO: Invalid cursors should 404
# Cursor pagination always enforces an ordering.
# Cursor pagination always enforces an ordering.
if
reverse
:
if
reverse
:
...
@@ -458,7 +462,7 @@ class CursorPagination(BasePagination):
...
@@ -458,7 +462,7 @@ class CursorPagination(BasePagination):
# If we have a reverse queryset, then the query ordering was in reverse
# If we have a reverse queryset, then the query ordering was in reverse
# so we need to reverse the items again before returning them to the user.
# so we need to reverse the items again before returning them to the user.
if
reverse
:
if
reverse
:
self
.
page
=
reversed
(
self
.
page
)
self
.
page
=
list
(
reversed
(
self
.
page
)
)
if
reverse
:
if
reverse
:
# Determine next and previous positions for reverse cursors.
# Determine next and previous positions for reverse cursors.
...
@@ -483,8 +487,14 @@ class CursorPagination(BasePagination):
...
@@ -483,8 +487,14 @@ class CursorPagination(BasePagination):
if
not
self
.
has_next
:
if
not
self
.
has_next
:
return
None
return
None
if
self
.
cursor
and
self
.
cursor
.
reverse
and
self
.
cursor
.
offset
!=
0
:
# If we're reversing direction and we have an offset cursor
# then we cannot use the first position we find as a marker.
compare
=
self
.
_get_position_from_instance
(
self
.
page
[
-
1
],
self
.
ordering
)
else
:
compare
=
self
.
next_position
compare
=
self
.
next_position
offset
=
0
offset
=
0
for
item
in
reversed
(
self
.
page
):
for
item
in
reversed
(
self
.
page
):
position
=
self
.
_get_position_from_instance
(
item
,
self
.
ordering
)
position
=
self
.
_get_position_from_instance
(
item
,
self
.
ordering
)
if
position
!=
compare
:
if
position
!=
compare
:
...
@@ -526,8 +536,14 @@ class CursorPagination(BasePagination):
...
@@ -526,8 +536,14 @@ class CursorPagination(BasePagination):
if
not
self
.
has_previous
:
if
not
self
.
has_previous
:
return
None
return
None
if
self
.
cursor
and
not
self
.
cursor
.
reverse
and
self
.
cursor
.
offset
!=
0
:
# If we're reversing direction and we have an offset cursor
# then we cannot use the first position we find as a marker.
compare
=
self
.
_get_position_from_instance
(
self
.
page
[
0
],
self
.
ordering
)
else
:
compare
=
self
.
previous_position
compare
=
self
.
previous_position
offset
=
0
offset
=
0
for
item
in
self
.
page
:
for
item
in
self
.
page
:
position
=
self
.
_get_position_from_instance
(
item
,
self
.
ordering
)
position
=
self
.
_get_position_from_instance
(
item
,
self
.
ordering
)
if
position
!=
compare
:
if
position
!=
compare
:
...
...
tests/test_pagination.py
View file @
94b5f7a8
...
@@ -451,171 +451,127 @@ class TestCursorPagination:
...
@@ -451,171 +451,127 @@ class TestCursorPagination:
def
order_by
(
self
,
ordering
):
def
order_by
(
self
,
ordering
):
if
ordering
.
startswith
(
'-'
):
if
ordering
.
startswith
(
'-'
):
return
MockQuerySet
(
reversed
(
self
.
items
))
return
MockQuerySet
(
list
(
reversed
(
self
.
items
)
))
return
self
return
self
def
__getitem__
(
self
,
sliced
):
def
__getitem__
(
self
,
sliced
):
return
self
.
items
[
sliced
]
return
self
.
items
[
sliced
]
self
.
pagination
=
pagination
.
CursorPagination
()
class
ExamplePagination
(
pagination
.
CursorPagination
):
self
.
queryset
=
MockQuerySet
(
page_size
=
5
[
MockObject
(
idx
)
for
idx
in
range
(
1
,
16
)]
)
def
paginate_queryset
(
self
,
request
):
return
list
(
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
))
# def get_paginated_content(self, queryset):
# response = self.pagination.get_paginated_response(queryset)
# return response.data
# def get_html_context(self):
# return self.pagination.get_html_context()
def
test_following_cursor
(
self
):
self
.
pagination
=
ExamplePagination
()
request
=
Request
(
factory
.
get
(
'/'
))
self
.
queryset
=
MockQuerySet
([
queryset
=
self
.
paginate_queryset
(
request
)
MockObject
(
idx
)
for
idx
in
[
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
2
,
3
,
4
,
5
]
1
,
1
,
1
,
1
,
1
,
1
,
2
,
3
,
4
,
4
,
4
,
4
,
5
,
6
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
8
,
9
,
9
,
9
,
9
,
9
,
9
]
])
next_url
=
self
.
pagination
.
get_next_link
()
def
get_pages
(
self
,
url
):
assert
next_url
"""
Given a URL return a tuple of:
request
=
Request
(
factory
.
get
(
next_url
))
(previous page, current page, next page, previous url, next url)
queryset
=
self
.
paginate_queryset
(
request
)
"""
assert
[
item
.
created
for
item
in
queryset
]
==
[
6
,
7
,
8
,
9
,
10
]
request
=
Request
(
factory
.
get
(
url
))
queryset
=
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
)
current
=
[
item
.
created
for
item
in
queryset
]
next_url
=
self
.
pagination
.
get_next_link
()
next_url
=
self
.
pagination
.
get_next_link
()
assert
next_url
previous_url
=
self
.
pagination
.
get_previous_link
()
if
next_url
is
not
None
:
request
=
Request
(
factory
.
get
(
next_url
))
request
=
Request
(
factory
.
get
(
next_url
))
queryset
=
self
.
paginate_queryset
(
request
)
queryset
=
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
11
,
12
,
13
,
14
,
15
]
next
=
[
item
.
created
for
item
in
queryset
]
else
:
next_url
=
self
.
pagination
.
get_next_link
()
next
=
None
assert
next_url
is
None
# Now page back again
previous_url
=
self
.
pagination
.
get_previous_link
()
assert
previous_url
if
previous_url
is
not
None
:
request
=
Request
(
factory
.
get
(
previous_url
))
request
=
Request
(
factory
.
get
(
previous_url
))
queryset
=
self
.
paginate_queryset
(
request
)
queryset
=
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
6
,
7
,
8
,
9
,
10
]
previous
=
[
item
.
created
for
item
in
queryset
]
else
:
previous
=
None
previous_url
=
self
.
pagination
.
get_previous_link
()
return
(
previous
,
current
,
next
,
previous_url
,
next_url
)
assert
previous_url
request
=
Request
(
factory
.
get
(
previous_url
))
def
test_invalid_cursor
(
self
):
queryset
=
self
.
paginate_queryset
(
request
)
request
=
Request
(
factory
.
get
(
'/'
,
{
'cursor'
:
'123'
}))
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
2
,
3
,
4
,
5
]
with
pytest
.
raises
(
exceptions
.
NotFound
):
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
)
previous_url
=
self
.
pagination
.
get_previous_link
()
def
test_cursor_pagination
(
self
):
assert
previous_url
is
None
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
'/'
)
assert
previous
is
None
assert
current
==
[
1
,
1
,
1
,
1
,
1
]
assert
next
==
[
1
,
2
,
3
,
4
,
4
]
class
TestCrazyCursorPagination
:
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
next_url
)
"""
Unit tests for `pagination.CursorPagination`.
"""
def
setup
(
self
):
assert
previous
==
[
1
,
1
,
1
,
1
,
1
]
class
MockObject
(
object
):
assert
current
==
[
1
,
2
,
3
,
4
,
4
]
def
__init__
(
self
,
idx
):
assert
next
==
[
4
,
4
,
5
,
6
,
7
]
self
.
created
=
idx
class
MockQuerySet
(
object
):
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
next_url
)
def
__init__
(
self
,
items
):
self
.
items
=
items
def
filter
(
self
,
created__gt
=
None
,
created__lt
=
None
):
assert
previous
==
[
1
,
2
,
3
,
4
,
4
]
if
created__gt
is
not
None
:
assert
current
==
[
4
,
4
,
5
,
6
,
7
]
return
MockQuerySet
([
assert
next
==
[
7
,
7
,
7
,
7
,
7
]
item
for
item
in
self
.
items
if
item
.
created
>
int
(
created__gt
)
])
assert
created__lt
is
not
None
return
MockQuerySet
([
item
for
item
in
self
.
items
if
item
.
created
<
int
(
created__lt
)
])
def
order_by
(
self
,
ordering
):
if
ordering
.
startswith
(
'-'
):
return
MockQuerySet
(
reversed
(
self
.
items
))
return
self
def
__getitem__
(
self
,
sliced
):
return
self
.
items
[
sliced
]
self
.
pagination
=
pagination
.
CursorPagination
()
self
.
queryset
=
MockQuerySet
([
MockObject
(
idx
)
for
idx
in
[
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
])
def
paginate_queryset
(
self
,
request
):
return
list
(
self
.
pagination
.
paginate_queryset
(
self
.
queryset
,
request
))
def
test_following_cursor_identical_items
(
self
):
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
next_url
)
request
=
Request
(
factory
.
get
(
'/'
))
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
1
,
1
,
1
]
next_url
=
self
.
pagination
.
get_next_link
()
assert
previous
==
[
4
,
4
,
4
,
5
,
6
]
# Paging artifact
assert
next_url
assert
current
==
[
7
,
7
,
7
,
7
,
7
]
assert
next
==
[
7
,
7
,
7
,
8
,
9
]
request
=
Request
(
factory
.
get
(
next_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
next_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
1
,
1
,
1
]
next_url
=
self
.
pagination
.
get_next_link
()
assert
previous
==
[
7
,
7
,
7
,
7
,
7
]
assert
next_url
assert
current
==
[
7
,
7
,
7
,
8
,
9
]
assert
next
==
[
9
,
9
,
9
,
9
,
9
]
request
=
Request
(
factory
.
get
(
next_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
next_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
2
,
3
,
4
]
next_url
=
self
.
pagination
.
get_next_link
()
assert
previous
==
[
7
,
7
,
7
,
8
,
9
]
assert
next_url
assert
current
==
[
9
,
9
,
9
,
9
,
9
]
assert
next
is
None
request
=
Request
(
factory
.
get
(
next_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
previous_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
5
,
6
,
7
,
8
,
9
]
next_url
=
self
.
pagination
.
get_next_link
()
assert
previous
==
[
7
,
7
,
7
,
7
,
7
]
assert
next_url
is
None
assert
current
==
[
7
,
7
,
7
,
8
,
9
]
assert
next
==
[
9
,
9
,
9
,
9
,
9
]
# Now page back again
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
previous_url
)
previous_url
=
self
.
pagination
.
get_previous_link
()
assert
previous
==
[
4
,
4
,
5
,
6
,
7
]
assert
previous_url
assert
current
==
[
7
,
7
,
7
,
7
,
7
]
assert
next
==
[
8
,
9
,
9
,
9
,
9
]
# Paging artifact
request
=
Request
(
factory
.
get
(
previous_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
previous_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
2
,
3
,
4
]
previous_url
=
self
.
pagination
.
get_previous_link
()
assert
previous
==
[
1
,
2
,
3
,
4
,
4
]
assert
previous_url
assert
current
==
[
4
,
4
,
5
,
6
,
7
]
assert
next
==
[
7
,
7
,
7
,
7
,
7
]
request
=
Request
(
factory
.
get
(
previous_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
previous_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
1
,
1
,
1
]
previous_url
=
self
.
pagination
.
get_previous_link
()
assert
previous
==
[
1
,
1
,
1
,
1
,
1
]
assert
previous_url
assert
current
==
[
1
,
2
,
3
,
4
,
4
]
assert
next
==
[
4
,
4
,
5
,
6
,
7
]
request
=
Request
(
factory
.
get
(
previous_url
))
(
previous
,
current
,
next
,
previous_url
,
next_url
)
=
self
.
get_pages
(
previous_url
)
queryset
=
self
.
paginate_queryset
(
request
)
assert
[
item
.
created
for
item
in
queryset
]
==
[
1
,
1
,
1
,
1
,
1
]
previous_url
=
self
.
pagination
.
get_previous_link
()
assert
previous
is
None
assert
previous_url
is
None
assert
current
==
[
1
,
1
,
1
,
1
,
1
]
assert
next
==
[
1
,
2
,
3
,
4
,
4
]
def
test_get_displayed_page_numbers
():
def
test_get_displayed_page_numbers
():
...
...
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