1 from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
2 from flask_cors import CORS
7 import matplotlib.pyplot as plt
8 from matplotlib.ticker import MaxNLocator
11 import coffee_db as db
14 from datetime import date, timedelta
16 from json import loads
17 from requests import post
21 CORS(app, supports_credentials=True)
22 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
29 return render_template('hello.html', name=db.get_name(uid))
30 return render_template('hello.html')
33 @app.route('/login', methods=["POST"])
34 @app.route('/login/<iid>')
36 if request.method == "POST":
37 iid = request.data.decode("utf-8")
44 db.add_user_identifier(iid, iid, "Default")
49 return redirect(url_for('user'))
54 session.pop('uid', None)
55 session.pop('iid', None)
56 return redirect(url_for('user'))
63 counts = db.drink_count(uid, 0)
64 return render_template('user.html',
65 name=db.get_name(uid),
66 flavors=[_name for (_name, _ord) in db.flavors()],
68 identifiers=db.list_user_identifiers(uid),
72 # TODO: Replace stamp parameter with proper cache control HTTP
74 return render_template('user.html', stamp=time.time())
77 @app.route('/user/rename')
79 name = request.args.get("name")
80 if name and "uid" in session:
82 db.name_user(uid, name)
83 return redirect(url_for('user'))
86 @app.route('/user/identifier/add', methods=["POST"])
87 def user_add_identifier():
88 if request.method == "POST":
90 if "uid" in session and "id" in json:
91 db.add_user_identifier(session["uid"], json["id"], 'None')
92 return redirect(url_for('user'))
95 @app.route('/user/identifier/rename', methods=["POST"])
96 def user_rename_identifier():
97 if request.method == "POST":
99 if "uid" in session and all(key in json for key in ["id", "name"]):
100 db.rename_user_identifier(session["uid"], json["id"], json["name"])
101 return redirect(url_for('user'))
104 @app.route('/user/identifier/disable', methods=["POST"])
105 def user_disable_identifier():
106 if request.method == "POST":
108 if "uid" in session and "id" in json:
109 db.disable_user_identifier(session["uid"], json["id"])
110 return logout() # force logout
113 @app.route("/coffee/graph_flavors")
114 def coffee_graph_flavors():
115 days = request.args.get('days', default = 0, type = int)
116 start = request.args.get('start', default = 0, type = int)
121 flavors, counts = zip(*db.coffee_flavors(uid, days, start))
123 flavors, counts = zip(*db.coffee_flavors(None, days, start))
124 fig = plt.figure(figsize=(3, 3))
125 ax = fig.add_subplot(111)
127 ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
128 ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
131 ax.set_title("Your taste")
133 ax.set_title("This week taste")
135 fig.savefig(b, format="svg", bbox_inches="tight")
137 return send_file(b, mimetype="image/svg+xml")
140 @app.route("/coffee/graph_history")
141 def coffee_graph_history():
145 hist = db.coffee_history(uid)
147 hist = db.coffee_history()
153 unix_days, counts, flavors = zip(*hist)
154 fig = plt.figure(figsize=(4, 3))
155 ax = fig.add_subplot(111)
157 list_flavor = [_name for (_name, _ord) in sorted(db.flavors(), key=lambda x: x[1])]
158 l = [{} for i in range(len(list_flavor))]
163 for(d, c, f) in zip(unix_days, counts, flavors):
167 for i in range(len(list_flavor)):
168 if f == list_flavor[i]:
173 z = list(0 for i in range(len(l[0])))
174 for flavor in range(len(list_flavor)):
175 sortedlist = [(k, l[flavor][k]) for k in sorted(l[flavor])]
176 x = [i[0] for i in sortedlist]
177 y = [i[1] for i in sortedlist]
178 ax.bar(range(len(x)), y, bottom=z)
179 z = [sum(i) for i in zip(y, z)]
181 unix_days = set(unix_days)
182 xdays = [i.strftime("%a") for i in [
183 date.today() - timedelta(j - 1) for j in
184 range(len(unix_days), 0, -1)]]
187 ax.set_xticks(range(len(unix_days)))
188 ax.set_xticklabels(xdays)
191 ax.set_title("Your week")
193 ax.set_title("This week total")
195 ax.yaxis.set_major_locator(MaxNLocator(integer=True))
196 fig.savefig(b, format="svg", bbox_inches="tight")
199 return send_file(b, mimetype="image/svg+xml")
202 @app.route("/coffee/add", methods=["POST"])
204 if request.method == "POST":
207 if "iid" in session and all(key in json for key in ["flavor", "time"]):
208 print("User/id '%s' had '%s' at %s" % (session["iid"], json["flavor"], json["time"]))
209 db.add_coffee(session["iid"], json["flavor"], json["time"])
210 return redirect(url_for('user'))
213 # TODO: Remove me - unused
214 @app.route("/coffee/count")
216 start = request.args.get("start")
217 stop = request.args.get("stop")
218 return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0))
223 response = make_response(render_template('main.js'))
224 response.headers['Content-Type'] = "text/javascript"
228 @app.route("/log", methods=["POST"])
230 if request.method == "POST":
231 data = request.data.decode("utf-8")
236 @app.route("/tellCoffeebot", methods=["POST"])
237 def tell_coffeebot():
238 err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
239 if request.method == "POST":
240 what = loads(request.data.decode("utf-8"))
242 with open(".coffee.conf", "r") as f:
243 conf = loads(f.read())
245 return "Config read error: '%s'! Please find in git history how the .coffee.conf file should look." \
248 res = post(conf["coffeebot"]["url"], json=what)
249 print("res is {}".format(res))
251 err = "No connection! No covfefe! We all die here!"
253 err = "Slack doesn't like our request! It's discrimination!"