Add navigatable pages for tags

master
Felix Stupp 10 months ago
parent 3a1b2725bc
commit 4ff8bef871
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -284,6 +284,13 @@ def _template_are_multiple_considered(elem_ids: Iterable[int]) -> Mapping[int, b
return are_multiple_considered(elem_ids)
@flask_app.template_filter()
def filter_preference_tags(tags: Iterable[Tag]) -> Iterable[Tag]:
for t in tags:
if t.use_for_preferences:
yield t
####
# Routes
####
@ -838,14 +845,28 @@ def show_stats_queries() -> ResponseReturnValue:
@flask_app.route("/tag")
def show_tag() -> ResponseReturnValue:
tag_list: List[Tag] = Tag.select()
def show_root_tags() -> ResponseReturnValue:
tag_list: List[Tag] = orm.select(
t for t in Tag if len(t.super_tag_list) <= 0
).order_by(Tag.title, Tag.id)
return render_template(
"tag_list.htm",
"tag/list.htm",
tag_list=tag_list,
)
@flask_app.route("/tag/<int:tag_id>")
def show_tag_element(tag_id: int) -> ResponseReturnValue:
tag: Optional[Tag] = Tag.get(id=tag_id)
if tag is None:
return make_response(f"Not found", 404)
return render_template(
"tag/element.htm",
tag=tag,
tag_media_list=tag.media_list.order_by(MediaElement.sort_key),
)
@flask_app.route("/debug/test")
def test() -> ResponseReturnValue:
first: MediaElement = MediaElement.select().first()
@ -1301,3 +1322,42 @@ def api_media_set_dependent() -> ResponseReturnValue:
def api_tag_delete_temporary() -> ResponseReturnValue:
Tag.scrub_temporary_tags()
return redirect_back_or_okay()
@flask_app.route("/api/tag/<int:tag_id>", methods=["GET", "POST"])
def api_tag(tag_id: int) -> ResponseReturnValue:
tag: Tag = Tag.get(id=tag_id)
if tag is None:
return {
"status": False,
"error": f"Object not found",
}, 404
if request.method == "GET":
return {
"status": True,
"data": {
"id": tag.id,
"title": tag.title,
"notes": tag.notes,
"use_for_preferences": tag.use_for_preferences,
"super_tags": [t.id for t in tag.super_tag_list],
"sub_tags": [t.id for t in tag.sub_tag_list],
},
}, 200
elif request.method == "POST":
data = request.form.to_dict()
if "redirect" in data:
del data["redirect"]
KEY_CONVETER: Mapping[str, Callable[[str], Any]] = {
"title": str,
"notes": str,
"use_for_preferences": environ_bool,
}
for key in data:
if key not in KEY_CONVETER:
return {
"status": False,
"error": f"Cannot set key {key!r} on Tag",
}, 400
tag.set(**{key: KEY_CONVETER[key](val) for key, val in data.items()})
return redirect_back_or_okay()

@ -200,6 +200,10 @@ class Tag(db.Entity, Tagable, TagProto["Tag"]):
def orm_super_tags(self) -> Query[Tag]:
return self.super_tag_list if self.use_for_preferences else []
@property
def info_link(self) -> str:
return f"/tag/{self.id}"
class TagKey(db.Entity):
num_id: int = orm.PrimaryKey(int, auto=True)

@ -66,7 +66,7 @@
{% endif %}
{% if collection.all_tags %}
<li>
Tags: {{ collection.all_tags | map(attribute="title") | sort | join(" | ") }}
Tags: {{ macros.tag_list(collection.all_tags | filter_preference_tags) }}
</li>
{% endif %}
{% if collection.watch_in_order %}

@ -42,7 +42,7 @@
</td>
<td><a href="{{ collection.info_link }}">{{ collection.title }}</a></td>
<td>
{{ collection.assigned_tags | map(attribute="title") | sort | join(" | ") }}
{{ macros.tag_list(collection.assigned_tags) }}
</td>
</tr>
{% endfor %}

@ -657,3 +657,47 @@
{% endfor %}
</table>
{%- endmacro %}
{% macro tag_list(tags) -%}
{%- for t in tags | sort(attribute="title") -%}
{%- if loop.index > 1 -%}
{{ " | " }}
{%- endif -%}
<a class="tag_item" href="{{ t.info_link }}"{% if t.notes %} title="{{ t.notes }}"{% endif %}>{{ t.title }}</a>
{%- endfor -%}
{%- endmacro %}
{% macro tag_table(tag_list) %}
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Use for Pref.</th>
<th>Notes</th>
</tr>
{% for tag in tag_list %}
{{ tag_table_entry(tag) }}
{% endfor %}
</table>
{%- endmacro %}
{% macro tag_table_entry(tag) %}
{%- set api_uri = "/api/tag/" + tag.id|string -%}
{%- set fragment = "tag_" + tag.id|string -%}
<tr id="{{ fragment }}">
<td>
{{- tag.id -}}
</td>
<td>
<a href="{{ tag.info_link }}">
{{- tag.title -}}
</a>
</td>
<td>
{{ post_form(api_uri, "use_for_preferences", tag.use_for_preferences | tenary("false", "true"), tag.use_for_preferences | tenary("Yes", "no"), fragment) }}
</td>
<td>
{{- tag.notes or "" -}}
</td>
</tr>
{%- endmacro %}

@ -57,7 +57,7 @@
{% endif %}
{% if element.all_tags %}
<li>
Tags: {{ element.all_tags | map(attribute="title") | join(" | ") }}
Tags: {{ macros.tag_list(element.all_tags | filter_preference_tags) }}
</li>
{% endif %}
</ul>

@ -0,0 +1,57 @@
{% import "macros.htm" as macros %}
<!DOCTYPE html>
<html>
{% set title = tag.title %}
<head>
<meta charset="utf-8"/>
<title>{{ title }} - Tag</title>
{{ macros.shared_style() }}
</head>
<body>
{{ macros.body_header() }}
<h1>{{ title }}</h1>
<h2>Notes</h2>
<pre>{{ tag.notes or "" }}</pre>
<h2>Properties</h2>
<ul>
{% set api_uri = "/api/tag/" + tag.id|string %}
<li>
Use for preferences:
{{ tag.use_for_preferences | tenary("Yes", "no") }}
{{ macros.post_form(api_uri, "use_for_preferences", tag.use_for_preferences | tenary("false", "true"), "toggle") }}
</li>
<li>
Direct Counts:
{{ tag.collection_list | length }} Collections,
{{ tag.media_list | length }} Elements
</li>
</ul>
{% if tag.super_tag_list | length > 0 %}
<h2>Super Tags</h2>
{{ macros.tag_table(tag.super_tag_list | sort(attribute="title")) }}
{% endif %}
{% if tag.sub_tag_list | length > 0 %}
<h2>Sub Tags</h2>
{{ macros.tag_table(tag.sub_tag_list | sort(attribute="title")) }}
{% endif %}
{% if tag.collection_list | length > 0 %}
<h2>Collections</h2>
<ul>
{% for collection in tag.collection_list|sort(attribute="title") %}
<li>
<a href="{{ collection.info_link }}">{{ collection.title }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% if tag.media_list | length > 0 %}
<h2>Elements</h2>
{{ macros.media_thumbnail_list(
elements=tag_media_list,
check_considered=True,
link_collection=True,
) }}
{% endif %}
</body>
</html>

@ -0,0 +1,15 @@
{% import "macros.htm" as macros %}
<!DOCTYPE html>
<html>
{% set title = tag_list | length | string + " Tags listed" %}
<head>
<meta charset="utf-8"/>
<title>{{ title }}</title>
{{ macros.shared_style() }}
</head>
<body>
{{ macros.body_header() }}
<h1>{{ title }}</h1>
{{ macros.tag_table(tag_list) }}
</body>
</html>

@ -1,55 +0,0 @@
{% import "macros.htm" as macros %}
<!DOCTYPE html>
<html>
{% set title = tag_list | length | string + " Tags known" %}
<head>
<meta charset="utf-8"/>
<title>{{ title }}</title>
{{ macros.shared_style() }}
</head>
<body>
{{ macros.body_header() }}
<h1>{{ title }}</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Use for Pref.</th>
<th>Super Tags</th>
<th>Sub Tags</th>
<th>Notes</th>
</tr>
{% macro format_tag(tag) %}
<a title="{{ tag.title }}">{{ tag.id }}</a>
{% endmacro %}
{% macro format_tag_list(tag_list) %}
{% for tag in tag_list|sort(attribute="id") %}
{{ format_tag(tag) }}
{{ " | " if not loop.last }}
{% endfor %}
{% endmacro %}
{% for tag in tag_list %}
<tr>
<td>
{{ tag.id }}
</td>
<td>
{{ tag.title }}
</td>
<td>
{{ tag.use_for_preferences | tenary("Yes", "no") }}
</td>
<td>
{{ format_tag_list(tag.super_tag_list) }}
</td>
<td>
{{ format_tag_list(tag.sub_tag_list) }}
</td>
<td>
{{ tag.notes or "" }}
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
Loading…
Cancel
Save