From 2ad0c5560b50dc07e38e7dc5a81b5c4f89c2f54c Mon Sep 17 00:00:00 2001 From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de> Date: Thu, 5 Sep 2024 15:25:53 +0200 Subject: [PATCH] Add instance.list_templates and template selection in the schild view --- schilder2000/instance.py | 19 ++++++++++++++++++- schilder2000/templates/schild.html.j2 | 7 +++++++ schilder2000/views.py | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/schilder2000/instance.py b/schilder2000/instance.py index f74d22e..978ed0c 100644 --- a/schilder2000/instance.py +++ b/schilder2000/instance.py @@ -1,6 +1,6 @@ from . import db from .models import Schild -from .helpers import Blueprint +from .helpers import Blueprint, get_template_attribute from flask import url_for @@ -25,3 +25,20 @@ def schild_html(ident): @bp.route("/schild/<ident>.pdf") def schild_pdf(ident): return render_pdf(url_for(".schild_html", ident=ident)) + + +def list_templates(): + schild = Schild() + loader = bp.jinja_loader + for t in loader.list_templates(): + if t.startswith("_"): + continue + yield dict( + name=t, + description=get_template_attribute( + bp.real_template_name(t), + "description", + vars=dict(schild=schild), + default=None, + ), + ) diff --git a/schilder2000/templates/schild.html.j2 b/schilder2000/templates/schild.html.j2 index f705571..8915deb 100644 --- a/schilder2000/templates/schild.html.j2 +++ b/schilder2000/templates/schild.html.j2 @@ -24,6 +24,13 @@ {%- for field in form -%} {{ render_field(field) }} {%- endfor -%} + <select name="template"> + {%- for t in templates -%} + <option value="{{ t.name }}"> + {{ t.description or t.name }} + </option> + {% endfor %} + </select> <div class="box"> <input type="submit" value="Schild erstellen" /> </div> diff --git a/schilder2000/views.py b/schilder2000/views.py index 9738086..953afa3 100644 --- a/schilder2000/views.py +++ b/schilder2000/views.py @@ -1,4 +1,5 @@ from . import db +from .instance import list_templates from .models import Schild from flask import Blueprint, render_template, request, redirect, url_for @@ -30,7 +31,7 @@ def schild(ident): db.session.commit() else: form = SchildForm(obj=schild) - return render_template("schild.html.j2", schild=schild, form=form) + return render_template("schild.html.j2", schild=schild, form=form, templates=list_templates()) @bp.route("/create", methods=["GET", "POST"]) -- GitLab