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
408261ee
Commit
408261ee
authored
Jan 22, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support ordering attribute either on view or on pagination class for CursorPagination
parent
83a82b44
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
5 deletions
+20
-5
rest_framework/pagination.py
+19
-5
tests/test_pagination.py
+1
-0
No files found.
rest_framework/pagination.py
View file @
408261ee
...
...
@@ -427,16 +427,16 @@ class LimitOffsetPagination(BasePagination):
class
CursorPagination
(
BasePagination
):
# Support case where ordering is already negative
# Support tuple orderings
# Support usage with OrderingFilter
# Determine how/if True, False and None positions work
cursor_query_param
=
'cursor'
page_size
=
api_settings
.
PAGINATE_BY
invalid_cursor_message
=
_
(
'Invalid cursor'
)
ordering
=
None
def
paginate_queryset
(
self
,
queryset
,
request
,
view
=
None
):
self
.
base_url
=
request
.
build_absolute_uri
()
self
.
ordering
=
self
.
get_ordering
()
self
.
ordering
=
self
.
get_ordering
(
view
)
# Determine if we have a cursor, and if so then decode it.
encoded
=
request
.
query_params
.
get
(
self
.
cursor_query_param
)
...
...
@@ -600,11 +600,25 @@ class CursorPagination(BasePagination):
encoded
=
_encode_cursor
(
cursor
)
return
replace_query_param
(
self
.
base_url
,
self
.
cursor_query_param
,
encoded
)
def
get_ordering
(
self
):
def
get_ordering
(
self
,
view
):
"""
Return a tuple of strings, that may be used in an `order_by` method.
"""
return
(
'created'
,)
ordering
=
getattr
(
view
,
'ordering'
,
getattr
(
self
,
'ordering'
,
None
))
assert
ordering
is
not
None
,
(
'Using cursor pagination, but no ordering attribute was declared '
'on the view or on the pagination class.'
)
assert
isinstance
(
ordering
,
(
six
.
string_types
,
list
,
tuple
)),
(
'Invalid ordering. Expected string or tuple, but got {type}'
.
format
(
type
=
type
(
ordering
)
.
__name__
)
)
if
isinstance
(
ordering
,
six
.
string_types
):
return
(
ordering
,)
return
ordering
def
_get_position_from_instance
(
self
,
instance
,
ordering
):
attr
=
getattr
(
instance
,
ordering
[
0
])
...
...
tests/test_pagination.py
View file @
408261ee
...
...
@@ -459,6 +459,7 @@ class TestCursorPagination:
class
ExamplePagination
(
pagination
.
CursorPagination
):
page_size
=
5
ordering
=
'created'
self
.
pagination
=
ExamplePagination
()
self
.
queryset
=
MockQuerySet
([
...
...
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