[PATCH v3] Add counting drink Club-Mate

The patch solves the problem of separate counting of individual types drinks. --- app.py | 15 +++++++++++++-- coffee_db.py | 11 ++--------- templates/user.html | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 62f79b8..36f10e7 100644 --- a/app.py +++ b/app.py @@ -51,10 +51,21 @@ def logout(): def user(): if "uid" in session: uid = session["uid"] + count=0 + count_mate=0 + data = db.drink_count(uid, 0) + if data != []: + key , value = zip(*data) + d = dict(zip(key,value)) + if "Club-Mate" in d: + count_mate=d["Club-Mate"] + if "Coffee" in d: + count=d["Coffee"] 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=count, + count_mate=count_mate, stamp=time.time() ) # TODO: Replace stamp parameter with proper cache control HTTP @@ -173,7 +184,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)["Coffee"]) @app.route('/js') diff --git a/coffee_db.py b/coffee_db.py index 348113a..26440b9 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -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,6 @@ 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" + res = list(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)) return res diff --git a/templates/user.html b/templates/user.html index 7e549b9..2cc3dec 100644 --- a/templates/user.html +++ b/templates/user.html @@ -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) }}"> -- 2.11.0

On Tue, Apr 09 2019, Tomas Prochazka wrote:
The patch solves the problem of separate counting of individual types drinks. --- app.py | 15 +++++++++++++-- coffee_db.py | 11 ++--------- templates/user.html | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/app.py b/app.py index 62f79b8..36f10e7 100644 --- a/app.py +++ b/app.py @@ -51,10 +51,21 @@ def logout(): def user(): if "uid" in session: uid = session["uid"] + count=0 + count_mate=0 + data = db.drink_count(uid, 0) + if data != []: + key , value = zip(*data) + d = dict(zip(key,value)) + if "Club-Mate" in d: + count_mate=d["Club-Mate"] + if "Coffee" in d: + count=d["Coffee"]
Tohle je pořád zbytečně složitý. Když v drink_count() bude: return dict(c.execute("SELECT CASE WHEN flavor LIKE 'Club%' THEN 'Club-Mate' ELSE 'Coffee' END AS drink,COUNT() FROM coffees GROUP BY drink")) tak sem napíšeš: 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=count, + count_mate=count_mate,
a tady: count=counts.get("Coffee", 0) count_mate=counts.get("Club-Mate", 0) -Michal
participants (2)
-
Michal Sojka
-
Tomas Prochazka