Revised the pagination utility to handle the empty data. better
This commit is contained in:
parent
a7a432914d
commit
fd63149066
@ -59,15 +59,15 @@ class Redirection(RequestRedirect):
|
|||||||
"""The HTTP code."""
|
"""The HTTP code."""
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_PAGE_SIZE: int = 10
|
||||||
|
"""The default page size."""
|
||||||
|
|
||||||
|
|
||||||
T = t.TypeVar("T")
|
T = t.TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
class Pagination(t.Generic[T]):
|
class Pagination(t.Generic[T]):
|
||||||
"""The pagination utilities"""
|
"""The pagination utility."""
|
||||||
AVAILABLE_PAGE_SIZES: list[int] = [10, 100, 200]
|
|
||||||
"""The available page sizes."""
|
|
||||||
DEFAULT_PAGE_SIZE: int = 10
|
|
||||||
"""The default page size."""
|
|
||||||
|
|
||||||
def __init__(self, items: list[T], is_reversed: bool = False):
|
def __init__(self, items: list[T], is_reversed: bool = False):
|
||||||
"""Constructs the pagination.
|
"""Constructs the pagination.
|
||||||
@ -77,32 +77,75 @@ class Pagination(t.Generic[T]):
|
|||||||
otherwise.
|
otherwise.
|
||||||
:raise Redirection: When the pagination parameters are malformed.
|
:raise Redirection: When the pagination parameters are malformed.
|
||||||
"""
|
"""
|
||||||
|
pagination: AbstractPagination[T] = EmptyPagination[T]() \
|
||||||
|
if len(items) == 0 \
|
||||||
|
else NonEmptyPagination[T](items, is_reversed)
|
||||||
|
self.is_paged: bool = pagination.is_paged
|
||||||
|
"""Whether there should be pagination."""
|
||||||
|
self.list: list[T] = pagination.list
|
||||||
|
"""The items shown in the list"""
|
||||||
|
self.page_links: list[Link] = pagination.page_links
|
||||||
|
"""The pagination links."""
|
||||||
|
self.page_sizes: list[Link] = pagination.page_sizes
|
||||||
|
"""The links to switch the number of items in a page."""
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractPagination(t.Generic[T]):
|
||||||
|
"""An abstract pagination."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Constructs an empty pagination."""
|
||||||
|
self.is_paged: bool = False
|
||||||
|
"""Whether there should be pagination."""
|
||||||
|
self.list: list[T] = []
|
||||||
|
"""The items shown in the list"""
|
||||||
|
self.page_links: list[Link] = []
|
||||||
|
"""The pagination links."""
|
||||||
|
self.page_sizes: list[Link] = []
|
||||||
|
"""The links to switch the number of items in a page."""
|
||||||
|
|
||||||
|
|
||||||
|
class EmptyPagination(AbstractPagination[T]):
|
||||||
|
"""The pagination from empty data."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NonEmptyPagination(AbstractPagination[T]):
|
||||||
|
"""The pagination with real data."""
|
||||||
|
AVAILABLE_PAGE_SIZES: list[int] = [10, 100, 200]
|
||||||
|
"""The available page sizes."""
|
||||||
|
|
||||||
|
def __init__(self, items: list[T], is_reversed: bool = False):
|
||||||
|
"""Constructs the pagination.
|
||||||
|
|
||||||
|
:param items: The items.
|
||||||
|
:param is_reversed: True if the default page is the last page, or False
|
||||||
|
otherwise.
|
||||||
|
:raise Redirection: When the pagination parameters are malformed.
|
||||||
|
"""
|
||||||
|
super().__init__()
|
||||||
self.__current_uri: str = request.full_path if request.query_string \
|
self.__current_uri: str = request.full_path if request.query_string \
|
||||||
else request.path
|
else request.path
|
||||||
"""The current URI."""
|
"""The current URI."""
|
||||||
self.__items: list[T] = items
|
|
||||||
"""All the items."""
|
|
||||||
self.__is_reversed: bool = is_reversed
|
self.__is_reversed: bool = is_reversed
|
||||||
"""Whether the default page is the last page."""
|
"""Whether the default page is the last page."""
|
||||||
self.__page_size: int = self.__get_page_size()
|
self.__page_size: int = self.__get_page_size()
|
||||||
"""The number of items in a page."""
|
"""The number of items in a page."""
|
||||||
self.__total_pages: int = 0 if len(items) == 0 \
|
self.__total_pages: int = int((len(items) - 1) / self.__page_size) + 1
|
||||||
else int((len(items) - 1) / self.__page_size) + 1
|
|
||||||
"""The total number of pages."""
|
"""The total number of pages."""
|
||||||
self.is_paged: bool = self.__total_pages > 1
|
self.is_paged = self.__total_pages > 1
|
||||||
"""Whether there should be pagination."""
|
self.__default_page_no: int = self.__total_pages \
|
||||||
self.__default_page_no: int = 0
|
if self.__is_reversed else 1
|
||||||
"""The default page number."""
|
"""The default page number."""
|
||||||
self.__page_no: int = 0
|
self.__page_no: int = self.__get_page_no()
|
||||||
"""The current page number."""
|
"""The current page number."""
|
||||||
self.list: list[T] = []
|
lower_bound: int = (self.__page_no - 1) * self.__page_size
|
||||||
"""The items shown in the list"""
|
upper_bound: int = lower_bound + self.__page_size
|
||||||
if self.__total_pages > 0:
|
if upper_bound > len(items):
|
||||||
self.__set_list()
|
upper_bound = len(items)
|
||||||
self.page_links: list[Link] = self.__get_page_links()
|
self.list = items[lower_bound:upper_bound]
|
||||||
"""The pagination links."""
|
self.page_links = self.__get_page_links()
|
||||||
self.page_sizes: list[Link] = self.__get_page_sizes()
|
self.page_sizes = self.__get_page_sizes()
|
||||||
"""The links to switch the number of items in a page."""
|
|
||||||
|
|
||||||
def __get_page_size(self) -> int:
|
def __get_page_size(self) -> int:
|
||||||
"""Returns the page size.
|
"""Returns the page size.
|
||||||
@ -111,29 +154,15 @@ class Pagination(t.Generic[T]):
|
|||||||
:raise Redirection: When the page size is malformed.
|
:raise Redirection: When the page size is malformed.
|
||||||
"""
|
"""
|
||||||
if "page-size" not in request.args:
|
if "page-size" not in request.args:
|
||||||
return self.DEFAULT_PAGE_SIZE
|
return DEFAULT_PAGE_SIZE
|
||||||
try:
|
try:
|
||||||
page_size: int = int(request.args["page-size"])
|
page_size: int = int(request.args["page-size"])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise Redirection(self.__uri_set("page-size", None))
|
raise Redirection(self.__uri_set("page-size", None))
|
||||||
if page_size == self.DEFAULT_PAGE_SIZE or page_size < 1:
|
if page_size == DEFAULT_PAGE_SIZE or page_size < 1:
|
||||||
raise Redirection(self.__uri_set("page-size", None))
|
raise Redirection(self.__uri_set("page-size", None))
|
||||||
return page_size
|
return page_size
|
||||||
|
|
||||||
def __set_list(self) -> None:
|
|
||||||
"""Sets the items to show in the list.
|
|
||||||
|
|
||||||
:return: None.
|
|
||||||
"""
|
|
||||||
self.__default_page_no = self.__total_pages if self.__is_reversed \
|
|
||||||
else 1
|
|
||||||
self.__page_no = self.__get_page_no()
|
|
||||||
lower_bound: int = (self.__page_no - 1) * self.__page_size
|
|
||||||
upper_bound: int = lower_bound + self.__page_size
|
|
||||||
if upper_bound > len(self.__items):
|
|
||||||
upper_bound = len(self.__items)
|
|
||||||
self.list = self.__items[lower_bound:upper_bound]
|
|
||||||
|
|
||||||
def __get_page_no(self) -> int:
|
def __get_page_no(self) -> int:
|
||||||
"""Returns the page number.
|
"""Returns the page number.
|
||||||
|
|
||||||
@ -255,7 +284,7 @@ class Pagination(t.Generic[T]):
|
|||||||
"""
|
"""
|
||||||
if page_size == self.__page_size:
|
if page_size == self.__page_size:
|
||||||
return self.__current_uri
|
return self.__current_uri
|
||||||
if page_size == self.DEFAULT_PAGE_SIZE:
|
if page_size == DEFAULT_PAGE_SIZE:
|
||||||
return self.__uri_set("page-size", None)
|
return self.__uri_set("page-size", None)
|
||||||
return self.__uri_set("page-size", str(page_size))
|
return self.__uri_set("page-size", str(page_size))
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ import httpx
|
|||||||
from flask import Flask, request
|
from flask import Flask, request
|
||||||
|
|
||||||
from accounting.utils.next_url import append_next, inherit_next, or_next
|
from accounting.utils.next_url import append_next, inherit_next, or_next
|
||||||
from accounting.utils.pagination import Pagination
|
from accounting.utils.pagination import Pagination, DEFAULT_PAGE_SIZE
|
||||||
from accounting.utils.query import parse_query_keywords
|
from accounting.utils.query import parse_query_keywords
|
||||||
from test_site import create_app
|
from test_site import create_app
|
||||||
from testlib import get_csrf_token
|
from testlib import get_csrf_token
|
||||||
@ -272,8 +272,7 @@ class PaginationTestCase(unittest.TestCase):
|
|||||||
self.__test_malformed("q=word&page-size=100a&page-no=37&next=%2F",
|
self.__test_malformed("q=word&page-size=100a&page-no=37&next=%2F",
|
||||||
range(1, 691), "q=word&page-no=37&next=%2F")
|
range(1, 691), "q=word&page-no=37&next=%2F")
|
||||||
# A default page size
|
# A default page size
|
||||||
self.__test_malformed("q=word"
|
self.__test_malformed(f"q=word&page-size={DEFAULT_PAGE_SIZE}"
|
||||||
f"&page-size={Pagination.DEFAULT_PAGE_SIZE}"
|
|
||||||
"&page-no=37&next=%2F",
|
"&page-no=37&next=%2F",
|
||||||
range(1, 691), "q=word&page-no=37&next=%2F")
|
range(1, 691), "q=word&page-no=37&next=%2F")
|
||||||
# An invalid page size
|
# An invalid page size
|
||||||
|
Loading…
Reference in New Issue
Block a user