Revised the Pagination so that the page size and page number that are the same as the default values are redirected and removed, too.

This commit is contained in:
依瑪貓 2023-02-04 10:37:39 +08:00
parent 8be44ccf5f
commit 90a8229db9
2 changed files with 18 additions and 1 deletions

View File

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

View File

@ -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),