From 81439bb646c7bf6329bd36685aa76a4a642a92b7 Mon Sep 17 00:00:00 2001 From: Wellington Adriano Nicoletti de Abreu Date: Sat, 1 Jul 2023 20:38:21 -0300 Subject: [PATCH] feat: added last and first page links to PageNumberPagination --- rest_framework/pagination.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 7303890b0..55b030728 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -231,8 +231,10 @@ class PageNumberPagination(BasePagination): def get_paginated_response(self, data): return Response({ 'count': self.page.paginator.count, + 'last': self.get_last_link(), 'next': self.get_next_link(), 'previous': self.get_previous_link(), + 'first': self.get_first_link(), 'results': data, }) @@ -272,6 +274,17 @@ class PageNumberPagination(BasePagination): ) return self.page_size + def get_last_link(self): + if not self.page.has_next(): + return None + next_page_number = self.page.next_page_number() + last_page_number = self.page.paginator.num_pages + # if next and last are the same it does not need to build the last url + if next_page_number == last_page_number: + return None + url = self.request.build_absolute_uri() + return replace_query_param(url, self.page_query_param, last_page_number) + def get_next_link(self): if not self.page.has_next(): return None @@ -288,6 +301,13 @@ class PageNumberPagination(BasePagination): return remove_query_param(url, self.page_query_param) return replace_query_param(url, self.page_query_param, page_number) + def get_first_link(self): + # if the current number is greater than 2, then it makes sense to build the first url + if self.page.number <= 2: + return None + url = self.request.build_absolute_uri() + return replace_query_param(url, self.page_query_param, 1) + def get_html_context(self): base_url = self.request.build_absolute_uri()