1 from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
2 from flask_cors import CORS
5 import matplotlib.pyplot as plt
6 from matplotlib.ticker import MaxNLocator
12 from datetime import date, timedelta, datetime, timezone
14 from json import loads
15 from requests import post
23 CORS(app, supports_credentials=True)
24 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
27 # https://github.com/matplotlib/matplotlib/issues/13723. Despite the
28 # issue is closed, the problem is still there. See
29 # https://github.com/matplotlib/matplotlib/issues/13723#issuecomment-761302131.
30 plot_lock = threading.Lock()
32 # Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
33 # updated to our needs
34 def humanize_ts(time, max_interval="years"):
35 """Convert date/time in ISO format to relative, human readable string.
37 Example return values: 'an hour ago', 'Yesterday', '3 months ago',
40 When optional max_interval is set to "days", the return value will
41 report at most the number of days ago, not week, months or years.
43 if jinja2.is_undefined(time):
45 if max_interval not in ["years", "days"]:
48 diff = datetime.now(timezone.utc) - time
49 second_diff = diff.seconds
53 return 'in the future'
59 return str(int(second_diff)) + " seconds ago"
62 if second_diff < 3600:
63 return str(int(second_diff / 60)) + " minutes ago"
64 if second_diff < 7200:
66 if second_diff < 86400:
67 return str(int(second_diff / 3600)) + " hours ago"
70 if day_diff < 7 or max_interval == "days":
71 return str(day_diff) + " days ago"
73 return str(int(day_diff / 7)) + " weeks ago"
75 return str(int(day_diff / 30)) + " months ago"
76 return str(int(day_diff / 365)) + " years ago"
79 app.jinja_env.filters['humanize'] = humanize_ts
82 def days_filter(time):
83 """Return the number of days elapsed since time."""
84 if jinja2.is_undefined(time):
86 diff = datetime.now(timezone.utc) - time
90 app.jinja_env.filters['days'] = days_filter
95 return render_template('hello.html')
100 # TODO: Replace stamp parameter with proper cache control HTTP
101 # headers in response
102 return render_template('home.html',
104 last_events=db.last_events())
107 @app.route('/login', methods=["POST"])
108 @app.route('/login/<iid>')
110 if request.method == "POST":
111 iid = request.data.decode("utf-8")
113 uid = db.get_uid(iid)
118 db.add_user_identifier(iid, iid, "Default")
123 return redirect(url_for('user'))
126 @app.route('/logout')
128 session.pop('uid', None)
129 session.pop('iid', None)
130 return redirect(url_for('home'))
137 counts = db.drink_count(uid, 0)
138 return render_template('user.html',
139 name=db.get_name(uid),
140 flavors=[_name for (_name, _ord) in db.flavors()],
142 identifiers=db.list_user_identifiers(uid),
145 last_events=db.last_events()
147 return redirect(url_for('home'))
150 @app.route('/user/rename')
152 name = request.args.get("name")
153 if name and "uid" in session:
155 db.name_user(uid, name)
156 return redirect(url_for('user'))
159 @app.route('/user/identifier/add', methods=["POST"])
160 def user_add_identifier():
161 if request.method == "POST":
163 if "uid" in session and "id" in json:
164 db.add_user_identifier(session["uid"], json["id"], 'None')
165 return redirect(url_for('user'))
168 @app.route('/user/identifier/rename', methods=["POST"])
169 def user_rename_identifier():
170 if request.method == "POST":
172 if "uid" in session and all(key in json for key in ["id", "name"]):
173 db.rename_user_identifier(session["uid"], json["id"], json["name"])
174 return redirect(url_for('user'))
177 @app.route('/user/identifier/disable', methods=["POST"])
178 def user_disable_identifier():
179 if request.method == "POST":
181 if "uid" in session and "id" in json:
182 db.disable_user_identifier(session["uid"], json["id"])
183 return logout() # force logout
186 @app.route("/coffee/graph_flavors")
187 def coffee_graph_flavors():
188 days = request.args.get('days', default=0, type=int)
189 start = request.args.get('start', default=0, type=int)
194 flavors, counts = zip(*db.coffee_flavors(uid, days, start))
196 flavors, counts = zip(*db.coffee_flavors(None, days, start))
197 fig = plt.figure(figsize=(3, 3))
198 ax = fig.add_subplot(111)
200 if "normalize" in matplotlib.pyplot.pie.__code__.co_varnames:
201 # Matplotlib >= 3.3.0
202 ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '',
203 normalize=sum(counts) != 0)
206 ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
208 ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
211 ax.set_title("Your taste")
213 ax.set_title("This week taste")
216 fig.savefig(b, format="svg", bbox_inches="tight")
219 return send_file(b, mimetype="image/svg+xml")
222 @app.route("/coffee/graph_history")
223 def coffee_graph_history():
227 hist = db.coffee_history(uid)
229 hist = db.coffee_history()
235 unix_days, counts, flavors = zip(*hist)
236 fig = plt.figure(figsize=(4, 3))
237 ax = fig.add_subplot(111)
239 list_flavor = [_name for (_name, _ord) in sorted(db.flavors(), key=lambda x: x[1])]
240 l = [{} for i in range(len(list_flavor))]
245 for(d, c, f) in zip(unix_days, counts, flavors):
249 for i in range(len(list_flavor)):
250 if f == list_flavor[i]:
255 z = list(0 for i in range(len(l[0])))
256 for flavor in range(len(list_flavor)):
257 sortedlist = [(k, l[flavor][k]) for k in sorted(l[flavor])]
258 x = [i[0] for i in sortedlist]
259 y = [i[1] for i in sortedlist]
260 ax.bar(range(len(x)), y, bottom=z)
261 z = [sum(i) for i in zip(y, z)]
263 unix_days = set(unix_days)
264 xdays = [i.strftime("%a") for i in [
265 date.today() - timedelta(j - 1) for j in
266 range(len(unix_days), 0, -1)]]
269 ax.set_xticks(range(len(unix_days)))
270 ax.set_xticklabels(xdays)
273 ax.set_title("Your week")
275 ax.set_title("This week total")
277 ax.yaxis.set_major_locator(MaxNLocator(integer=True))
279 fig.savefig(b, format="svg", bbox_inches="tight")
282 return send_file(b, mimetype="image/svg+xml")
285 @app.route("/coffee/add", methods=["POST"])
287 if request.method == "POST":
290 if "iid" in session and all(key in json for key in ["flavor", "time"]):
291 print("User/id '%s' had '%s' at %s" % (session["iid"], json["flavor"], json["time"]))
292 db.add_coffee(session["iid"], json["flavor"], json["time"])
293 return redirect(url_for('user'))
296 @app.route("/event", methods=["POST"])
299 print("User '%(uid)s' registered event %(event_name)s at %(time)s" % json)
300 db.add_event(json["uid"], json["event_name"], json["time"])
301 return redirect(url_for('user'))
306 response = make_response(render_template('main.js'))
307 response.headers['Content-Type'] = "text/javascript"
311 @app.route("/log", methods=["POST"])
313 if request.method == "POST":
314 data = request.data.decode("utf-8")
320 @app.route("/tellCoffeebot", methods=["POST"])
321 def tell_coffeebot():
322 err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
323 if request.method == "POST":
324 what = loads(request.data.decode("utf-8"))
326 with open(".coffee.conf", "r") as f:
327 conf = loads(f.read())
329 return "Config read error: '%s'! Please find in git history how the .coffee.conf file should look." \
332 res = post(conf["coffeebot"]["url"], json=what)
333 print("res is {}".format(res))
335 err = "No connection! No covfefe! We all die here!"
337 err = "Slack doesn't like our request! It's discrimination!"