]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - app.py
Colorize milk cleaning event box after some days without cleaning
[coffee/coffee-flask.git] / app.py
diff --git a/app.py b/app.py
index d009705b817abd921fcbdbcbfc71e56180bd2069..06c6be2c9cf36b99167f528ada33e1c765ffd349 100644 (file)
--- a/app.py
+++ b/app.py
@@ -1,9 +1,7 @@
 from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
 from flask_cors import CORS
 
-import numpy as np
 import matplotlib
-matplotlib.use('Agg')
 import matplotlib.pyplot as plt
 from matplotlib.ticker import MaxNLocator
 from io import BytesIO
@@ -11,10 +9,13 @@ 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
+
+matplotlib.use('Agg')
 
 db.init_db()
 app = Flask(__name__)
@@ -22,6 +23,67 @@ 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, max_interval="years"):
+    """Convert date/time in ISO format to relative, human readable string.
+
+    Example return values: 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc.
+
+    When optional max_interval is set to "days", the return value will
+    report at most the number of days ago, not week, months or years.
+    """
+    if jinja2.is_undefined(time):
+        return time
+    if max_interval not in ["years", "days"]:
+        raise ValueError
+
+    diff = datetime.now(timezone.utc) - time
+    second_diff = diff.seconds
+    day_diff = diff.days
+
+    if day_diff < 0:
+        return 'in the future'
+
+    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 or max_interval == "days":
+        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
+
+
+def days_filter(time):
+    """Return the number of days elapsed since time."""
+    if jinja2.is_undefined(time):
+        return time
+    diff = datetime.now(timezone.utc) - time
+    return diff.days
+
+
+app.jinja_env.filters['days'] = days_filter
+
+
 @app.route('/')
 def hello():
     if "uid" in session:
@@ -67,11 +129,14 @@ def user():
                                counts=counts,
                                identifiers=db.list_user_identifiers(uid),
                                iid=session["iid"],
-                               stamp=time.time()
+                               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')
@@ -107,13 +172,13 @@ def user_disable_identifier():
         json = request.json
         if "uid" in session and "id" in json:
             db.disable_user_identifier(session["uid"], json["id"])
-    return logout() # force logout
+    return logout()  # force logout
 
 
 @app.route("/coffee/graph_flavors")
 def coffee_graph_flavors():
-    days = request.args.get('days', default = 0, type = int)
-    start = request.args.get('start', default = 0, type = int)
+    days = request.args.get('days', default=0, type=int)
+    start = request.args.get('start', default=0, type=int)
 
     b = BytesIO()
     if "uid" in session:
@@ -125,9 +190,11 @@ def coffee_graph_flavors():
     ax = fig.add_subplot(111)
     ax.set_aspect(1)
     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))
@@ -215,6 +282,14 @@ def coffee_add():
     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():
@@ -238,6 +313,7 @@ def log():
         return data
     return "nope"
 
+
 @app.route("/tellCoffeebot", methods=["POST"])
 def tell_coffeebot():
     err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"