# Try to remove old relation of identifier
# As 'delete or ignore' does not exist, workaround is used
- res = c.execute("select * from identifiers where id = ? and active = ?", (iid, 0, ))
+ res = c.execute("select * from identifiers where id = ? and active = 0", (iid, ))
# This is True when some rows were found before; delete old relation
if res.fetchone():
- res = c.execute("delete from identifiers where id = ? and active = ?", (iid, 0, ))
+ print("Deleting uid:%s id:%s" % (uid, iid))
+ res = c.execute("delete from identifiers where id = ? and active = 0", (iid, ))
# Add new relation
res = c.execute("insert into identifiers (userid, id, name) values (?, ?, ?)", (uid, iid, name, ))
def disable_user_identifier(uid, iid):
conn, c = open_db()
- c.execute("update identifiers set active = ? where userid = ? and id = ?", (0, uid, iid, ))
+ c.execute("update identifiers set active = 0 where userid = ? and id = ?", (uid, iid, ))
close_db(conn)
def get_name(uid):
return res
def drink_count(uid=None, start=None, stop=None):
+ """Return a list of tuples ('<drink type>', <count>).
+
+ >>> drink_count(stop=0)
+ [('coffee', 7066), ('Club-Mate', 497), ('tea', 1)]
+ """
conn, c = open_db()
args = []
if stop is not None:
clauses.append("date(time, 'localtime') <= date('now', 'localtime', '-%d days')" % int(stop))
- return dict(c.execute("select case when flavor like 'Club%' then 'Club-Mate' else 'Coffee' end as drink, count(*) "
- "from coffees co left join identifiers ids on co.id = ids.id where "
- + " and ".join(clauses) + " group by drink", args))
+ return list(c.execute("select fl.type, count(*) from coffees co "
+ "left join flavors fl on co.flavor = fl.name "
+ "left join identifiers ids on co.id = ids.id where "
+ + " and ".join(clauses) + " group by fl.type "
+ "order by fl.ord asc", args))