def decode_cursor(self, request):
"""
Given a request with a cursor, return a `Cursor` instance.
Differs from the standard CursorPagination to handle a tuple in the
position field.
"""
# Determine if we have a cursor, and if so then decode it.
encoded = request.query_params.get(self.cursor_query_param)
if encoded is None:
return None
try:
querystring = b64decode(encoded.encode('ascii')).decode('ascii')
tokens = urlparse.parse_qs(querystring, keep_blank_values=True)
offset = tokens.get('o', ['0'])[0]
# This was hard-coded until Django REST Framework 3.4.0.
try:
offset_cutoff = self.offset_cutoff
except AttributeError:
offset_cutoff = 1000
offset = _positive_int(offset, cutoff=offset_cutoff)
reverse = tokens.get('r', ['0'])[0]
reverse = bool(int(reverse))
# The difference. Don't get just the 0th entry: get all entries.
position = tokens.get('p', None)
except (TypeError, ValueError):
raise NotFound(self.invalid_cursor_message)
return Cursor(offset=offset, reverse=reverse, position=position)
评论列表
文章目录