From: Jaroslav Klapalek Date: Thu, 7 Mar 2019 13:47:03 +0000 (+0100) Subject: Add support for another beverages (namely club mate) X-Git-Url: https://rtime.felk.cvut.cz/gitweb/coffee/coffee-flask.git/commitdiff_plain/c0be73d43b24e8e7a223f0730489407025a2459d Add support for another beverages (namely club mate) 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. --- diff --git a/app.py b/app.py index ef9075b..62f79b8 100644 --- 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: diff --git a/coffee_db.py b/coffee_db.py index b7206fe..348113a 100644 --- a/coffee_db.py +++ b/coffee_db.py @@ -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) diff --git a/coffee_db.sql b/coffee_db.sql index 03a7e7c..acb42a2 100644 --- a/coffee_db.sql +++ b/coffee_db.sql @@ -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 (