]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - app.py
Add event logging
[coffee/coffee-flask.git] / app.py
diff --git a/app.py b/app.py
index 62f79b8bab23b316087d32c8c35cd3ab92aeaa7f..14a26842d4faccf78c975f5e327bb3485f21f565 100644 (file)
--- a/app.py
+++ b/app.py
@@ -11,10 +11,11 @@ from io import BytesIO
 import coffee_db as db
 import time
 import sys
-from datetime import date, timedelta
+from datetime import date, timedelta, datetime, timezone
 
 from json import loads
 from requests import post
+import jinja2
 
 db.init_db()
 app = Flask(__name__)
@@ -22,6 +23,53 @@ CORS(app, supports_credentials=True)
 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
 
 
+# Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
+# updated to our needs
+def humanize_ts(time):
+    """
+    Convert date in ISO format to relative, human readable string
+    like 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc
+    """
+    if jinja2.is_undefined(time):
+        return time
+    now = datetime.now(timezone.utc)
+    if time[-1] == 'Z':     # Convert Zulu time zone to datetime compatible format
+        time = time[0:-1] + '+00:00'
+    diff = now - datetime.fromisoformat(time)
+    second_diff = diff.seconds
+    day_diff = diff.days
+
+    if day_diff < 0:
+        return ''
+
+    if day_diff == 0:
+        if second_diff < 10:
+            return "just now"
+        if second_diff < 60:
+            return str(int(second_diff)) + " seconds ago"
+        if second_diff < 120:
+            return "a minute ago"
+        if second_diff < 3600:
+            return str(int(second_diff / 60)) + " minutes ago"
+        if second_diff < 7200:
+            return "an hour ago"
+        if second_diff < 86400:
+            return str(int(second_diff / 3600)) + " hours ago"
+    if day_diff == 1:
+        return "Yesterday"
+    if day_diff < 7:
+        return str(day_diff) + " days ago"
+    if day_diff < 31:
+        return str(int(day_diff / 7)) + " weeks ago"
+    if day_diff < 365:
+        return str(int(day_diff / 30)) + " months ago"
+    return str(int(day_diff / 365)) + " years ago"
+
+
+app.jinja_env.filters['humanize'] = humanize_ts
+
+
 @app.route('/')
 def hello():
     if "uid" in session:
@@ -31,19 +79,28 @@ def hello():
 
 
 @app.route('/login', methods=["POST"])
-@app.route('/login/<uid>')
-def login(uid=None):
+@app.route('/login/<iid>')
+def login(iid=None):
     if request.method == "POST":
-        uid = request.data.decode("utf-8")
-    if uid is not None:
-        db.add_user(uid)
-        session["uid"] = uid
+        iid = request.data.decode("utf-8")
+    if iid is not None:
+        uid = db.get_uid(iid)
+
+        session["iid"] = iid
+
+        if uid is None:
+            db.add_user_identifier(iid, iid, "Default")
+            session["uid"] = iid
+            db.add_user(iid)
+        else:
+            session["uid"] = uid
     return redirect(url_for('user'))
 
 
 @app.route('/logout')
 def logout():
     session.pop('uid', None)
+    session.pop('iid', None)
     return redirect(url_for('user'))
 
 
@@ -51,15 +108,21 @@ def logout():
 def user():
     if "uid" in session:
         uid = session["uid"]
+        counts = db.drink_count(uid, 0)
         return render_template('user.html',
                                name=db.get_name(uid),
                                flavors=[_name for (_name, _ord) in db.flavors()],
-                               count=db.coffee_count(uid, 0),
-                               stamp=time.time()
+                               counts=counts,
+                               identifiers=db.list_user_identifiers(uid),
+                               iid=session["iid"],
+                               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())
+    return render_template('user.html', stamp=time.time(),
+                           last_events=db.last_events(),
+                           )
 
 
 @app.route('/user/rename')
@@ -71,6 +134,33 @@ def user_rename():
     return redirect(url_for('user'))
 
 
+@app.route('/user/identifier/add', methods=["POST"])
+def user_add_identifier():
+    if request.method == "POST":
+        json = request.json
+        if "uid" in session and "id" in json:
+            db.add_user_identifier(session["uid"], json["id"], 'None')
+    return redirect(url_for('user'))
+
+
+@app.route('/user/identifier/rename', methods=["POST"])
+def user_rename_identifier():
+    if request.method == "POST":
+        json = request.json
+        if "uid" in session and all(key in json for key in ["id", "name"]):
+            db.rename_user_identifier(session["uid"], json["id"], json["name"])
+    return redirect(url_for('user'))
+
+
+@app.route('/user/identifier/disable', methods=["POST"])
+def user_disable_identifier():
+    if request.method == "POST":
+        json = request.json
+        if "uid" in session and "id" in json:
+            db.disable_user_identifier(session["uid"], json["id"])
+    return logout() # force logout
+
+
 @app.route("/coffee/graph_flavors")
 def coffee_graph_flavors():
     days = request.args.get('days', default = 0, type = int)
@@ -85,7 +175,14 @@ def coffee_graph_flavors():
     fig = plt.figure(figsize=(3, 3))
     ax = fig.add_subplot(111)
     ax.set_aspect(1)
-    ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
+    if "normalize" in matplotlib.pyplot.pie.__code__.co_varnames:
+        # Matplotlib >= 3.3.0
+        ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '',
+               normalize=True)
+    else:
+        # Matplotlib < 3.3.0
+        ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
+
     ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
 
     if "uid" in session:
@@ -164,16 +261,27 @@ def coffee_graph_history():
 def coffee_add():
     if request.method == "POST":
         json = request.json
-        print("User '%(uid)s' had '%(flavor)s' at %(time)s" % json)
-        db.add_coffee(json["uid"], json["flavor"], json["time"])
+
+        if "iid" in session and all(key in json for key in ["flavor", "time"]):
+            print("User/id '%s' had '%s' at %s" % (session["iid"], json["flavor"], json["time"]))
+            db.add_coffee(session["iid"], json["flavor"], json["time"])
+    return redirect(url_for('user'))
+
+
+@app.route("/event", methods=["POST"])
+def event_add():
+    json = request.json
+    print("User '%(uid)s' registered event %(event_name)s at %(time)s" % json)
+    db.add_event(json["uid"], json["event_name"], json["time"])
     return redirect(url_for('user'))
 
 
+# TODO: Remove me - unused
 @app.route("/coffee/count")
 def coffee_count():
     start = request.args.get("start")
     stop = request.args.get("stop")
-    return str(db.coffee_count(session.get("uid"), start, stop))
+    return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0))
 
 
 @app.route('/js')