4 コミット 667e7f3161 ... f692715250

作者 SHA1 メッセージ 日付
  encrypt f692715250 sistemato il select del template 11 ヶ月 前
  encrypt b8d4d96760 aggiornato readme 11 ヶ月 前
  encrypt fe013b5e28 il template simple po essere bianco po essere nero in base al parametro 11 ヶ月 前
  encrypt 326e76bae9 modificato il sistema di gestione dei templates, ora un template è fatto dal template jinja + i parametri, aggiunto autoselect in home 11 ヶ月 前
6 ファイル変更78 行追加17 行削除
  1. 2 8
      README.md
  2. 8 4
      app.py
  3. 15 0
      static/flyers/simple/white.css
  4. 29 0
      templates.json
  5. 4 1
      templates/flyers/simple.html
  6. 20 4
      templates/index.html

+ 2 - 8
README.md

@@ -11,12 +11,6 @@ flask run
 
 ## Come aggiungere dei nuovi template
 
-In teoria sto coso supporta più template ma non ho ancora finito di sistemarlo.
+Sto coso supporta più template, per template si intendono i file jinja2 (chiamiamoli template base) e dei parametri, l'insieme delle due cose forma un template.
 Se vuoi aggiungerne uno nuovo metti il template in `templates/flyers/nometemplate.html` e crea la directory `static/flyers/nometemplate` per i files css.
-
-## Come usare i tamplate esistenti
-
-Vai nel file app.py ed alla riga #13 passa, come valore di "template_name", il nome del template che vuoi utilizzare.
-I template si trovano in /templates/flyers.
-Ad esempio:
-template_name = 'simple_white'
+Poi vai in `templates.json` e aggiungi il tuo scopiazzando dagli altri.

+ 8 - 4
app.py

@@ -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)
     
     

+ 15 - 0
static/flyers/simple/white.css

@@ -0,0 +1,15 @@
+body {
+  background: #fff;
+}
+
+.intestazione {
+  color: #000;
+}
+
+footer {
+  color: #0d1117;
+}
+
+.contacts svg {
+  fill: #0d1117;
+}

+ 29 - 0
templates.json

@@ -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
+  }
+]

+ 4 - 1
templates/flyers/simple.html

@@ -5,7 +5,10 @@
     <title>Balotta cartecea</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <link rel="stylesheet" href="{{ url_for('static', filename='flyers/simple/style.css') }}">
-    <link rel="stylesheet" href="{{ url_for('static', filename='flyers/simple/paper.css') }} ">
+    {% if params.colorMode == 'white' %}
+       <link rel="stylesheet" href="{{ url_for('static', filename='flyers/simple/white.css') }} ">
+    {% endif %}
+    <link rel="stylesheet" href="{{ url_for('static', filename='flyers/simple/paper.css') }} ">    
   </head>
 
   <body class="A3 center">

+ 20 - 4
templates/index.html

@@ -5,20 +5,36 @@
 Scegli gli eventi che vuoi mettere nel volantino e premi genera.
 Per stampare fai "File >> Stampa" Scegli come destinazione "Salva come PDF" e seleziona il giusto formato di carta (A3).
 <br>
-L'attuale layout può contenere fino a 24 eventi.
+L'attuale layout può contenere fino a <span id="maxEvents"></span> eventi.
 </div>
 <br>
 <form action="/flyer" target="_blank">
   <label for="template">Scegli un template</label>
-  <select id="template" name="template">
+  <select onchange="templateSelected(this.value)" 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(templateName) {
+    const template = templates.find((t) => t.name == templateName)
+    const events = document.querySelectorAll("[name=events]")
+    document.querySelector("#maxEvents").innerHTML = template.maxEvents
+    var maxEvents = template.maxEvents
+    events.forEach((e) => {
+      e.checked = maxEvents > 0
+      maxEvents--
+    })
+  }
+  document.querySelector("#template").value = "{{default_template}}"
+  templateSelected("{{default_template}}")
+</script>
+