From 80e2f665b71ca9bf8a7470d5b5d43d021c2c2f0e Mon Sep 17 00:00:00 2001 From: Christian Heller <c.heller@plomlompom.de> Date: Thu, 28 Nov 2024 22:14:53 +0100 Subject: [PATCH] Add basic filtering by filename to /files view. --- src/templates/files.tmpl | 5 ++++- src/ytplom/misc.py | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/templates/files.tmpl b/src/templates/files.tmpl index 35786b4..72f99ac 100644 --- a/src/templates/files.tmpl +++ b/src/templates/files.tmpl @@ -3,7 +3,10 @@ {% block body %} {{ macros.nav_head(page_names, "files") }} -<p>downloaded videos:</p> +<form method="GET"> +filter: <input name="filter" value="{{filter}}" /><input type="submit" value="filter" /> +</form> +<p>downloaded videos ({{files|length}}):</p> <ul> {% for file in files %} <li><a href="/{{page_names.file}}/{{file.rel_path_b64}}">{{file.rel_path}}</a> diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index a85e6bc..f35a9c8 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -46,14 +46,15 @@ FlagsInt = NewType('FlagsInt', int) AmountDownloads = NewType('AmountDownloads', int) PlayerUpdateId = NewType('PlayerUpdateId', str) B64Str = NewType('B64Str', str) +ParamsStr = NewType('ParamsStr', str) PageNames: TypeAlias = dict[str, PathStr] DownloadsIndex: TypeAlias = dict[YoutubeId, PathStr] FilesWithIndex: TypeAlias = list[tuple[int, 'VideoFile']] TemplateContext: TypeAlias = dict[ str, None | bool - | FilesWithIndex | PageNames | PathStr | PlayerUpdateId | QueryText - | QuotaCost | 'VideoFile' | YoutubeId | 'YoutubeVideo' + | FilesWithIndex | PageNames | ParamsStr | PathStr | PlayerUpdateId + | QueryText | QuotaCost | 'VideoFile' | YoutubeId | 'YoutubeVideo' | list[FlagName] | list['VideoFile'] | list['YoutubeVideo'] | list['YoutubeQuery'] ] @@ -856,7 +857,8 @@ class TaskHandler(BaseHTTPRequestHandler): elif PAGE_NAMES['download'] == page_name: self._send_or_download_video(YoutubeId(toks_url[2])) elif PAGE_NAMES['files'] == page_name: - self._send_files_index() + filter_ = ParamsStr(parse_qs(url.query).get('filter', [''])[0]) + self._send_files_index(filter_) elif PAGE_NAMES['file'] == page_name: self._send_file_data(B64Str(toks_url[2])) elif PAGE_NAMES['yt_result'] == page_name: @@ -960,12 +962,13 @@ class TaskHandler(BaseHTTPRequestHandler): NAME_TEMPLATE_FILE_DATA, {'file': file, 'flag_names': list(FILE_FLAGS)}) - def _send_files_index(self) -> None: + def _send_files_index(self, filter_: ParamsStr) -> None: conn = DbConnection() - files = VideoFile.get_all(conn) + files = [f for f in VideoFile.get_all(conn) if filter_ in f.rel_path] conn.commit_close() files.sort(key=lambda t: t.rel_path) - self._send_rendered_template(NAME_TEMPLATE_FILES, {'files': files}) + self._send_rendered_template(NAME_TEMPLATE_FILES, + {'files': files, 'filter': filter_}) def _send_missing_json(self) -> None: conn = DbConnection() -- 2.30.2