aggiunto parametro template

This commit is contained in:
encrypt 2023-10-17 20:18:31 +02:00
parent 288335f563
commit 667e7f3161
2 changed files with 19 additions and 4 deletions

14
app.py
View file

@ -3,6 +3,7 @@
import requests
import json
import datetime
import pathlib
from jinja2 import Environment, FileSystemLoader
from flask import Flask
from flask import render_template, request
@ -10,7 +11,8 @@ from flask import render_template, request
app = Flask(__name__)
gancio_instance = 'balotta.org'
template_name = 'simple'
templates = [x.stem for x in pathlib.Path('./templates/flyers/').glob("*.html")]
default_template = 'simple'
events_ep = f'https://{gancio_instance}/api/events'
def day2giorno(day):
@ -48,14 +50,20 @@ def fetch_events(api_endpoint):
@app.route("/")
def main():
events = fetch_events(events_ep)
return render_template('index.html', events=events)
return render_template('index.html',
events=events,
templates=templates,
default_template=default_template)
@app.route("/flyer")
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:
return "template not found", 404
events = fetch_events(events_ep)
events = [ e for e in events if e['id'] in relevant_events ]
return render_template(f'flyers/{template_name}.html', events=events)
return render_template(f'flyers/{template}.html', events=events)

View file

@ -9,7 +9,14 @@ L'attuale layout può contenere fino a 24 eventi.
</div>
<br>
<form action="/flyer" target="_blank">
{% for event in events %}
<label for="template">Scegli un template</label>
<select id="template" name="template">
{% for template in templates %}
<option value="{{template}}">{{template}}</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}}">
<label for="{{event.id}}"> {{loop.index}} [ {{event.when}} ] {{ event.title }}</label><br>
{% endfor %}