]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - app.py
17d2a7c14fcc36d565854e5d021af8dfbab7e80b
[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     ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
129
130     if "uid" in session:
131         ax.set_title("Your taste")
132     else:
133         ax.set_title("This week taste")
134
135     fig.savefig(b, format="svg", bbox_inches="tight")
136     b.seek(0)
137     return send_file(b, mimetype="image/svg+xml")
138
139
140 @app.route("/coffee/graph_history")
141 def coffee_graph_history():
142     b = BytesIO()
143     if "uid" in session:
144         uid = session["uid"]
145         hist = db.coffee_history(uid)
146     else:
147         hist = db.coffee_history()
148     if hist == []:
149         unix_days = tuple()
150         counts = tuple()
151         flavors = tuple()
152     else:
153         unix_days, counts, flavors = zip(*hist)
154     fig = plt.figure(figsize=(4, 3))
155     ax = fig.add_subplot(111)
156
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))]
159     for ll in l:
160         for d in unix_days:
161             ll[d] = 0
162
163     for(d, c, f) in zip(unix_days, counts, flavors):
164         if f is None:
165             continue
166         what_f = 0
167         for i in range(len(list_flavor)):
168             if f == list_flavor[i]:
169                 what_f = i
170                 break
171         l[what_f][d] += c
172
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)]
180
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)]]
185     xdays[-1] = "TDY"
186     xdays[-2] = "YDA"
187     ax.set_xticks(range(len(unix_days)))
188     ax.set_xticklabels(xdays)
189
190     if "uid" in session:
191         ax.set_title("Your week")
192     else:
193         ax.set_title("This week total")
194
195     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
196     fig.savefig(b, format="svg", bbox_inches="tight")
197     b.seek(0)
198     plt.close(fig)
199     return send_file(b, mimetype="image/svg+xml")
200
201
202 @app.route("/coffee/add", methods=["POST"])
203 def coffee_add():
204     if request.method == "POST":
205         json = request.json
206
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'))
211
212
213 # TODO: Remove me - unused
214 @app.route("/coffee/count")
215 def 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))
219
220
221 @app.route('/js')
222 def js():
223     response = make_response(render_template('main.js'))
224     response.headers['Content-Type'] = "text/javascript"
225     return response
226
227
228 @app.route("/log", methods=["POST"])
229 def log():
230     if request.method == "POST":
231         data = request.data.decode("utf-8")
232         print("Log:", data)
233         return data
234     return "nope"
235
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"))
241     try:
242         with open(".coffee.conf", "r") as f:
243             conf = loads(f.read())
244     except:
245         return "Config read error: '%s'! Please find in git history how the .coffee.conf file should look." \
246             % sys.exc_info()[1]
247     try:
248         res = post(conf["coffeebot"]["url"], json=what)
249         print("res is {}".format(res))
250     except:
251         err = "No connection! No covfefe! We all die here!"
252     if not res.ok:
253         err = "Slack doesn't like our request! It's discrimination!"
254     return err