From 443b2aafd92cfbe160c17b20531b0e797afc8abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bistuer?= Date: Wed, 15 Jul 2015 11:14:35 +0700 Subject: [PATCH] Added get_previous_cursor() and get_next_cursor(). --- rest_framework/pagination.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index a14141d3c..b005a2cb2 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -534,7 +534,7 @@ class CursorPagination(BasePagination): def get_page_size(self, request): return self.page_size - def get_next_link(self): + def get_next_cursor(self): if not self.has_next: return None @@ -579,10 +579,15 @@ class CursorPagination(BasePagination): offset = self.cursor.offset + self.page_size position = self.previous_position - cursor = Cursor(offset=offset, reverse=False, position=position) - return self.encode_cursor(cursor) + return Cursor(offset=offset, reverse=False, position=position) - def get_previous_link(self): + def get_next_link(self): + cursor = self.get_next_cursor() + if not cursor: + return None + return replace_query_param(self.base_url, self.cursor_query_param, self.encode_cursor(cursor)) + + def get_previous_cursor(self): if not self.has_previous: return None @@ -627,8 +632,13 @@ class CursorPagination(BasePagination): offset = 0 position = self.next_position - cursor = Cursor(offset=offset, reverse=True, position=position) - return self.encode_cursor(cursor) + return Cursor(offset=offset, reverse=True, position=position) + + def get_previous_link(self): + cursor = self.get_previous_cursor() + if not cursor: + return None + return replace_query_param(self.base_url, self.cursor_query_param, self.encode_cursor(cursor)) def get_ordering(self, request, queryset, view): """ @@ -714,8 +724,7 @@ class CursorPagination(BasePagination): tokens['p'] = cursor.position querystring = urlparse.urlencode(tokens, doseq=True) - encoded = b64encode(querystring.encode('ascii')).decode('ascii') - return replace_query_param(self.base_url, self.cursor_query_param, encoded) + return b64encode(querystring.encode('ascii')).decode('ascii') def _get_position_from_instance(self, instance, ordering): attr = getattr(instance, ordering[0].lstrip('-'))