]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Separate counting of coffees and club-mate
authorTomas Prochazka <tomas.prochazka@cvut.cz>
Tue, 9 Apr 2019 14:21:26 +0000 (16:21 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Tue, 9 Apr 2019 14:44:22 +0000 (16:44 +0200)
The patch solves the problem of separate counting of individual types
of drinks.

[commit message updated by M. Sojka]

app.py
coffee_db.py
templates/user.html

diff --git a/app.py b/app.py
index 62f79b8bab23b316087d32c8c35cd3ab92aeaa7f..b1f50cc105ba73d4b6b90e0d23408ac2f83df74b 100644 (file)
--- a/app.py
+++ b/app.py
@@ -51,10 +51,12 @@ 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),
+                               count=counts.get("Coffee", 0),
+                               count_mate=counts.get("Club-Mate", 0),
                                stamp=time.time()
                                )
     # TODO: Replace stamp parameter with proper cache control HTTP
@@ -173,7 +175,7 @@ def coffee_add():
 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(db.drink_count(session.get("uid"), start, stop).get("Coffee", 0))
 
 
 @app.route('/js')
index 348113ae41d394d2bb02b0ede633e5c93bf581b5..328d603fff5bd5136240a96d06cdefb796f2068c 100644 (file)
@@ -122,7 +122,7 @@ def coffee_history(uid=None):
     close_db(conn)
     return res
 
-def coffee_count(uid=None, start=None, stop=None):
+def drink_count(uid=None, start=None, stop=None):
     conn, c = open_db()
 
     args = []
@@ -138,13 +138,4 @@ def coffee_count(uid=None, start=None, stop=None):
     if stop is not None:
         clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop))
 
-    for count, in c.execute(
-            "select count(*) from coffees where " +
-            " and ".join(clauses)
-            , args):
-        res = count
-
-    if not res:
-        res = "0"
-
-    return res
+    return dict(c.execute("SELECT CASE WHEN flavor LIKE 'Club%' THEN 'Club-Mate' ELSE 'Coffee' END AS drink,COUNT() FROM coffees where " + " and ".join(clauses) + " GROUP BY drink", args))
index 7e549b91f0197610a7fc7f2586bd064d287e8143..2cc3dec4b7c50a4cbbe3e1aeed35b9051420ff3c 100644 (file)
@@ -7,8 +7,8 @@
 
     <p id="nextStep"></p>
 
-    {% if count %}
-    <p>You've had {{ count }} coffee{% if count != 1 %}s{% endif %} today.</p>
+    {% if count or count_mate%}
+    <p>You've had {{ count }} coffee{% if count != 1 %}s{% endif %} and {{ count_mate }} Club-Mate today.</p>
     {% endif %}
 
     <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}">