Added get_previous_cursor() and get_next_cursor().

This commit is contained in:
Loïc Bistuer 2015-07-15 11:14:35 +07:00
parent 6b08e97b6a
commit 443b2aafd9

View File

@ -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('-'))