Skip to content

Commit 58dfde7

Browse files
committed
Tweaks for cursor pagination and docs
1 parent f791792 commit 58dfde7

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

docs/api-guide/pagination.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ You can then apply your new style to a view using the `.pagination_class` attrib
5151
Or apply the style globally, using the `DEFAULT_PAGINATION_CLASS` settings key. For example:
5252

5353
REST_FRAMEWORK = {
54-
'DEFAULT_PAGINATION_CLASS': 'apps.core.pagination.StandardResultsSetPagination' }
54+
'DEFAULT_PAGINATION_CLASS': 'apps.core.pagination.StandardResultsSetPagination'
55+
}
5556

5657
---
5758

@@ -163,6 +164,10 @@ Cursor based pagination is more complex than other schemes. It also requires tha
163164

164165
#### Details and limitations
165166

167+
Cursor based pagination requires a specified ordering to be applied to the queryset. This will default to `'-created'`, which requires the model being paged against to have a `'created'` field.
168+
169+
170+
166171
This implementation of cursor pagination uses a smart "position plus offset" style that allows it to properly support not-strictly-unique values as the ordering.
167172

168173
It should be noted that using non-unique values the ordering does introduce the possibility of paging artifacts, where pagination consistency is no longer 100% guaranteed.
@@ -192,7 +197,7 @@ To set these attributes you should override the `CursorPagination` class, and th
192197

193198
* `page_size` = A numeric value indicating the page size. If set, this overrides the `DEFAULT_PAGE_SIZE` setting. Defaults to the same value as the `DEFAULT_PAGE_SIZE` settings key.
194199
* `cursor_query_param` = A string value indicating the name of the "cursor" query parameter. Defaults to `'cursor'`.
195-
* `ordering` = This should be a string, or list of strings, indicating the field against which the cursor based pagination will be applied. For example: `ordering = 'created'`. Any filters on the view which define a `get_ordering` will override this attribute. Defaults to `None`.
200+
* `ordering` = This should be a string, or list of strings, indicating the field against which the cursor based pagination will be applied. For example: `ordering = '-created'`. Any filters on the view which define a `get_ordering` will override this attribute. Defaults to `None`.
196201
* `template` = The name of a template to use when rendering pagination controls in the browsable API. May be overridden to modify the rendering style, or set to `None` to disable HTML pagination controls completely. Defaults to `"rest_framework/pagination/previous_and_next.html"`.
197202

198203
---
@@ -236,7 +241,8 @@ Let's modify the built-in `PageNumberPagination` style, so that instead of inclu
236241

237242
class LinkHeaderPagination(pagination.PageNumberPagination):
238243
def get_paginated_response(self, data):
239-
next_url = self.get_next_link() previous_url = self.get_previous_link()
244+
next_url = self.get_next_link()
245+
previous_url = self.get_previous_link()
240246

241247
if next_url is not None and previous_url is not None:
242248
link = '<{next_url}; rel="next">, <{previous_url}; rel="prev">'

docs/topics/3.1-announcement.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,4 @@ The next focus will be on HTML renderings of API output and will include:
203203
This will either be made as a single 3.2 release, or split across two separate releases, with the HTML forms and filter controls coming in 3.2, and the admin-style interface coming in a 3.3 release.
204204

205205
[custom-exception-handler]: ../api-guide/exceptions.md#custom-exception-handling
206+
[pagination]: ../api-guide/pagination.md

docs/topics/release-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ You can determine your currently installed version using `pip freeze`:
4040

4141
## 3.0.x series
4242

43+
### 3.1.0
44+
45+
**Date**: [5th March 2015][3.1.0-milestone].
46+
47+
For full details see the [3.1 release announcement](3.1-announcement.md).
4348

4449
### 3.0.5
4550

rest_framework/pagination.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,19 @@ def _decode_cursor(encoded):
131131
"""
132132
Given a string representing an encoded cursor, return a `Cursor` instance.
133133
"""
134+
135+
# The offset in the cursor is used in situations where we have a
136+
# nearly-unique index. (Eg millisecond precision creation timestamps)
137+
# We guard against malicious users attempting to cause expensive database
138+
# queries, by having a hard cap on the maximum possible size of the offset.
139+
OFFSET_CUTOFF = 1000
140+
134141
try:
135142
querystring = b64decode(encoded.encode('ascii')).decode('ascii')
136143
tokens = urlparse.parse_qs(querystring, keep_blank_values=True)
137144

138145
offset = tokens.get('o', ['0'])[0]
139-
offset = _positive_int(offset)
146+
offset = _positive_int(offset, cutoff=OFFSET_CUTOFF)
140147

141148
reverse = tokens.get('r', ['0'])[0]
142149
reverse = bool(int(reverse))
@@ -472,14 +479,15 @@ def to_html(self):
472479

473480

474481
class CursorPagination(BasePagination):
475-
# Determine how/if True, False and None positions work - do the string
476-
# encodings work with Django queryset filters?
477-
# Consider a max offset cap.
478-
# Tidy up the `get_ordering` API (eg remove queryset from it)
482+
"""
483+
The cursor pagination implementation is neccessarily complex.
484+
For an overview of the position/offset style we use, see this post:
485+
http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/
486+
"""
479487
cursor_query_param = 'cursor'
480488
page_size = api_settings.PAGE_SIZE
481489
invalid_cursor_message = _('Invalid cursor')
482-
ordering = None
490+
ordering = '-created'
483491
template = 'rest_framework/pagination/previous_and_next.html'
484492

485493
def paginate_queryset(self, queryset, request, view=None):
@@ -680,12 +688,12 @@ def get_ordering(self, request, queryset, view):
680688
)
681689
)
682690
else:
683-
# The default case is to check for an `ordering` attribute,
684-
# first on the view instance, and then on this pagination instance.
685-
ordering = getattr(view, 'ordering', getattr(self, 'ordering', None))
691+
# The default case is to check for an `ordering` attribute
692+
# on this pagination instance.
693+
ordering = self.ordering
686694
assert ordering is not None, (
687695
'Using cursor pagination, but no ordering attribute was declared '
688-
'on the view or on the pagination class.'
696+
'on the pagination class.'
689697
)
690698

691699
assert isinstance(ordering, (six.string_types, list, tuple)), (

0 commit comments

Comments
 (0)