modificato il sistema di gestione dei templates, ora un template è fatto dal template jinja + i parametri, aggiunto autoselect in home

This commit is contained in:
encrypt 2023-10-17 22:37:02 +02:00
parent 667e7f3161
commit 326e76bae9
3 changed files with 53 additions and 7 deletions

12
app.py
View file

@ -11,7 +11,7 @@ from flask import render_template, request
app = Flask(__name__)
gancio_instance = 'balotta.org'
templates = [x.stem for x in pathlib.Path('./templates/flyers/').glob("*.html")]
templates = json.load(open('./templates.json'))
default_template = 'simple'
events_ep = f'https://{gancio_instance}/api/events'
@ -60,10 +60,14 @@ def main():
def flyer():
relevant_events = request.args.getlist('events', type=int)
template = request.args.get("template", default=default_template, type=str)
if template not in templates:
if template not in [ t['name'] for t in templates ]:
return "template not found", 404
template = [t for t in templates if t['name'] == template][0]
events = fetch_events(events_ep)
events = [ e for e in events if e['id'] in relevant_events ]
return render_template(f'flyers/{template}.html', events=events)
events = [ e for e in events if e['id'] in relevant_events ]
params = {}
if 'params' in template:
params = template['params']
return render_template(f'flyers/{template["baseTemplate"]}.html', events=events, params=params)

29
templates.json Normal file
View file

@ -0,0 +1,29 @@
[
{
"name": "simple",
"baseTemplate": "simple",
"paperSize": "A3",
"maxEvents": 24,
"pages": 1,
"params": {
"colorMode": "black"
}
},
{
"name": "simple-white",
"baseTemplate": "simple",
"paperSize": "A3",
"maxEvents": 24,
"pages": 1,
"params": {
"colorMode": "white"
}
},
{
"name": "asymmetric",
"baseTemplate": "asymmetric",
"paperSize": "A3",
"maxEvents": 24,
"pages": 1
}
]

View file

@ -10,15 +10,28 @@ L'attuale layout può contenere fino a 24 eventi.
<br>
<form action="/flyer" target="_blank">
<label for="template">Scegli un template</label>
<select id="template" name="template">
<select onchange="templateSelected(this)" id="template" name="template">
{% for template in templates %}
<option value="{{template}}">{{template}}</option>
<option value="{{template.name}}">{{template.name}}</option>
{% endfor %}
</select>
<br>
{% for event in events %}
<input type="checkbox" {% if loop.index <= 24 %} checked {% endif %} id="{{event.id}}" name="events" value="{{event.id}}">
<input type="checkbox" id="{{event.id}}" name="events" value="{{event.id}}">
<label for="{{event.id}}"> {{loop.index}} [ {{event.when}} ] {{ event.title }}</label><br>
{% endfor %}
<input type="submit" value="Genera volantino">
</form>
<script>
const templates={{templates|tojson}}
function templateSelected(el) {
const template = templates.find((t) => t.name == el.value)
const events = document.querySelectorAll("[name=events]")
var maxEvents = template.maxEvents
events.forEach((e) => {
e.checked = maxEvents > 0
maxEvents--
})
}
</script>