]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - app.py
Do not return empty string for future times
[coffee/coffee-flask.git] / app.py
diff --git a/app.py b/app.py
index 0e0564aa398d4ad8fc256edf80852914b6ad097e..9c6f6d168b0c0cacd3fe5562b19253c96c21e5de 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
@@ -17,6 +15,8 @@ from json import loads
 from requests import post
 import jinja2
 
+matplotlib.use('Agg')
+
 db.init_db()
 app = Flask(__name__)
 CORS(app, supports_credentials=True)
@@ -41,7 +41,7 @@ def humanize_ts(time):
     day_diff = diff.days
 
     if day_diff < 0:
-        return ''
+        return 'in the future'
 
     if day_diff == 0:
         if second_diff < 10:
@@ -115,11 +115,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')
@@ -155,13 +158,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:
@@ -265,6 +268,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():
@@ -288,6 +299,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!"