Automatic load of home screen and cleanups/refactorings

This makes more sense and does not cause confusion because user is also a name of a template. --- templates/hello.html | 2 +- templates/main.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/templates/hello.html b/templates/hello.html index 0e709ce..509ef08 100644 --- a/templates/hello.html +++ b/templates/hello.html @@ -1,5 +1,5 @@ <center> -<div id="user"></div> +<div id="content"></div> <p>Hack me: <tt>https://rtime.felk.cvut.cz/gitweb/coffee/main.git</tt></p> <p>Debug: <span id="log"></span></p> </center> diff --git a/templates/main.js b/templates/main.js index 9add69a..9cc11f5 100644 --- a/templates/main.js +++ b/templates/main.js @@ -73,7 +73,7 @@ function hiddenUpdateRemote(json, time = new Date()) { break; case "rfid": if (identifier_registration) { - ajax("POST", "user/identifier/add", JSON.stringify({ id: msg.uid }), "user"); + ajax("POST", "user/identifier/add", JSON.stringify({ id: msg.uid }), "content"); addIdentifier_finish(); } else { @@ -165,14 +165,14 @@ function ajax(method, route, data, id) { function login(id) { - ajax("POST", "login", id, "user"); + ajax("POST", "login", id, "content"); id_user = id; countingTimeLogout(120); } function logout() { sendReset(); - ajax("GET", "logout", "", "user"); + ajax("GET", "logout", "", "content"); id_user = undefined; timeToLogout = undefined; identifier_registration = false; @@ -192,7 +192,7 @@ function countingTimeLogout(count_time) } function renameUser() { - ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "user"); + ajax("GET", "user/rename?name=" + document.getElementById("username").value, "", "content"); } function getFlavor(letter) { @@ -211,7 +211,7 @@ function addCoffee(flavor, time = new Date()) { flavor: flavor }); if (id_user) { - ajax("POST", "coffee/add", data, "user"); + ajax("POST", "coffee/add", data, "content"); flavorChosen = flavor; id_user = undefined; countingTimeLogout(10); //mean 10 seconds @@ -227,7 +227,7 @@ function addEvent(event_name, action_msg, time = new Date()) { }); if (id_user) { eventMsg = "You have " + action_msg + ". Thanks!" - ajax("POST", "event", data, "user"); + ajax("POST", "event", data, "content"); window.scrollTo(0, 0); // Scroll up } } @@ -248,7 +248,7 @@ function addIdentifier_finish() { } function disableIdentifier(id) { - ajax("POST", "user/identifier/disable", JSON.stringify({ id: id }), "user"); + ajax("POST", "user/identifier/disable", JSON.stringify({ id: id }), "content"); } function renameIdentifier(i) { @@ -257,7 +257,7 @@ function renameIdentifier(i) { name: document.getElementById("identifier_name_" + i).value }); - ajax("POST", "user/identifier/rename", data, "user"); + ajax("POST", "user/identifier/rename", data, "content"); } function sendLog(json) { -- 2.28.0.rc2

This will simplify the next commit. --- templates/events.html | 85 ++++++++++++++++++++++++++++++++++++++++++ templates/user.html | 87 +------------------------------------------ 2 files changed, 87 insertions(+), 85 deletions(-) create mode 100644 templates/events.html diff --git a/templates/events.html b/templates/events.html new file mode 100644 index 0000000..541af41 --- /dev/null +++ b/templates/events.html @@ -0,0 +1,85 @@ +<style> + +.warning { + background-color: navajowhite; +} + +.bad { + background-color: orangered; + color: white; +} + +.events { + margin: 0.8em; + margin-bottom: 1.5em; + padding: 2px; + border-spacing: 2em 0; +} + +.events h3 { + display: inline-block; + margin-top: 1ex; + margin-right: 0.7em; +} + +.events-box { + border: 1px solid black; + padding: 0.5em; + display: inline-block; + vertical-align: top; +} + +.events-box h4 { + margin: 0.5ex; +} +.events-box .btnline { + text-align: left; +} +.events-box input { + padding: 1.5ex; + margin: 0.5ex; +} +</style> + + <br /> + <form> + {%- macro event_box(title, events, warn_days=999, bad_days=9999) -%} + {# Calculate maximum timestamp of all relevant events #} + {%- set when = last_events.items() | selectattr(0, 'in', events) | map(attribute=1) | max -%} + {%- set days = when | days | default(0) -%} + <div class="events-box{% if days >= bad_days %} bad {% elif days >= warn_days %} warning{% endif %}"> + <h4>{{title | capitalize}}</h4> + {#- The first item in the list is used as button label, last item in the overview -#} + {%- set verb = { + "COFFEE_MACHINE_CLEANED": ["cleaned"], + "COFFEE_PACK_OPENED": ["opened"], + "LAST_COFFEE_PACK_OPENED": ["last opened"], + "MILK_CONTAINER_CLEANED": ["cleaned (water)", "cleaned"], + "MILK_CONTAINER_CLEANED_WITH_TABLET": ["cleaned (tablet)"], + } + -%} + {% if name -%} {# User logged in - show action buttons #} + {%- for event in events %} + <div class="btnline"> + <input type="button" value="{{ verb[event]|first }}" + onclick="addEvent('{{ event }}', '{{ verb[events[0]]|last }} the {{ title }}')" /> + ({{ last_events[event] | humanize(max_interval="days") if event in last_events else "never" }}) + </div> + {%- endfor -%} + {%- else -%} {# Nobody logged in - show overview with summary times #} + {%- if when -%} + {{ verb[events[0]]|last }} {{ when | humanize(max_interval="days") }} + {%- else -%} + never {{ verb[events[0]]|last }} + {%- endif -%} + {%- endif %} + </div> + {%- endmacro %} + <div class="events"> + <h3>{{ ("Record<br />event" if name else "Events") | safe }}:</h3> + {{ event_box('coffee machine', ['COFFEE_MACHINE_CLEANED'] ) }} + {{ event_box('milk container', ['MILK_CONTAINER_CLEANED', 'MILK_CONTAINER_CLEANED_WITH_TABLET'], warn_days=4, bad_days=7) }} + + {{ event_box('coffee pack', ['COFFEE_PACK_OPENED'] ) }} + </div> + </form> diff --git a/templates/user.html b/templates/user.html index cca3ef8..e10409a 100644 --- a/templates/user.html +++ b/templates/user.html @@ -1,46 +1,3 @@ -<style> - -.warning { - background-color: navajowhite; -} - -.bad { - background-color: orangered; - color: white; -} - -.events { - margin: 0.8em; - margin-bottom: 1.5em; - padding: 2px; - border-spacing: 2em 0; -} - -.events h3 { - display: inline-block; - margin-top: 1ex; - margin-right: 0.7em; -} - -.events-box { - border: 1px solid black; - padding: 0.5em; - display: inline-block; - vertical-align: top; -} - -.events-box h4 { - margin: 0.5ex; -} -.events-box .btnline { - text-align: left; -} -.events-box input { - padding: 1.5ex; - margin: 0.5ex; -} -</style> - {###############} {# Graphs etc. #} {###############} @@ -100,48 +57,8 @@ {##########} {# Events #} {##########} - <br /> - <form> - {%- macro event_box(title, events, warn_days=999, bad_days=9999) -%} - {# Calculate maximum timestamp of all relevant events #} - {%- set when = last_events.items() | selectattr(0, 'in', events) | map(attribute=1) | max -%} - {%- set days = when | days | default(0) -%} - <div class="events-box{% if days >= bad_days %} bad {% elif days >= warn_days %} warning{% endif %}"> - <h4>{{title | capitalize}}</h4> - {#- The first item in the list is used as button label, last item in the overview -#} - {%- set verb = { - "COFFEE_MACHINE_CLEANED": ["cleaned"], - "COFFEE_PACK_OPENED": ["opened"], - "LAST_COFFEE_PACK_OPENED": ["last opened"], - "MILK_CONTAINER_CLEANED": ["cleaned (water)", "cleaned"], - "MILK_CONTAINER_CLEANED_WITH_TABLET": ["cleaned (tablet)"], - } - -%} - {% if name -%} {# User logged in - show action buttons #} - {%- for event in events %} - <div class="btnline"> - <input type="button" value="{{ verb[event]|first }}" - onclick="addEvent('{{ event }}', '{{ verb[events[0]]|last }} the {{ title }}')" /> - ({{ last_events[event] | humanize(max_interval="days") if event in last_events else "never" }}) - </div> - {%- endfor -%} - {%- else -%} {# Nobody logged in - show overview with summary times #} - {%- if when -%} - {{ verb[events[0]]|last }} {{ when | humanize(max_interval="days") }} - {%- else -%} - never {{ verb[events[0]]|last }} - {%- endif -%} - {%- endif %} - </div> - {%- endmacro %} - <div class="events"> - <h3>{{ ("Record<br />event" if name else "Events") | safe }}:</h3> - {{ event_box('coffee machine', ['COFFEE_MACHINE_CLEANED'] ) }} - {{ event_box('milk container', ['MILK_CONTAINER_CLEANED', 'MILK_CONTAINER_CLEANED_WITH_TABLET'], warn_days=4, bad_days=7) }} - - {{ event_box('coffee pack', ['COFFEE_PACK_OPENED'] ) }} - </div> - </form> + +{% include "events.html" %} {##################################} {# User name and chip idnetifiers #} -- 2.28.0.rc2

Now, when the application starts, only a blank page is shown. The home screen (summary graphs for all users) shows only after somebody logs in and out. With this change, the home screen is loaded automatically on the first start or on manual page reload. Together with this change, we split the home and user screens to separate templates (previously the home screen was the user screen with name unset). Hopefully, this change makes the UI logic a bit easier to follow. --- app.py | 20 +++++++++++--------- templates/home.html | 5 +++++ templates/main.js | 4 ++++ templates/user.html | 9 --------- 4 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 templates/home.html diff --git a/app.py b/app.py index cbdb9e9..64a39f6 100644 --- a/app.py +++ b/app.py @@ -83,12 +83,18 @@ app.jinja_env.filters['days'] = days_filter @app.route('/') def hello(): - if "uid" in session: - uid = session["uid"] - return render_template('hello.html', name=db.get_name(uid)) return render_template('hello.html') +@app.route('/home') +def home(): + # TODO: Replace stamp parameter with proper cache control HTTP + # headers in response + return render_template('home.html', + stamp=time.time(), + last_events=db.last_events()) + + @app.route('/login', methods=["POST"]) @app.route('/login/<iid>') def login(iid=None): @@ -112,7 +118,7 @@ def login(iid=None): def logout(): session.pop('uid', None) session.pop('iid', None) - return redirect(url_for('user')) + return redirect(url_for('home')) @app.route('/user') @@ -129,11 +135,7 @@ def user(): stamp=time.time(), last_events=db.last_events() ) - # TODO: Replace stamp parameter with proper cache control HTTP - # headers in response - return render_template('user.html', stamp=time.time(), - last_events=db.last_events(), - ) + return redirect(url_for('home')) @app.route('/user/rename') diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..48c0430 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,5 @@ + <p>Use your card/token to log in...</p> + + <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}"> + <img src="{{ url_for('coffee_graph_flavors', _external=True, stamp=stamp, days=7) }}"> + {% include "events.html" %} diff --git a/templates/main.js b/templates/main.js index 9cc11f5..0976a9e 100644 --- a/templates/main.js +++ b/templates/main.js @@ -106,6 +106,10 @@ function loadRemote(string) { replayOfflineQueue(); updateUI(); clearTimeout(reloadTimer); + if (id_user) + logout(); + else + ajax("GET", "home", "", "content"); // Load home screen on first load or reload } else { // Cancel current timer for the case when loadRemote() // was called multiple times (e.g. multiple ajax() diff --git a/templates/user.html b/templates/user.html index e10409a..28d6022 100644 --- a/templates/user.html +++ b/templates/user.html @@ -1,7 +1,6 @@ {###############} {# Graphs etc. #} {###############} -{% if name %} <form style="position: absolute; right: 15%; width: 15%; height: 15%;"> <button type="button" id="logout_button" onclick="logout()" style="width: 100%; height: 100%;">logout</button> </form> @@ -47,12 +46,6 @@ </form> {% endif %} </p> -{% else %} - <p>Use your card/token to log in...</p> - - <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}"> - <img src="{{ url_for('coffee_graph_flavors', _external=True, stamp=stamp, days=7) }}"> -{% endif %} {##########} {# Events #} @@ -63,7 +56,6 @@ {##################################} {# User name and chip idnetifiers #} {##################################} -{% if name %} <br /> <p> <form> @@ -92,4 +84,3 @@ </tr> </table> </form> -{% endif %} -- 2.28.0.rc2

Mně se to líbí :-) Konečně tam nebude ta prázdná obrazovka. :) J. Cituji Michal Sojka <michal.sojka@cvut.cz>:
A few more changes. Let me know what do you think.
_______________________________________________ Coffee mailing list Coffee@rtime.felk.cvut.cz https://rtime.felk.cvut.cz/mailman/listinfo/coffee
participants (2)
-
klapajar@fel.cvut.cz
-
Michal Sojka