]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - app.py
Suppress Matplotlib's deprecation warning
[coffee/coffee-flask.git] / app.py
1 from flask import Flask, render_template, send_file, request, session, redirect, url_for, make_response
2 from flask_cors import CORS
3
4 import numpy as np
5 import matplotlib
6 matplotlib.use('Agg')
7 import matplotlib.pyplot as plt
8 from matplotlib.ticker import MaxNLocator
9 from io import BytesIO
10
11 import coffee_db as db
12 import time
13 import sys
14 from datetime import date, timedelta
15
16 from json import loads
17 from requests import post
18
19 db.init_db()
20 app = Flask(__name__)
21 CORS(app, supports_credentials=True)
22 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
23
24
25 @app.route('/')
26 def hello():
27     if "uid" in session:
28         uid = session["uid"]
29         return render_template('hello.html', name=db.get_name(uid))
30     return render_template('hello.html')
31
32
33 @app.route('/login', methods=["POST"])
34 @app.route('/login/<iid>')
35 def login(iid=None):
36     if request.method == "POST":
37         iid = request.data.decode("utf-8")
38     if iid is not None:
39         uid = db.get_uid(iid)
40
41         session["iid"] = iid
42
43         if uid is None:
44             db.add_user_identifier(iid, iid, "Default")
45             session["uid"] = iid
46             db.add_user(iid)
47         else:
48             session["uid"] = uid
49     return redirect(url_for('user'))
50
51
52 @app.route('/logout')
53 def logout():
54     session.pop('uid', None)
55     session.pop('iid', None)
56     return redirect(url_for('user'))
57
58
59 @app.route('/user')
60 def user():
61     if "uid" in session:
62         uid = session["uid"]
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()],
67                                counts=counts,
68                                identifiers=db.list_user_identifiers(uid),
69                                iid=session["iid"],
70                                stamp=time.time()
71                                )
72     # TODO: Replace stamp parameter with proper cache control HTTP
73     # headers in response
74     return render_template('user.html', stamp=time.time())
75
76
77 @app.route('/user/rename')
78 def user_rename():
79     name = request.args.get("name")
80     if name and "uid" in session:
81         uid = session["uid"]
82         db.name_user(uid, name)
83     return redirect(url_for('user'))
84
85
86 @app.route('/user/identifier/add', methods=["POST"])
87 def user_add_identifier():
88     if request.method == "POST":
89         json = request.json
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'))
93
94
95 @app.route('/user/identifier/rename', methods=["POST"])
96 def user_rename_identifier():
97     if request.method == "POST":
98         json = request.json
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'))
102
103
104 @app.route('/user/identifier/disable', methods=["POST"])
105 def user_disable_identifier():
106     if request.method == "POST":
107         json = request.json
108         if "uid" in session and "id" in json:
109             db.disable_user_identifier(session["uid"], json["id"])
110     return logout() # force logout
111
112
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)
117
118     b = BytesIO()
119     if "uid" in session:
120         uid = session["uid"]
121         flavors, counts = zip(*db.coffee_flavors(uid, days, start))
122     else:
123         flavors, counts = zip(*db.coffee_flavors(None, days, start))
124     fig = plt.figure(figsize=(3, 3))
125     ax = fig.add_subplot(111)
126     ax.set_aspect(1)
127     ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '',
128            normalize=True)
129     ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
130
131     if "uid" in session:
132         ax.set_title("Your taste")
133     else:
134         ax.set_title("This week taste")
135
136     fig.savefig(b, format="svg", bbox_inches="tight")
137     b.seek(0)
138     return send_file(b, mimetype="image/svg+xml")
139
140
141 @app.route("/coffee/graph_history")
142 def coffee_graph_history():
143     b = BytesIO()
144     if "uid" in session:
145         uid = session["uid"]
146         hist = db.coffee_history(uid)
147     else:
148         hist = db.coffee_history()
149     if hist == []:
150         unix_days = tuple()
151         counts = tuple()
152         flavors = tuple()
153     else:
154         unix_days, counts, flavors = zip(*hist)
155     fig = plt.figure(figsize=(4, 3))
156     ax = fig.add_subplot(111)
157
158     list_flavor = [_name for (_name, _ord) in sorted(db.flavors(), key=lambda x: x[1])]
159     l = [{} for i in range(len(list_flavor))]
160     for ll in l:
161         for d in unix_days:
162             ll[d] = 0
163
164     for(d, c, f) in zip(unix_days, counts, flavors):
165         if f is None:
166             continue
167         what_f = 0
168         for i in range(len(list_flavor)):
169             if f == list_flavor[i]:
170                 what_f = i
171                 break
172         l[what_f][d] += c
173
174     z = list(0 for i in range(len(l[0])))
175     for flavor in range(len(list_flavor)):
176         sortedlist = [(k, l[flavor][k]) for k in sorted(l[flavor])]
177         x = [i[0] for i in sortedlist]
178         y = [i[1] for i in sortedlist]
179         ax.bar(range(len(x)), y, bottom=z)
180         z = [sum(i) for i in zip(y, z)]
181
182     unix_days = set(unix_days)
183     xdays = [i.strftime("%a") for i in [
184         date.today() - timedelta(j - 1) for j in
185         range(len(unix_days), 0, -1)]]
186     xdays[-1] = "TDY"
187     xdays[-2] = "YDA"
188     ax.set_xticks(range(len(unix_days)))
189     ax.set_xticklabels(xdays)
190
191     if "uid" in session:
192         ax.set_title("Your week")
193     else:
194         ax.set_title("This week total")
195
196     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
197     fig.savefig(b, format="svg", bbox_inches="tight")
198     b.seek(0)
199     plt.close(fig)
200     return send_file(b, mimetype="image/svg+xml")
201
202
203 @app.route("/coffee/add", methods=["POST"])
204 def coffee_add():
205     if request.method == "POST":
206         json = request.json
207
208         if "iid" in session and all(key in json for key in ["flavor", "time"]):
209             print("User/id '%s' had '%s' at %s" % (session["iid"], json["flavor"], json["time"]))
210             db.add_coffee(session["iid"], json["flavor"], json["time"])
211     return redirect(url_for('user'))
212
213
214 # TODO: Remove me - unused
215 @app.route("/coffee/count")
216 def coffee_count():
217     start = request.args.get("start")
218     stop = request.args.get("stop")
219     return str(dict(db.drink_count(session.get("uid"), start, stop)).get("coffee", 0))
220
221
222 @app.route('/js')
223 def js():
224     response = make_response(render_template('main.js'))
225     response.headers['Content-Type'] = "text/javascript"
226     return response
227
228
229 @app.route("/log", methods=["POST"])
230 def log():
231     if request.method == "POST":
232         data = request.data.decode("utf-8")
233         print("Log:", data)
234         return data
235     return "nope"
236
237 @app.route("/tellCoffeebot", methods=["POST"])
238 def tell_coffeebot():
239     err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
240     if request.method == "POST":
241         what = loads(request.data.decode("utf-8"))
242     try:
243         with open(".coffee.conf", "r") as f:
244             conf = loads(f.read())
245     except:
246         return "Config read error: '%s'! Please find in git history how the .coffee.conf file should look." \
247             % sys.exc_info()[1]
248     try:
249         res = post(conf["coffeebot"]["url"], json=what)
250         print("res is {}".format(res))
251     except:
252         err = "No connection! No covfefe! We all die here!"
253     if not res.ok:
254         err = "Slack doesn't like our request! It's discrimination!"
255     return err