mirror of
https://github.com/UiO-Gaming/mustafa.git
synced 2026-11-05 13:56:48 +01:00
new paginator + refactor
This commit is contained in:
1 file changed
+73
-28
@@ -1,6 +1,5 @@
|
||||
from typing import Union, Dict
|
||||
from math import ceil
|
||||
from contextlib import contextmanager
|
||||
from math import ceil
|
||||
|
||||
|
||||
@contextmanager
|
||||
@@ -16,41 +15,87 @@ def ignore_exception(*exceptions) -> None:
|
||||
----------
|
||||
None
|
||||
"""
|
||||
|
||||
try:
|
||||
yield
|
||||
except (exceptions):
|
||||
except exceptions:
|
||||
pass
|
||||
|
||||
class Paginator:
|
||||
def __init__(self, content: list):
|
||||
self.content = content
|
||||
self.total_page_count = ceil(len(content) / 10)
|
||||
self.current_page = 1
|
||||
|
||||
def paginator(content: list, page: int) -> Dict[str, Union[int, str]]:
|
||||
"""
|
||||
Divides content into 10 element pages
|
||||
def get_page(self, page: int) -> list | None:
|
||||
"""
|
||||
Returns the page of the paginator
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
content: A list of strings
|
||||
page: The page
|
||||
Parameters
|
||||
----------
|
||||
page (int): The page you want to get
|
||||
|
||||
Returns
|
||||
-----------
|
||||
(dict): A dictionay containing content and metadata
|
||||
keys: pagecount, page, page_content
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
(list|None): The page you requested
|
||||
"""
|
||||
if page < 1 or page > self.total_page_count:
|
||||
return None
|
||||
|
||||
pagecount = ceil(len(content) / 10)
|
||||
start_index = (page - 1) * 10
|
||||
end_index = page * 10
|
||||
|
||||
if not page or page <= 0 or page > pagecount:
|
||||
page = 1
|
||||
return self.content[start_index:end_index]
|
||||
|
||||
start_index = (page - 1) * 10
|
||||
end_index = page * 10
|
||||
def get_current_page(self) -> list:
|
||||
"""
|
||||
Returns the current page of the paginator
|
||||
|
||||
page_content = content[start_index:end_index]
|
||||
Returns
|
||||
----------
|
||||
(list): The current page of the paginator
|
||||
"""
|
||||
return self.get_page(self.current_page)
|
||||
|
||||
page_data = {
|
||||
'pagecount': pagecount,
|
||||
'page': page,
|
||||
'page_content': page_content
|
||||
}
|
||||
return page_data
|
||||
def next_page(self) -> list | None:
|
||||
"""
|
||||
Returns the next page of the paginator
|
||||
|
||||
Returns
|
||||
----------
|
||||
(list): The next page of the paginator
|
||||
"""
|
||||
self.current_page += 1
|
||||
return self.get_page(self.current_page)
|
||||
|
||||
def previous_page(self) -> list | None:
|
||||
"""
|
||||
Returns the previous page of the paginator
|
||||
|
||||
Returns
|
||||
----------
|
||||
(list): The previous page of the paginator
|
||||
"""
|
||||
self.current_page -= 1
|
||||
return self.get_page(self.current_page)
|
||||
|
||||
def first_page(self) -> list | None:
|
||||
"""
|
||||
Returns the first page of the paginator
|
||||
|
||||
Returns
|
||||
----------
|
||||
(list): The first page of the paginator
|
||||
"""
|
||||
self.current_page = 1
|
||||
return self.get_page(self.current_page)
|
||||
|
||||
def last_page(self) -> list | None:
|
||||
"""
|
||||
Returns the last page of the paginator
|
||||
|
||||
Returns
|
||||
----------
|
||||
(list): The last page of the paginator
|
||||
"""
|
||||
self.current_page = self.total_page_count
|
||||
return self.get_page(self.current_page)
|
||||
Reference in new issue
Block a user