]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Add support for another beverages (namely club mate)
authorJaroslav Klapalek <klapajar@fel.cvut.cz>
Thu, 7 Mar 2019 13:47:03 +0000 (14:47 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 8 Mar 2019 07:37:40 +0000 (08:37 +0100)
Required to run on the server database before using this patch:
```
alter table flavors add ord integer not null default 999;
update flavors set ord=1 where name='cappuccino';
update flavors set ord=2 where name='espresso';
update flavors set ord=3 where name='espresso lungo';
update flavors set ord=4 where name='latte macchiato';
```

This patch slightly changes structure of a `flavors` table. It adds
another column called `ord`, which is used for ordering of coffee
flavors / beverages on the graphs (to maintain the same color scheme).

Scripts `app.py` and `coffee_db.py` are modified to work with this
change.

app.py
coffee_db.py
coffee_db.sql

diff --git a/app.py b/app.py
index ef9075b629a104c895f2a2eaa6363faa8343c122..62f79b8bab23b316087d32c8c35cd3ab92aeaa7f 100644 (file)
--- a/app.py
+++ b/app.py
@@ -53,7 +53,7 @@ def user():
         uid = session["uid"]
         return render_template('user.html',
                                name=db.get_name(uid),
-                               flavors=db.flavors(),
+                               flavors=[_name for (_name, _ord) in db.flavors()],
                                count=db.coffee_count(uid, 0),
                                stamp=time.time()
                                )
@@ -115,7 +115,7 @@ def coffee_graph_history():
     fig = plt.figure(figsize=(4, 3))
     ax = fig.add_subplot(111)
 
-    list_flavor = sorted(db.flavors())
+    list_flavor = [_name for (_name, _ord) in sorted(db.flavors(), key=lambda x: x[1])]
     l = [{} for i in range(len(list_flavor))]
     for ll in l:
         for d in unix_days:
index b7206fe20563b910ac065b363cb41a6a9294f79b..348113ae41d394d2bb02b0ede633e5c93bf581b5 100644 (file)
@@ -56,7 +56,7 @@ def add_coffee(uid, flavor, time=None):
 
 def flavors():
     conn, c = open_db()
-    res = [row for row, in c.execute("select distinct name from flavors")]
+    res = list(c.execute("select distinct name, ord from flavors"))
     close_db(conn)
     return res
 
@@ -91,6 +91,7 @@ def coffee_flavors(uid=None, days=0, start=0):
         left join (select * from coffees
         """+query+""") c
         on f.name=c.flavor group by f.name
+        order by f.ord asc
         """, variables))
 
     close_db(conn)
index 03a7e7cd86d621bb5f4d66c6e5f58d671622d1e1..acb42a2edd712cc790d17da66b540c3a6189ec35 100644 (file)
@@ -6,14 +6,17 @@ create table if not exists users (
 );
 
 create table if not exists flavors (
-    name varchar(255) primary key not null
+    name varchar(255) primary key not null,
+    ord integer not null default 999
 );
 
 insert or ignore into flavors values
-    ("espresso"),
-    ("espresso lungo"),
-    ("cappuccino"),
-    ("latte macchiato")
+    ("espresso", 2),
+    ("espresso lungo", 3),
+    ("cappuccino", 1),
+    ("latte macchiato", 4),
+    ("Club-Mate 0,5 l", 5),
+    ("Club-Mate 0,33 l", 6)
 ;
 
 create table if not exists coffees (