]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Ensure that the home screen is loaded automatically
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 20 Aug 2020 22:06:28 +0000 (00:06 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 21 Aug 2020 10:27:25 +0000 (12:27 +0200)
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
templates/home.html [new file with mode: 0644]
templates/main.js
templates/user.html

diff --git a/app.py b/app.py
index 4f3c791a20fbe6233ab69908740f660de3863bf6..f9785b87d4fadb2ec753b9f14115e3d14060e0b7 100644 (file)
--- a/app.py
+++ b/app.py
@@ -86,12 +86,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):
@@ -115,7 +121,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')
@@ -132,11 +138,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 (file)
index 0000000..48c0430
--- /dev/null
@@ -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" %}
index 9cc11f5351a4ff891aceb0bf4c3341c426d22fe4..0976a9e8b44bb434842414c6129aba8870d3668f 100644 (file)
@@ -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()
index e10409aab2074fa70640427fbaa454e306f73662..28d602246222fb406e9b51d4f713e518f2094e99 100644 (file)
@@ -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>
         </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 %}