]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blob - app.py
389aa2828c1e3017529a85de1772358ce3129425
[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 from datetime import date, timedelta
14
15 from json import loads
16 from requests import post
17
18 db.init_db()
19 app = Flask(__name__)
20 CORS(app, supports_credentials=True)
21 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
22
23
24 @app.route('/')
25 def hello():
26     if "uid" in session:
27         uid = session["uid"]
28         return render_template('hello.html', name=db.get_name(uid))
29     return render_template('hello.html')
30
31
32 @app.route('/login', methods=["POST"])
33 @app.route('/login/<uid>')
34 def login(uid=None):
35     if request.method == "POST":
36         uid = request.data.decode("utf-8")
37     if uid is not None:
38         db.add_user(uid)
39         session["uid"] = uid
40     return redirect(url_for('user'))
41
42
43 @app.route('/logout')
44 def logout():
45     session.pop('uid', None)
46     return redirect(url_for('user'))
47
48
49 @app.route('/user')
50 def user():
51     if "uid" in session:
52         uid = session["uid"]
53         return render_template('user.html',
54                                name=db.get_name(uid),
55                                flavors=db.flavors(),
56                                count=db.coffee_count(uid, 0),
57                                stamp=time.time()
58                                )
59     # TODO: Replace stamp parameter with proper cache control HTTP
60     # headers in response
61     return render_template('user.html', stamp=time.time())
62
63
64 @app.route('/user/rename')
65 def user_rename():
66     name = request.args.get("name")
67     if name and "uid" in session:
68         uid = session["uid"]
69         db.name_user(uid, name)
70     return redirect(url_for('user'))
71
72
73 @app.route("/coffee/graph_flavors")
74 def coffee_graph_flavors():
75     days = request.args.get('days', default = 0, type = int)
76     start = request.args.get('start', default = 0, type = int)
77
78     b = BytesIO()
79     if "uid" in session:
80         uid = session["uid"]
81         flavors, counts = zip(*db.coffee_flavors(uid, days, start))
82     else:
83         flavors, counts = zip(*db.coffee_flavors(None, days, start))
84     fig = plt.figure(figsize=(3, 3))
85     ax = fig.add_subplot(111)
86     ax.set_aspect(1)
87     ax.pie(counts, autopct=lambda p: '{:.0f}'.format(p * sum(counts)/100) if p != 0 else '')
88     ax.legend(flavors, bbox_to_anchor=(1.0, 1.0))
89
90     if "uid" in session:
91         ax.set_title("Your taste")
92     else:
93         ax.set_title("This week taste")
94
95     fig.savefig(b, format="svg", bbox_inches="tight")
96     b.seek(0)
97     return send_file(b, mimetype="image/svg+xml")
98
99
100 @app.route("/coffee/graph_history")
101 def coffee_graph_history():
102     b = BytesIO()
103     if "uid" in session:
104         uid = session["uid"]
105         hist = db.coffee_history(uid)
106     else:
107         hist = db.coffee_history()
108     if hist == []:
109         unix_days = tuple()
110         counts = tuple()
111         flavors = tuple()
112     else:
113         unix_days, counts, flavors = zip(*hist)
114     fig = plt.figure(figsize=(4, 3))
115     ax = fig.add_subplot(111)
116
117     list_flavor = sorted(db.flavors())
118     l = [{} for i in range(len(list_flavor))]
119     for ll in l:
120         for d in unix_days:
121             ll[d] = 0
122
123     for(d, c, f) in zip(unix_days, counts, flavors):
124         if f is None:
125             continue
126         what_f = 0
127         for i in range(len(list_flavor)):
128             if f == list_flavor[i]:
129                 what_f = i
130                 break
131         l[what_f][d] += c
132
133     z = list(0 for i in range(len(l[0])))
134     for flavor in range(len(list_flavor)):
135         sortedlist = [(k, l[flavor][k]) for k in sorted(l[flavor])]
136         x = [i[0] for i in sortedlist]
137         y = [i[1] for i in sortedlist]
138         ax.bar(range(len(x)), y, bottom=z)
139         z = [sum(i) for i in zip(y, z)]
140
141     unix_days = set(unix_days)
142     xdays = [i.strftime("%a") for i in [
143         date.today() - timedelta(j - 1) for j in
144         range(len(unix_days), 0, -1)]]
145     xdays[-1] = "TDY"
146     xdays[-2] = "YDA"
147     ax.set_xticks(range(len(unix_days)))
148     ax.set_xticklabels(xdays)
149
150     if "uid" in session:
151         ax.set_title("Your week")
152     else:
153         ax.set_title("This week total")
154
155     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
156     fig.savefig(b, format="svg", bbox_inches="tight")
157     b.seek(0)
158     plt.close(fig)
159     return send_file(b, mimetype="image/svg+xml")
160
161
162 @app.route("/coffee/add", methods=["POST"])
163 def coffee_add():
164     if request.method == "POST":
165         json = request.json
166         print("User '%(uid)s' had '%(flavor)s' at %(time)s" % json)
167         db.add_coffee(json["uid"], json["flavor"], json["time"])
168     return redirect(url_for('user'))
169
170
171 @app.route("/coffee/count")
172 def coffee_count():
173     start = request.args.get("start")
174     stop = request.args.get("stop")
175     return str(db.coffee_count(session.get("uid"), start, stop))
176
177
178 @app.route('/js')
179 def js():
180     response = make_response(render_template('main.js'))
181     response.headers['Content-Type'] = "text/javascript"
182     return response
183
184
185 @app.route("/log", methods=["POST"])
186 def log():
187     if request.method == "POST":
188         data = request.data.decode("utf-8")
189         print("Log:", data)
190         return data
191     return "nope"
192
193 @app.route("/tellCoffeebot", methods=["POST"])
194 def tell_coffeebot():
195     err = "Don't worry now! There is a NEW HOPE Tonda is buying NEW PACK!"
196     if request.method == "POST":
197         what = loads(request.data.decode("utf-8"))
198     try:
199         with open(".config", "r") as f:
200             conf = loads(f.read())
201     except:
202         return "Config needed! Please find in git history how it should look."
203     try:
204         res = post(conf["coffeebot"]["url"], json=what)
205         print("res is {}".format(res))
206     except:
207         err = "No connection! No covfefe! We all die here!"
208     if not res.ok:
209         err = "Slack don't like the request! It's discrimination!"
210     return err