From c912eccdcb3e65783fe9d3e035978b22a8b9a3f8 Mon Sep 17 00:00:00 2001 From: Jaroslav Klapalek Date: Wed, 11 Dec 2019 11:00:12 +0100 Subject: [PATCH] Add `type` for flavors and build current consumption dynamically --- app.py | 5 ++--- coffee_db.py | 8 +++++--- coffee_db.sql | 17 +++++++++-------- templates/user.html | 24 ++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index a3e3088..7432fcb 100644 --- a/app.py +++ b/app.py @@ -64,8 +64,7 @@ def user(): return render_template('user.html', name=db.get_name(uid), flavors=[_name for (_name, _ord) in db.flavors()], - count=counts.get("Coffee", 0), - count_mate=counts.get("Club-Mate", 0), + counts=counts, identifiers=db.list_user_identifiers(uid), iid=session["iid"], stamp=time.time() @@ -215,7 +214,7 @@ def coffee_add(): def coffee_count(): start = request.args.get("start") stop = request.args.get("stop") - return str(db.drink_count(session.get("uid"), start, stop).get("Coffee", 0)) + return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0)) @app.route('/js') diff --git a/coffee_db.py b/coffee_db.py index f0ad407..c7dbdcc 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -183,6 +183,8 @@ def drink_count(uid=None, start=None, stop=None): if stop is not None: clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop)) - return dict(c.execute("select case when flavor like 'Club%' then 'Club-Mate' else 'Coffee' end as drink, count(*) " - "from coffees co left join identifiers ids on co.id = ids.id where " - + " and ".join(clauses) + " group by drink", args)) + return list(c.execute("select fl.type, count(*) from coffees co " + "left join flavors fl on co.flavor = fl.name " + "left join identifiers ids on co.id = ids.id where " + + " and ".join(clauses) + " group by fl.type " + "order by fl.ord asc", args)) diff --git a/coffee_db.sql b/coffee_db.sql index 23011af..51f83aa 100644 --- a/coffee_db.sql +++ b/coffee_db.sql @@ -7,17 +7,18 @@ create table if not exists users ( create table if not exists flavors ( name varchar(255) primary key not null, - ord integer not null default 999 + ord integer not null default 999, + type varchar(24) not null default "" ); insert or ignore into flavors values - ("espresso", 2), - ("espresso lungo", 3), - ("cappuccino", 1), - ("latte macchiato", 4), - ("Club-Mate 0,5 l", 5), - ("Club-Mate 0,33 l", 6), - ("tea", 7) + ("espresso", 2, "coffee"), + ("espresso lungo", 3, "coffee"), + ("cappuccino", 1, "coffee"), + ("latte macchiato", 4, "coffee"), + ("Club-Mate 0,5 l", 5, "Club-Mate"), + ("Club-Mate 0,33 l", 6, "Club-Mate"), + ("tea", 7, "tea") ; create table if not exists coffees ( diff --git a/templates/user.html b/templates/user.html index e0be310..5ad7b90 100644 --- a/templates/user.html +++ b/templates/user.html @@ -7,8 +7,28 @@

- {% if count or count_mate%} -

You've had {{ count }} coffee{% if count != 1 %}s{% endif %} and {{ count_mate }} Club-Mate today.

+ {% if counts|length > 0 %} +

You've had + {% for c in counts %} + {%- if counts|length > 1 -%} + {%- if loop.last -%} + {%- if counts|length > 2 -%} + , + {% endif %} + and + {%- elif not loop.first -%} + , + {% endif %} + {% endif %} + {{ c[1] }} + {% if c[0] == "" %} + other beverage + {%- else -%} + {{ c[0] }} + {%- endif -%} + {%- if c[1] != 1 -%}s{%- endif -%} + {% endfor %} + today.

{% endif %} -- 2.39.2