dbdef = os.path.join(dbdir, "coffee_db.sql")
dbfile = os.path.join(dbdir, "coffee.db")
+
def open_db():
conn = sqlite3.connect(dbfile)
c = conn.cursor()
return conn, c
+
def close_db(conn):
conn.commit()
conn.close()
+
def init_db():
conn, c = open_db()
with open(dbdef, "r") as f:
c.executescript(f.read())
close_db(conn)
+
def add_user(uid):
conn, c = open_db()
c.execute("insert or ignore into users (id) values (?)", (uid,))
close_db(conn)
+
def add_user_identifier(uid, iid, name):
# Check if this identifier is not currently associated with different account
if not get_uid(iid):
close_db(conn)
+
def disable_user_identifier(uid, iid):
conn, c = open_db()
c.execute("update identifiers set active = 0 where userid = ? and id = ?", (uid, iid, ))
close_db(conn)
+
def get_name(uid):
conn, c = open_db()
- for name, in c.execute("select name from users where id = ?",(uid,)):
+ for name, in c.execute("select name from users where id = ?", (uid,)):
close_db(conn)
return name
close_db(conn)
return None
+
def get_uid(iid):
conn, c = open_db()
res = list(c.execute("""
return res[0][0] if len(res) > 0 else None
+
def name_user(uid, name):
conn, c = open_db()
c.execute("update users set name = ? where id = ?", (name, uid))
close_db(conn)
+
def rename_user_identifier(uid, iid, name):
conn, c = open_db()
c.execute("update identifiers set name = ? where userid = ? and id = ?", (name, uid, iid, ))
close_db(conn)
+
def list_users():
conn, c = open_db()
for row in c.execute("select * from users"):
print(row)
close_db(conn)
+
def list_user_identifiers(uid):
conn, c = open_db()
res = list(c.execute("""
def add_coffee(uid, flavor, time=None):
conn, c = open_db()
if time is None:
- c.execute("insert into coffees (id, flavor) values (?,?)", (uid,flavor))
+ c.execute("insert into coffees (id, flavor) values (?,?)", (uid, flavor))
else:
c.execute("insert or ignore into coffees (id, flavor, time) values (?,?,?)", (uid, flavor, time))
close_db(conn)
close_db(conn)
return res
+
def coffee_flavors(uid=None, days=0, start=0):
- """Returns flavor statistics for team/user during selected period/since beginning.
+ """Returns flavor statistics for team/user during selected
+ period/since beginning.
days -- number of days for computation
start -- shift size from the current time
variables = list()
if days is not None and days != 0:
- query += " where date(time) between date('now', 'localtime', '-"+ str(days+start-1) +" days') and date('now', 'localtime', '-"+ str(start) +" days')"
+ query += " where date(time) between date('now', 'localtime', '-" + str(days+start-1) + " days') and date('now', 'localtime', '-" + str(start) + " days')"
if uid is not None:
query += " and ids.userid = ? and ids.active"
close_db(conn)
return res
+
def coffee_history(uid=None):
conn, c = open_db()
left join
(select date(time, 'localtime') as time,flavor from coffees co left join identifiers ids on co.id = ids.id where ids.userid = ? and ids.active) c
on d = date(c.time) group by d, c.flavor
- """
- , (uid,)))
+ """,
+ (uid,)))
close_db(conn)
return res
+
def drink_count(uid=None, start=None, stop=None):
"""Return a list of tuples ('<drink type>', <count>).