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