From 90a8229db9afa76dde99c861e2927a4457089ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sat, 4 Feb 2023 10:37:39 +0800 Subject: [PATCH] Revised the Pagination so that the page size and page number that are the same as the default values are redirected and removed, too. --- src/accounting/utils/pagination.py | 7 ++++++- tests/test_utils.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/accounting/utils/pagination.py b/src/accounting/utils/pagination.py index ef769fb..5c4afbc 100644 --- a/src/accounting/utils/pagination.py +++ b/src/accounting/utils/pagination.py @@ -115,9 +115,12 @@ class Pagination(t.Generic[T]): if "page-size" not in request.args: return self.DEFAULT_PAGE_SIZE try: - return int(request.args["page-size"]) + page_size: int = int(request.args["page-size"]) except ValueError: raise Redirection(self.__uri_set("page-size", None)) + if page_size == self.DEFAULT_PAGE_SIZE: + raise Redirection(self.__uri_set("page-size", None)) + return page_size def __set_list(self) -> None: """Sets the items to show in the list. @@ -149,6 +152,8 @@ class Pagination(t.Generic[T]): page_no: int = int(request.args["page-no"]) except ValueError: raise Redirection(self.__uri_set("page-no", None)) + if page_no == self.__default_page_no: + raise Redirection(self.__uri_set("page-no", None)) if page_no < 1: if not self.__is_reversed: raise Redirection(self.__uri_set("page-no", None)) diff --git a/tests/test_utils.py b/tests/test_utils.py index 69c431e..3058339 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -271,9 +271,21 @@ class PaginationTestCase(unittest.TestCase): # A malformed page size self.__test_malformed("q=word&page-size=100a&page-no=37&next=%2F", range(1, 691), "q=word&page-no=37&next=%2F") + # A default page size + self.__test_malformed("q=word" + f"&page-size={Pagination.DEFAULT_PAGE_SIZE}" + "&page-no=37&next=%2F", + range(1, 691), "q=word&page-no=37&next=%2F") # A malformed page number self.__test_malformed("q=word&page-size=15&page-no=37a&next=%2F", range(1, 691), "q=word&page-size=15&next=%2F") + # A default page number + self.__test_malformed("q=word&page-size=15&page-no=1&next=%2F", + range(1, 691), "q=word&page-size=15&next=%2F") + # A default page number, on a reversed list + self.__test_malformed("q=word&page-size=15&page-no=46&next=%2F", + range(1, 691), "q=word&page-size=15&next=%2F", + is_reversed=True) # A page number beyond the last page self.__test_malformed("q=word&page-size=15&page-no=100&next=%2F", range(1, 691),