home · contact · privacy
To /files view, add filtering by tags.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 3 Dec 2024 05:22:44 +0000 (06:22 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 3 Dec 2024 05:22:44 +0000 (06:22 +0100)
src/templates/files.tmpl
src/ytplom/http.py

index 7db6174c1e64daf2eada1731f944b12259ecd5f2..f8286e54f3172335d73e29b92926e73558d61ae5 100644 (file)
@@ -4,18 +4,20 @@
 {% block body %}
 {{ macros.nav_head(page_names, "files") }}
 <form method="GET">
-filter: <input name="filter" value="{{filter}}" />
+filter filename: <input name="filter_path" value="{{filter_path}}" />
+filter tags: <input name="filter_tags" value="{{filter_tags}}" />
 show absent: <input type="checkbox" name="show_absent" {% if show_absent %}checked{% endif %}/>
 <input type="submit" value="filter" />
 </form>
 <p>known files (shown: {{files|length}}):</p>
 <form action="files" method="POST">
 <table>
-<tr><th>size</th><th>actions</th><th>path</th></tr>
+<tr><th>size</th><th>actions</th><th>tags</th><th>path</th></tr>
 {% for file in files %}
 <tr>
 <td>{{ file.size | round(3) }}</td>
 <td><input type="submit" name="play_{{file.digest.b64}}" value="play" {% if not file.present %}disabled {% endif %}/></td>
+<td>{% for tag in file.tags %}{{tag}} {%endfor %}</td>
 <td><a href="/{{page_names.file}}/{{file.digest.b64}}">{{file.rel_path}}</a></td>
 </tr>
 {% endfor %}
index 006d96458b1a99a3bb1c41b2d527f463474d49e9..d61b75ca2c92b582d81fd19bd4d3839ffe953a48 100644 (file)
@@ -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: