mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-09-11 11:20:02 +00:00
feat: add autorefresh button to the main page (#149)
* also add configuration options and change behaviour accordingly
This commit is contained in:
@ -17,6 +17,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# pylint: disable=too-many-public-methods
|
||||
import configparser
|
||||
import shlex
|
||||
import sys
|
||||
@ -85,9 +86,10 @@ class Configuration(configparser.RawConfigParser):
|
||||
empty_lines_in_values=not allow_multi_key,
|
||||
interpolation=ShellInterpolator(),
|
||||
converters={
|
||||
"intlist": lambda value: list(map(int, shlex.split(value))),
|
||||
"list": shlex.split,
|
||||
"path": self._convert_path,
|
||||
"pathlist": lambda value: [self._convert_path(element) for element in shlex.split(value)],
|
||||
"pathlist": lambda value: list(map(self._convert_path, shlex.split(value))),
|
||||
},
|
||||
)
|
||||
|
||||
@ -236,6 +238,8 @@ class Configuration(configparser.RawConfigParser):
|
||||
|
||||
# pylint and mypy are too stupid to find these methods
|
||||
# pylint: disable=missing-function-docstring,unused-argument
|
||||
def getintlist(self, *args: Any, **kwargs: Any) -> list[int]: ... # type: ignore[empty-body]
|
||||
|
||||
def getlist(self, *args: Any, **kwargs: Any) -> list[str]: ... # type: ignore[empty-body]
|
||||
|
||||
def getpath(self, *args: Any, **kwargs: Any) -> Path: ... # type: ignore[empty-body]
|
||||
|
@ -324,6 +324,15 @@ CONFIGURATION_SCHEMA: ConfigurationSchema = {
|
||||
"empty": False,
|
||||
"is_url": ["http", "https"],
|
||||
},
|
||||
"autorefresh_intervals": {
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"coerce": "integer",
|
||||
"min": 0,
|
||||
},
|
||||
},
|
||||
"enable_archive_upload": {
|
||||
"type": "boolean",
|
||||
"coerce": "boolean",
|
||||
|
@ -51,6 +51,7 @@ __all__ = [
|
||||
"parse_version",
|
||||
"partition",
|
||||
"pretty_datetime",
|
||||
"pretty_interval",
|
||||
"pretty_size",
|
||||
"safe_filename",
|
||||
"srcinfo_property",
|
||||
@ -353,6 +354,28 @@ def pretty_datetime(timestamp: datetime.datetime | float | int | None) -> str:
|
||||
return timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
def pretty_interval(interval: int) -> str:
|
||||
"""
|
||||
convert time interval to string
|
||||
|
||||
Args:
|
||||
interval(int): time interval in seconds
|
||||
|
||||
Returns:
|
||||
str: pretty printable interval as string
|
||||
"""
|
||||
minutes, seconds = divmod(interval, 60)
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
return " ".join([
|
||||
f"{value} {description}{"s" if value > 1 else ""}"
|
||||
for value, description in [
|
||||
(hours, "hour"),
|
||||
(minutes, "minute"),
|
||||
(seconds, "second"),
|
||||
] if value > 0
|
||||
])
|
||||
|
||||
|
||||
def pretty_size(size: float | None, level: int = 0) -> str:
|
||||
"""
|
||||
convert size to string
|
||||
|
@ -22,6 +22,7 @@ import aiohttp_jinja2
|
||||
from typing import Any, ClassVar
|
||||
|
||||
from ahriman.core.auth.helpers import authorized_userid
|
||||
from ahriman.core.utils import pretty_interval
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.apispec import aiohttp_apispec
|
||||
from ahriman.web.views.base import BaseView
|
||||
@ -37,6 +38,10 @@ class IndexView(BaseView):
|
||||
* control - HTML to insert for login control, HTML string, required
|
||||
* enabled - whether authorization is enabled by configuration or not, boolean, required
|
||||
* username - authenticated username if any, string, null means not authenticated
|
||||
* autorefresh_intervals - auto refresh intervals, optional
|
||||
* interval - auto refresh interval in milliseconds, integer, required
|
||||
* is_active - is current interval active or not, boolean, required
|
||||
* text - text representation of the interval (e.g. "30 seconds"), string, required
|
||||
* docs_enabled - indicates if api docs is enabled, boolean, required
|
||||
* index_url - url to the repository index, string, optional
|
||||
* repositories - list of repositories unique identifiers, required
|
||||
@ -66,8 +71,18 @@ class IndexView(BaseView):
|
||||
"username": auth_username,
|
||||
}
|
||||
|
||||
autorefresh_intervals = [
|
||||
{
|
||||
"interval": interval * 1000, # milliseconds
|
||||
"is_active": index == 0,
|
||||
"text": pretty_interval(interval),
|
||||
}
|
||||
for index, interval in enumerate(self.configuration.getintlist("web", "autorefresh_intervals", fallback=[]))
|
||||
]
|
||||
|
||||
return {
|
||||
"auth": auth,
|
||||
"autorefresh_intervals": sorted(autorefresh_intervals, key=lambda interval: interval["interval"]),
|
||||
"docs_enabled": aiohttp_apispec is not None,
|
||||
"index_url": self.configuration.get("web", "index_url", fallback=None),
|
||||
"repositories": [
|
||||
|
Reference in New Issue
Block a user