]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - coffee_db.py
Workaround the incompatibility created by Matplotlib 3.3.0
[coffee/coffee-flask.git] / coffee_db.py
index 072fe92f19e937bd30158c1b22db5013d8d7b0fc..25551f90fed08e2a54dd1ef53f62c50fabbd2f99 100644 (file)
@@ -32,11 +32,12 @@ def add_user_identifier(uid, iid, name):
 
         # 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, ))
@@ -45,7 +46,7 @@ def add_user_identifier(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):
@@ -167,6 +168,11 @@ def coffee_history(uid=None):
     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 = []
@@ -182,6 +188,8 @@ def drink_count(uid=None, start=None, stop=None):
     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))