2020-07-07 20:49:31 +08:00
|
|
|
# The core application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/1
|
|
|
|
|
|
|
|
# Copyright (c) 2020 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 views of the mia core application.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-07-01 23:35:23 +08:00
|
|
|
import urllib.parse
|
|
|
|
|
2020-07-07 22:18:40 +08:00
|
|
|
from django.utils.translation import pgettext
|
|
|
|
|
2020-07-01 23:35:23 +08:00
|
|
|
|
|
|
|
class UrlBuilder:
|
|
|
|
"""The URL builder.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
base_path (str): the base path
|
|
|
|
params (list[Param]): The query parameters
|
|
|
|
"""
|
|
|
|
base_path = None
|
|
|
|
params = []
|
|
|
|
|
|
|
|
def __init__(self, start_url):
|
|
|
|
"""Constructs a new URL builder.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
start_url (str): The URL to start with
|
|
|
|
"""
|
|
|
|
pos = start_url.find("?")
|
|
|
|
if pos == -1:
|
|
|
|
self.base_path = start_url
|
|
|
|
return
|
|
|
|
self.base_path = start_url[:pos]
|
|
|
|
self.params = []
|
|
|
|
for piece in start_url[pos+1:].split("&"):
|
|
|
|
pos = piece.find("=")
|
|
|
|
name = urllib.parse.unquote(piece[:pos])
|
|
|
|
value = urllib.parse.unquote(piece[pos+1:])
|
|
|
|
self.params.append(self.Param(name, value))
|
|
|
|
|
|
|
|
def add_param(self, name, value):
|
|
|
|
"""Adds a query parameter.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): The parameter name
|
|
|
|
value (str): The parameter value
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
UrlBuilder: The URL builder itself, with the parameter
|
|
|
|
modified.
|
2020-07-01 23:35:23 +08:00
|
|
|
"""
|
|
|
|
self.params.append(self.Param(name, value))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def del_param(self, name):
|
|
|
|
"""Removes a query parameter.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): The parameter name
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
UrlBuilder: The URL builder itself, with the parameter
|
|
|
|
modified.
|
2020-07-01 23:35:23 +08:00
|
|
|
"""
|
|
|
|
self.params = [x for x in self.params if x.name != name]
|
|
|
|
return self
|
|
|
|
|
|
|
|
def set_param(self, name, value):
|
|
|
|
"""Sets a query parameter. The current parameters with the
|
|
|
|
same name will be replaced.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): The parameter name
|
|
|
|
value (str): The parameter value
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
UrlBuilder: The URL builder itself, with the parameter
|
|
|
|
modified.
|
2020-07-01 23:35:23 +08:00
|
|
|
"""
|
|
|
|
return self.del_param(name).add_param(name, value)
|
|
|
|
|
2020-07-07 22:18:40 +08:00
|
|
|
def clone(self):
|
|
|
|
"""Returns a copy of this URL builder.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
UrlBuilder: A copy of this URL builder.
|
|
|
|
"""
|
|
|
|
another = UrlBuilder(self.base_path)
|
|
|
|
another.params = [
|
|
|
|
self.Param(x.name, x.value) for x in self.params]
|
|
|
|
return another
|
|
|
|
|
2020-07-01 23:35:23 +08:00
|
|
|
def __str__(self):
|
2020-07-07 21:06:11 +08:00
|
|
|
if len(self.params) == 0:
|
|
|
|
return self.base_path
|
|
|
|
return self.base_path + "?" + "&".join([
|
|
|
|
str(x) for x in self.params])
|
2020-07-01 23:35:23 +08:00
|
|
|
|
|
|
|
class Param:
|
|
|
|
"""A query parameter.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name (str): The parameter name
|
|
|
|
value (str): The parameter value
|
|
|
|
"""
|
|
|
|
name = None
|
|
|
|
value = None
|
|
|
|
|
|
|
|
def __init__(self, name, value):
|
|
|
|
"""Constructs a new query parameter
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): The parameter name
|
|
|
|
value (str): The parameter value
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this query
|
|
|
|
parameter.
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
str: The string representation of this query
|
|
|
|
parameter
|
2020-07-01 23:35:23 +08:00
|
|
|
"""
|
|
|
|
return "%s=%s" % (
|
|
|
|
urllib.parse.quote(self.name),
|
|
|
|
urllib.parse.quote(self.value))
|
2020-07-07 20:54:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Pagination:
|
|
|
|
"""The pagination.
|
|
|
|
|
|
|
|
Args:
|
2020-07-07 22:18:40 +08:00
|
|
|
request (HttpRequest): The request
|
2020-07-07 21:07:05 +08:00
|
|
|
records (list[Model]): All the records
|
2020-07-07 20:54:35 +08:00
|
|
|
page_no (int): The specified page number
|
|
|
|
page_size (int): The specified number of records per page
|
2020-07-07 22:18:40 +08:00
|
|
|
is_reversed (bool): Whether we should display the last
|
|
|
|
page first
|
2020-07-07 20:54:35 +08:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
PageNoOutOfRangeError: if the specified page number is out
|
|
|
|
of range or is redundant.
|
|
|
|
|
|
|
|
Attributes:
|
2020-07-07 22:49:37 +08:00
|
|
|
current_url (bool): The current request URL
|
2020-07-07 22:18:40 +08:00
|
|
|
is_reversed (bool): Whether we should display the last
|
|
|
|
page first
|
|
|
|
page_size (int): The page size.
|
|
|
|
total_pages (int): The total number of pages available.
|
|
|
|
is_paged (bool): Whether there are more than one page.
|
|
|
|
page_no (int): The current page number.
|
|
|
|
records (list[Model]): The records in the current page.
|
|
|
|
links (list[Link]): The navigation links in the pagination
|
|
|
|
bar.
|
2020-07-07 20:54:35 +08:00
|
|
|
"""
|
2020-07-07 22:49:37 +08:00
|
|
|
current_url = None
|
2020-07-07 22:18:40 +08:00
|
|
|
is_reversed = False
|
2020-07-07 20:54:35 +08:00
|
|
|
page_size = None
|
2020-07-07 22:18:40 +08:00
|
|
|
total_pages = None
|
|
|
|
is_paged = None
|
|
|
|
page_no = None
|
2020-07-07 21:04:07 +08:00
|
|
|
records = None
|
2020-07-07 20:54:35 +08:00
|
|
|
|
|
|
|
DEFAULT_PAGE_SIZE = 10
|
|
|
|
|
2020-07-07 22:49:37 +08:00
|
|
|
def __init__(self, current_url, records, page_no,
|
2020-07-07 22:18:40 +08:00
|
|
|
page_size, is_reversed=False):
|
2020-07-07 22:49:37 +08:00
|
|
|
self.current_url = current_url
|
2020-07-07 22:18:40 +08:00
|
|
|
self.is_reversed = is_reversed
|
2020-07-07 20:54:35 +08:00
|
|
|
self.page_size = page_size \
|
|
|
|
if page_size is not None \
|
|
|
|
else self.DEFAULT_PAGE_SIZE
|
2020-07-07 22:18:40 +08:00
|
|
|
self.total_pages = int(
|
|
|
|
(len(records) - 1) / self.page_size) + 1
|
|
|
|
self.is_paged = self.total_pages > 1
|
|
|
|
if not self.is_paged:
|
|
|
|
self.page_no = 1
|
|
|
|
self.records = records
|
2020-07-07 22:49:37 +08:00
|
|
|
self._links = []
|
2020-07-07 22:18:40 +08:00
|
|
|
return
|
|
|
|
default_page = 1 if not is_reversed else self.total_pages
|
2020-07-07 20:54:35 +08:00
|
|
|
if page_no == default_page:
|
2020-07-07 20:56:01 +08:00
|
|
|
raise PageNoOutOfRangeException()
|
2020-07-07 20:54:35 +08:00
|
|
|
self.page_no = page_no \
|
|
|
|
if page_no is not None \
|
|
|
|
else default_page
|
2020-07-07 22:18:40 +08:00
|
|
|
if self.page_no > self.total_pages:
|
2020-07-07 20:56:01 +08:00
|
|
|
raise PageNoOutOfRangeException()
|
2020-07-07 21:04:07 +08:00
|
|
|
start_no = self.page_size * (self.page_no - 1)
|
|
|
|
self.records = records[start_no:start_no + self.page_size]
|
2020-07-07 22:18:40 +08:00
|
|
|
|
2020-07-07 22:49:37 +08:00
|
|
|
_links = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def links(self):
|
2020-07-07 22:52:00 +08:00
|
|
|
"""Returns the navigation links of the pagination bar."""
|
2020-07-07 22:49:37 +08:00
|
|
|
if self._links is None:
|
|
|
|
base_url = UrlBuilder(self.current_url).del_param("page")
|
|
|
|
self._links = []
|
|
|
|
# The previous page
|
|
|
|
link = self.Link()
|
|
|
|
link.title = pgettext("Pagination|", "Previous")
|
|
|
|
if self.page_no > 1:
|
|
|
|
if self.page_no - 1 == 1:
|
|
|
|
if not self.is_reversed:
|
|
|
|
link.url = str(base_url)
|
|
|
|
else:
|
|
|
|
link.url = str(base_url.clone().add_param(
|
|
|
|
"page", "1"))
|
2020-07-07 22:18:40 +08:00
|
|
|
else:
|
|
|
|
link.url = str(base_url.clone().add_param(
|
2020-07-07 22:49:37 +08:00
|
|
|
"page", str(self.page_no - 1)))
|
|
|
|
link.is_small_screen = True
|
|
|
|
self._links.append(link)
|
|
|
|
# The first page
|
|
|
|
link = self.Link()
|
|
|
|
link.title = "1"
|
|
|
|
if not self.is_reversed:
|
|
|
|
link.url = str(base_url)
|
2020-07-07 22:18:40 +08:00
|
|
|
else:
|
2020-07-07 22:49:37 +08:00
|
|
|
link.url = str(base_url.clone().add_param("page", "1"))
|
|
|
|
if self.page_no == 1:
|
|
|
|
link.is_active = True
|
|
|
|
self._links.append(link)
|
|
|
|
# The previous ellipsis
|
|
|
|
if self.page_no > 4:
|
|
|
|
link = self.Link()
|
|
|
|
if self.page_no > 5:
|
|
|
|
link.title = pgettext("Pagination|", "...")
|
|
|
|
else:
|
|
|
|
link.title = "2"
|
|
|
|
link.url = str(base_url.clone().add_param(
|
|
|
|
"page", "2"))
|
|
|
|
self._links.append(link)
|
|
|
|
# The nearby pages
|
|
|
|
for no in range(self.page_no - 2, self.page_no + 3):
|
|
|
|
if no <= 1 or no >= self.total_pages:
|
|
|
|
continue
|
|
|
|
link = self.Link()
|
|
|
|
link.title = str(no)
|
2020-07-07 22:18:40 +08:00
|
|
|
link.url = str(base_url.clone().add_param(
|
2020-07-07 22:49:37 +08:00
|
|
|
"page", str(no)))
|
|
|
|
if no == self.page_no:
|
|
|
|
link.is_active = True
|
|
|
|
self._links.append(link)
|
|
|
|
# The next ellipsis
|
|
|
|
if self.page_no + 3 < self.total_pages:
|
|
|
|
link = self.Link()
|
|
|
|
if self.page_no + 4 < self.total_pages:
|
|
|
|
link.title = pgettext("Pagination|", "...")
|
|
|
|
else:
|
|
|
|
link.title = str(self.total_pages - 1)
|
|
|
|
link.url = str(base_url.clone().add_param(
|
|
|
|
"page", str(self.total_pages - 1)))
|
|
|
|
self._links.append(link)
|
|
|
|
# The last page
|
2020-07-07 22:18:40 +08:00
|
|
|
link = self.Link()
|
2020-07-07 22:49:37 +08:00
|
|
|
link.title = str(self.total_pages)
|
|
|
|
if self.is_reversed:
|
|
|
|
link.url = str(base_url)
|
2020-07-07 22:18:40 +08:00
|
|
|
else:
|
|
|
|
link.url = str(base_url.clone().add_param(
|
2020-07-07 22:49:37 +08:00
|
|
|
"page", str(self.total_pages)))
|
|
|
|
if self.page_no == self.total_pages:
|
2020-07-07 22:18:40 +08:00
|
|
|
link.is_active = True
|
2020-07-07 22:49:37 +08:00
|
|
|
self._links.append(link)
|
|
|
|
# The next page
|
2020-07-07 22:18:40 +08:00
|
|
|
link = self.Link()
|
2020-07-07 22:49:37 +08:00
|
|
|
link.title = pgettext("Pagination|", "Next")
|
|
|
|
if self.page_no < self.total_pages:
|
|
|
|
if self.page_no + 1 == self.total_pages:
|
|
|
|
if self.is_reversed:
|
|
|
|
link.url = str(base_url)
|
|
|
|
else:
|
|
|
|
link.url = str(base_url.clone().add_param(
|
|
|
|
"page", str(self.total_pages)))
|
2020-07-07 22:18:40 +08:00
|
|
|
else:
|
|
|
|
link.url = str(base_url.clone().add_param(
|
2020-07-07 22:49:37 +08:00
|
|
|
"page", str(self.page_no + 1)))
|
|
|
|
link.is_small_screen = True
|
|
|
|
self._links.append(link)
|
|
|
|
return self._links
|
2020-07-07 22:18:40 +08:00
|
|
|
|
|
|
|
class Link:
|
|
|
|
"""A navigation link in the pagination bar.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
url (str): The link URL, or for a non-link slot.
|
|
|
|
title (str): The title of the link.
|
|
|
|
is_active (bool): Whether this link is currently active.
|
|
|
|
is_small_screen (bool): Whether this link is for small
|
|
|
|
screens
|
|
|
|
"""
|
|
|
|
url = None
|
|
|
|
title = None
|
|
|
|
is_active = False
|
|
|
|
is_small_screen = False
|
2020-07-07 20:54:35 +08:00
|
|
|
|
|
|
|
|
2020-07-07 20:56:01 +08:00
|
|
|
class PageNoOutOfRangeException(Exception):
|
2020-07-07 20:55:29 +08:00
|
|
|
"""The error thrown when the specified page number is out of
|
|
|
|
range.
|
|
|
|
"""
|
2020-07-07 21:07:25 +08:00
|
|
|
pass
|