]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Add `type` for flavors and build current consumption dynamically
authorJaroslav Klapalek <klapajar@fel.cvut.cz>
Wed, 11 Dec 2019 10:00:12 +0000 (11:00 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Mon, 13 Jan 2020 21:33:13 +0000 (22:33 +0100)
app.py
coffee_db.py
coffee_db.sql
templates/user.html

diff --git a/app.py b/app.py
index a3e30884c881159b823b200c79d3ce719d25bda7..7432fcbf64874705a4f646605db42c45fff17c93 100644 (file)
--- 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')
index f0ad4078bfe3c34223721ce539266bc06b624318..c7dbdcca22574f60ce134e2a450a88eea438f691 100644 (file)
@@ -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))
index 23011af167bf7f0dabe96d6024ecfa5269f5e1c1..51f83aa36974af4504360f62a5b5f5b7c24efcf7 100644 (file)
@@ -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 (
index e0be3101584d438f1e6554262a25a92325693e2d..5ad7b901584c6fa01eab306c41af6fc878197bc6 100644 (file)
@@ -7,8 +7,28 @@
 
     <p id="nextStep"></p>
 
-    {% if count or count_mate%}
-    <p>You've had {{ count }} coffee{% if count != 1 %}s{% endif %} and {{ count_mate }} Club-Mate today.</p>
+    {% if counts|length > 0 %}
+    <p>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.</p>
     {% endif %}
 
     <img src="{{ url_for('coffee_graph_history', _external=True, stamp=stamp) }}">