]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - coffee_db.py
Reformat
[coffee/coffee-flask.git] / coffee_db.py
1 import sqlite3
2 import os
3
4 dbdir = os.path.dirname(__file__)
5 dbdef = os.path.join(dbdir, "coffee_db.sql")
6 dbfile = os.path.join(dbdir, "coffee.db")
7
8 def open_db():
9     conn = sqlite3.connect(dbfile)
10     c = conn.cursor()
11     return conn, c
12
13 def close_db(conn):
14     conn.commit()
15     conn.close()
16
17 def init_db():
18     conn, c = open_db()
19     with open(dbdef, "r") as f:
20         c.executescript(f.read())
21     close_db(conn)
22
23 def add_user(uid):
24     conn, c = open_db()
25     c.execute("insert or ignore into users (id) values (?)", (uid,))
26     close_db(conn)
27
28 def get_name(uid):
29     conn, c = open_db()
30     for name, in c.execute("select name from users where id = ?",(uid,)):
31         close_db(conn)
32         return name
33     close_db(conn)
34     return None
35
36
37 def name_user(uid, name):
38     conn, c = open_db()
39     c.execute("update users set name = ? where id = ?", (name, uid))
40     close_db(conn)
41
42 def list_users():
43     conn, c = open_db()
44     for row in c.execute("select * from users"):
45         print(row)
46     close_db(conn)
47
48
49 def add_coffee(uid, flavor, time=None):
50     conn, c = open_db()
51     print(uid, flavor, time)
52     if time is None:
53         c.execute("insert into coffees (id, flavor) values (?,?)", (uid,flavor))
54     else:
55         c.execute("insert into coffees (id, flavor, time) values (?,?,?)", (uid, flavor, time))
56     close_db(conn)
57
58
59 def list_coffees(uid=None):
60     c = conn.cursor()
61     if uid is None:
62         for row in c.execute("select id, time, flavor from coffees"):
63             print(row)
64     else:
65         for row in c.execute("select time, flavor from coffees where id = ?", (uid,)):
66             print(row)
67
68 def flavors():
69     conn, c = open_db()
70     res = [row for row, in c.execute("select distinct name from flavors")]
71     close_db(conn)
72     return res
73
74 def coffee_flavors(uid=None):
75     conn, c = open_db()
76
77     if uid is None:
78         res = list(c.execute("""
79             select f.name, count(c.flavor) from flavors f left join
80             (select * from coffees) c
81             on f.name=c.flavor group by f.name
82             """))
83     else:
84         res = list(c.execute("""
85             select f.name, count(c.flavor) from flavors f left join
86             (select * from coffees where id = ?) c
87             on f.name=c.flavor group by f.name
88             """, (uid,)))
89
90     close_db(conn)
91     return res
92
93
94 def coffee_history(uid=None):
95     conn, c = open_db()
96
97     if uid is None:
98         res = list(c.execute("""
99             select strftime('%d', ds.d),count(c.flavor),c.flavor from
100             (select num,date('now',-num || ' days') as d from days) ds
101             left join coffees c
102             on d = date(c.time) group by d, c.flavor
103             """))
104     else:
105         res = list(c.execute(
106             """
107             select strftime('%d', ds.d),count(c.flavor),c.flavor from
108             (select num,date('now',-num || ' days') as d from days) ds
109             left join
110             (select time,flavor from coffees where id = ?) c
111             on d = date(c.time) group by d, c.flavor
112             """
113             , (uid,)))
114
115     close_db(conn)
116     return res
117
118 def coffee_count(uid=None, start=None, stop=None):
119     conn, c = open_db()
120
121     args = []
122     clauses = []
123
124     if uid is not None:
125         clauses.append("id = ?")
126         args.append(uid)
127
128     if start is not None:
129         clauses.append("date(time) >= date('now', '-%d days')" % int(start))
130
131     if stop is not None:
132         clauses.append("date(time) <= date('now', '-%d days')" % int(stop))
133
134     for count, in c.execute(
135             "select count(*) from coffees where " +
136             " and ".join(clauses)
137             , args):
138         res = count
139
140     if not res:
141         res = "0"
142
143     return res