From bcb5da1a5e4f218589179dd578fc17e0fc129dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 9 Jul 2020 01:03:03 +0800 Subject: [PATCH] Revised the code to generate the page size options in Pagination, to be clear. --- mia_core/utils.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/mia_core/utils.py b/mia_core/utils.py index 9a7cbd9..07f6c6f 100644 --- a/mia_core/utils.py +++ b/mia_core/utils.py @@ -142,6 +142,7 @@ class UrlBuilder: urllib.parse.quote(self.name), urllib.parse.quote(self.value)) + DEFAULT_PAGE_SIZE = 10 @@ -320,10 +321,8 @@ class Pagination: def page_size_options(self): base_url = UrlBuilder(self._current_url).del_param( "page").del_param("page-size") - return [self.PageSizeOption( - x, str(base_url.clone().add_param("page-size", str(x))) - if x != DEFAULT_PAGE_SIZE else str(base_url)) - for x in [10, 100, 200]] + return [self.PageSizeOption(x, _page_size_url(base_url, x)) + for x in [10, 100, 200]] class PageSizeOption: """A page size option. @@ -344,6 +343,21 @@ class Pagination: self.url = url +def _page_size_url(base_url, size): + """Returns the URL for a new page size. + + Args: + base_url (UrlBuilder): The base URL builder. + size (int): The new page size. + + Returns: + str: The URL for the new page size. + """ + if size == DEFAULT_PAGE_SIZE: + return str(base_url) + return str(base_url.clone().add_param("page-size", str(size))) + + class PageNoOutOfRangeException(Exception): """The error thrown when the specified page number is out of range.