2023-01-29 22:28:27 +08:00
|
|
|
# The Mia! Accounting Flask Project.
|
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/25
|
|
|
|
|
|
|
|
# Copyright (c) 2023 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""The pagination utilities.
|
|
|
|
|
|
|
|
This module should not import any other module from the application.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import typing as t
|
|
|
|
from urllib.parse import urlparse, parse_qsl, urlencode, urlunparse, \
|
|
|
|
ParseResult
|
|
|
|
|
|
|
|
from flask import request
|
2023-02-04 09:34:52 +08:00
|
|
|
from werkzeug.routing import RequestRedirect
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
from accounting.locale import gettext
|
|
|
|
|
|
|
|
|
2023-02-04 10:18:22 +08:00
|
|
|
class Link:
|
|
|
|
"""A link."""
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
def __init__(self, text: str, uri: str | None = None,
|
|
|
|
is_current: bool = False, is_for_mobile: bool = False):
|
|
|
|
"""Constructs the link.
|
|
|
|
|
|
|
|
:param text: The link text.
|
|
|
|
:param uri: The link URI, or None if there is no link.
|
|
|
|
:param is_current: True if the page is the current page, or False
|
|
|
|
otherwise.
|
|
|
|
:param is_for_mobile: True if the page should be shown on small
|
|
|
|
screens, or False otherwise.
|
|
|
|
"""
|
|
|
|
self.text: str = text
|
|
|
|
"""The link text"""
|
|
|
|
self.uri: str | None = uri
|
|
|
|
"""The link URI, or None if there is no link."""
|
|
|
|
self.is_current: bool = is_current
|
|
|
|
"""Whether the link is the current page."""
|
|
|
|
self.is_for_mobile: bool = is_for_mobile
|
|
|
|
"""Whether the link should be shown on mobile screens."""
|
|
|
|
|
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
class Redirection(RequestRedirect):
|
|
|
|
"""The redirection."""
|
|
|
|
code = 302
|
|
|
|
"""The HTTP code."""
|
|
|
|
|
|
|
|
|
2023-01-29 22:28:27 +08:00
|
|
|
T = t.TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
class Pagination(t.Generic[T]):
|
|
|
|
"""The pagination utilities"""
|
|
|
|
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):
|
|
|
|
"""Constructs the pagination.
|
|
|
|
|
|
|
|
:param items: The items.
|
|
|
|
:param is_reversed: True if the default page is the last page, or False
|
|
|
|
otherwise.
|
2023-02-04 10:51:07 +08:00
|
|
|
:raise Redirection: When the pagination parameters are malformed.
|
2023-01-29 22:28:27 +08:00
|
|
|
"""
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__current_uri: str = request.full_path if request.query_string \
|
|
|
|
else request.path
|
|
|
|
"""The current URI."""
|
2023-01-29 22:28:27 +08:00
|
|
|
self.__items: list[T] = items
|
|
|
|
"""All the items."""
|
|
|
|
self.__is_reversed: bool = is_reversed
|
|
|
|
"""Whether the default page is the last page."""
|
2023-02-04 09:34:52 +08:00
|
|
|
self.page_size: int = self.__get_page_size()
|
2023-01-29 22:28:27 +08:00
|
|
|
"""The number of items in a page."""
|
|
|
|
self.__total_pages: int = 0 if len(items) == 0 \
|
|
|
|
else int((len(items) - 1) / self.page_size) + 1
|
|
|
|
"""The total number of pages."""
|
2023-02-04 10:26:28 +08:00
|
|
|
self.is_paged: bool = self.__total_pages > 1
|
2023-01-29 22:28:27 +08:00
|
|
|
"""Whether there should be pagination."""
|
|
|
|
self.__default_page_no: int = 0
|
|
|
|
"""The default page number."""
|
|
|
|
self.page_no: int = 0
|
|
|
|
"""The current page number."""
|
|
|
|
self.list: list[T] = []
|
|
|
|
"""The items shown in the list"""
|
|
|
|
if self.__total_pages > 0:
|
|
|
|
self.__set_list()
|
|
|
|
"""The base URI parameters."""
|
2023-02-04 10:18:22 +08:00
|
|
|
self.page_links: list[Link] = self.__get_page_links()
|
2023-01-29 22:28:27 +08:00
|
|
|
"""The pagination links."""
|
2023-02-04 10:18:22 +08:00
|
|
|
self.page_sizes: list[Link] = self.__get_page_sizes()
|
2023-01-29 22:28:27 +08:00
|
|
|
"""The links to switch the number of items in a page."""
|
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
def __get_page_size(self) -> int:
|
|
|
|
"""Returns the page size.
|
|
|
|
|
|
|
|
:return: The page size.
|
|
|
|
:raise Redirection: When the page size is malformed.
|
|
|
|
"""
|
|
|
|
if "page-size" not in request.args:
|
|
|
|
return self.DEFAULT_PAGE_SIZE
|
|
|
|
try:
|
2023-02-04 10:37:39 +08:00
|
|
|
page_size: int = int(request.args["page-size"])
|
2023-02-04 09:34:52 +08:00
|
|
|
except ValueError:
|
|
|
|
raise Redirection(self.__uri_set("page-size", None))
|
2023-02-04 10:37:39 +08:00
|
|
|
if page_size == self.DEFAULT_PAGE_SIZE:
|
|
|
|
raise Redirection(self.__uri_set("page-size", None))
|
|
|
|
return page_size
|
2023-02-04 09:34:52 +08:00
|
|
|
|
2023-01-29 22:28:27 +08:00
|
|
|
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
|
2023-02-04 09:34:52 +08:00
|
|
|
self.page_no = self.__get_page_no()
|
2023-01-29 22:28:27 +08:00
|
|
|
if self.page_no < 1:
|
|
|
|
self.page_no = 1
|
|
|
|
if self.page_no > self.__total_pages:
|
|
|
|
self.page_no = self.__total_pages
|
|
|
|
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]
|
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
def __get_page_no(self) -> int:
|
|
|
|
"""Returns the page number.
|
|
|
|
|
|
|
|
:return: The page number.
|
|
|
|
:raise Redirection: When the page number is malformed.
|
|
|
|
"""
|
|
|
|
if "page-no" not in request.args:
|
|
|
|
return self.__default_page_no
|
|
|
|
try:
|
|
|
|
page_no: int = int(request.args["page-no"])
|
|
|
|
except ValueError:
|
|
|
|
raise Redirection(self.__uri_set("page-no", None))
|
2023-02-04 10:37:39 +08:00
|
|
|
if page_no == self.__default_page_no:
|
|
|
|
raise Redirection(self.__uri_set("page-no", None))
|
2023-02-04 09:34:52 +08:00
|
|
|
if page_no < 1:
|
|
|
|
if not self.__is_reversed:
|
|
|
|
raise Redirection(self.__uri_set("page-no", None))
|
|
|
|
raise Redirection(self.__uri_set("page-no", "1"))
|
|
|
|
if page_no > self.__total_pages:
|
|
|
|
if self.__is_reversed:
|
|
|
|
raise Redirection(self.__uri_set("page-no", None))
|
|
|
|
raise Redirection(self.__uri_set("page-no",
|
|
|
|
str(self.__total_pages)))
|
|
|
|
return page_no
|
|
|
|
|
2023-02-04 10:18:22 +08:00
|
|
|
def __get_page_links(self) -> list[Link]:
|
2023-01-29 22:28:27 +08:00
|
|
|
"""Returns the page links in the pagination navigation.
|
|
|
|
|
|
|
|
:return: The page links in the pagination navigation.
|
|
|
|
"""
|
|
|
|
if self.__total_pages < 2:
|
|
|
|
return []
|
|
|
|
uri: str | None
|
2023-02-04 10:18:22 +08:00
|
|
|
links: list[Link] = []
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The previous page.
|
|
|
|
uri = None if self.page_no == 1 else self.__uri_page(self.page_no - 1)
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(gettext("Previous"), uri, is_for_mobile=True))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The first page.
|
|
|
|
if self.page_no > 1:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link("1", self.__uri_page(1)))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The eclipse of the previous pages.
|
|
|
|
if self.page_no - 3 == 2:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no - 3),
|
|
|
|
self.__uri_page(self.page_no - 3)))
|
2023-01-29 22:28:27 +08:00
|
|
|
elif self.page_no - 3 > 2:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link("…"))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The previous two pages.
|
|
|
|
if self.page_no - 2 > 1:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no - 2),
|
|
|
|
self.__uri_page(self.page_no - 2)))
|
2023-01-29 22:28:27 +08:00
|
|
|
if self.page_no - 1 > 1:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no - 1),
|
|
|
|
self.__uri_page(self.page_no - 1)))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The current page.
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no), self.__uri_page(self.page_no),
|
|
|
|
is_current=True))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The next two pages.
|
|
|
|
if self.page_no + 1 < self.__total_pages:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no + 1),
|
|
|
|
self.__uri_page(self.page_no + 1)))
|
2023-01-29 22:28:27 +08:00
|
|
|
if self.page_no + 2 < self.__total_pages:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no + 2),
|
|
|
|
self.__uri_page(self.page_no + 2)))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The eclipse of the next pages.
|
|
|
|
if self.page_no + 3 == self.__total_pages - 1:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.page_no + 3),
|
|
|
|
self.__uri_page(self.page_no + 3)))
|
2023-01-29 22:28:27 +08:00
|
|
|
elif self.page_no + 3 < self.__total_pages - 1:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link("…"))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The last page.
|
|
|
|
if self.page_no < self.__total_pages:
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(str(self.__total_pages),
|
|
|
|
self.__uri_page(self.__total_pages)))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
# The next page.
|
|
|
|
uri = None if self.page_no == self.__total_pages \
|
|
|
|
else self.__uri_page(self.page_no + 1)
|
2023-02-04 10:18:22 +08:00
|
|
|
links.append(Link(gettext("Next"), uri, is_for_mobile=True))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
return links
|
|
|
|
|
|
|
|
def __uri_page(self, page_no: int) -> str:
|
|
|
|
"""Returns the URI of a page.
|
|
|
|
|
|
|
|
:param page_no: The page number.
|
|
|
|
:return: The URI of the page.
|
|
|
|
"""
|
2023-02-04 10:49:35 +08:00
|
|
|
if page_no == self.page_no:
|
|
|
|
return self.__current_uri
|
|
|
|
if page_no == self.__default_page_no:
|
|
|
|
return self.__uri_set("page-no", None)
|
|
|
|
return self.__uri_set("page-no", str(page_no))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
2023-02-04 10:18:22 +08:00
|
|
|
def __get_page_sizes(self) -> list[Link]:
|
2023-01-29 22:28:27 +08:00
|
|
|
"""Returns the available page sizes.
|
|
|
|
|
|
|
|
:return: The available page sizes.
|
|
|
|
"""
|
2023-02-04 10:18:22 +08:00
|
|
|
return [Link(str(x), self.__uri_size(x),
|
|
|
|
is_current=x == self.page_size)
|
2023-01-29 22:28:27 +08:00
|
|
|
for x in self.AVAILABLE_PAGE_SIZES]
|
|
|
|
|
|
|
|
def __uri_size(self, page_size: int) -> str:
|
|
|
|
"""Returns the URI of a page size.
|
|
|
|
|
|
|
|
:param page_size: The page size.
|
|
|
|
:return: The URI of the page size.
|
|
|
|
"""
|
|
|
|
if page_size == self.page_size:
|
|
|
|
return self.__current_uri
|
2023-02-04 10:49:35 +08:00
|
|
|
if page_size == self.DEFAULT_PAGE_SIZE:
|
|
|
|
return self.__uri_set("page-size", None)
|
|
|
|
return self.__uri_set("page-size", str(page_size))
|
2023-01-29 22:28:27 +08:00
|
|
|
|
2023-02-04 10:49:35 +08:00
|
|
|
def __uri_set(self, name: str, value: str | None) -> str:
|
|
|
|
"""Raises current URI with a parameter set.
|
2023-01-29 22:28:27 +08:00
|
|
|
|
2023-02-04 10:49:35 +08:00
|
|
|
:param name: The name of the parameter.
|
|
|
|
:param value: The value, or None to remove the parameter.
|
|
|
|
:return: The URI with the parameter set.
|
2023-01-29 22:28:27 +08:00
|
|
|
"""
|
2023-02-04 10:49:35 +08:00
|
|
|
uri_p: ParseResult = urlparse(self.__current_uri)
|
|
|
|
params: list[tuple[str, str]] = parse_qsl(uri_p.query)
|
|
|
|
|
|
|
|
# Try to keep the position of the parameter.
|
|
|
|
i: int = 0
|
|
|
|
is_found: bool = False
|
|
|
|
while i < len(params):
|
|
|
|
if params[i][0] == name:
|
|
|
|
if is_found or value is None:
|
|
|
|
params = params[:i] + params[i + 1:]
|
|
|
|
continue
|
|
|
|
params[i] = (name, value)
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
parts: list[str] = list(uri_p)
|
|
|
|
parts[4] = urlencode(params)
|
2023-01-29 22:28:27 +08:00
|
|
|
return urlunparse(parts)
|