
Akorát jsem zapomněl připsat, že je potřeba upravit stávající tabulku `flavors`: ``` alter table flavors add ord integer not null default 999; insert into flavors values ('club mate', 5); 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'; ``` J. Dne 06. 02. 19 v 20:42 Jaroslav Klapalek napsal(a):
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. --- Drobná úprava skriptu, abychom zde mohli zaznamenávat i jiné nápoje -- v tomto případě jde o club mate. Tím pádem nebudeme potřebovat žádné další zařízení na počítání. (Záznam lze provést tlačítkem na spodní části obrazovky po přihlášení). app.py | 4 ++-- coffee_db.py | 3 ++- coffee_db.sql | 12 +++++++----- 3 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/app.py b/app.py index d66eff1..a172f5d 100644 --- a/app.py +++ b/app.py @@ -49,7 +49,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() ) @@ -111,7 +111,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 7e28f92..8e65c43 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 d9f7d8e..77a2f52 100644 --- a/coffee_db.sql +++ b/coffee_db.sql @@ -6,14 +6,16 @@ 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", 5) ;
create table if not exists coffees (