from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
from flask_cors import CORS
-import numpy as np
import matplotlib
-matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from io import BytesIO
from requests import post
import jinja2
+matplotlib.use('Agg')
+
db.init_db()
app = Flask(__name__)
CORS(app, supports_credentials=True)
json = request.json
if "uid" in session and "id" in json:
db.disable_user_identifier(session["uid"], json["id"])
- return logout() # force logout
+ return logout() # force logout
@app.route("/coffee/graph_flavors")
def coffee_graph_flavors():
- days = request.args.get('days', default = 0, type = int)
- start = request.args.get('start', default = 0, type = int)
+ days = request.args.get('days', default=0, type=int)
+ start = request.args.get('start', default=0, type=int)
b = BytesIO()
if "uid" in session:
return data
return "nope"
+
@app.route("/tellCoffeebot", methods=["POST"])
def tell_coffeebot():
err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
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>).