From 7781a04122abb059a295a0d4b08b9b4350988c46 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 3 Dec 2024 06:22:44 +0100 Subject: [PATCH] To /files view, add filtering by tags. --- src/templates/files.tmpl | 6 ++++-- src/ytplom/http.py | 30 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/templates/files.tmpl b/src/templates/files.tmpl index 7db6174..f8286e5 100644 --- a/src/templates/files.tmpl +++ b/src/templates/files.tmpl @@ -4,18 +4,20 @@ {% block body %} {{ macros.nav_head(page_names, "files") }}
-filter: +filter filename: +filter tags: show absent:

known files (shown: {{files|length}}):

- + {% for file in files %} + {% endfor %} diff --git a/src/ytplom/http.py b/src/ytplom/http.py index 006d964..d61b75c 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -22,11 +22,11 @@ from ytplom.primitives import NotFoundException, PATH_APP_DATA # type definitions for mypy _PageNames: TypeAlias = dict[str, Path] -_ParamsStr = NewType('_ParamsStr', str) +_FilterStr = NewType('_FilterStr', str) _TemplateContext: TypeAlias = dict[ str, None | bool - | FilesWithIndex | _PageNames | _ParamsStr | Path | PlayerUpdateId + | FilesWithIndex | _PageNames | _FilterStr | Path | PlayerUpdateId | QueryText | QuotaCost | UrlStr | 'VideoFile' | YoutubeId | 'YoutubeVideo' | list[FlagName] | list['Tag'] | list['VideoFile'] | list['YoutubeVideo'] | list['YoutubeQuery'] @@ -175,10 +175,7 @@ class _TaskHandler(BaseHTTPRequestHandler): elif PAGE_NAMES['download'] == page_name: self._send_or_download_video(YoutubeId(toks_url[2])) elif PAGE_NAMES['files'] == page_name: - params = parse_qs(url.query) - filter_ = _ParamsStr(params.get('filter', [''])[0]) - show_absent = params.get('show_absent', [False])[0] - self._send_files_index(filter_, bool(show_absent)) + self._send_files_index(parse_qs(url.query)) elif PAGE_NAMES['file'] == page_name: self._send_file_data(Hash.from_b64(toks_url[2])) elif PAGE_NAMES['yt_result'] == page_name: @@ -282,19 +279,22 @@ class _TaskHandler(BaseHTTPRequestHandler): {'file': file, 'flag_names': list(FILE_FLAGS), 'unused_tags': [t for t in all_tags if t not in file.tags]}) - def _send_files_index(self, - filter_: _ParamsStr, - show_absent: bool - ) -> None: + def _send_files_index(self, params: dict[str, list[str]]) -> None: + filter_path = _FilterStr(params.get('filter_path', [''])[0]) + filter_tags = _FilterStr(params.get('filter_tags', [''])[0]) + show_absent = bool(params.get('show_absent', [False])[0]) with DbConn() as conn: files = [f for f in VideoFile.get_all(conn) - if filter_.lower() in str(f.rel_path).lower() + if str(filter_path).lower() in str(f.rel_path).lower() + and ([t for t in f.tags if str(filter_tags).lower() in t] + or not filter_tags) and (show_absent or f.present)] files.sort(key=lambda t: t.rel_path) - self._send_rendered_template( - _NAME_TEMPLATE_FILES, - {'files': files, 'filter': filter_, - 'show_absent': show_absent}) + self._send_rendered_template(_NAME_TEMPLATE_FILES, + {'files': files, + 'filter_path': filter_path, + 'filter_tags': filter_tags, + 'show_absent': show_absent}) def _send_missing_json(self) -> None: with DbConn() as conn: -- 2.30.2
sizeactionspath
sizeactionstagspath
{{ file.size | round(3) }} {% for tag in file.tags %}{{tag}} {%endfor %} {{file.rel_path}}