-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (101 loc) · 4.73 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from functools import wraps
from flask import Flask
from flask import flash, redirect, render_template, request, session, url_for, jsonify
from google.appengine.api import users
from utils import config
from utils import models
app = Flask(__name__)
app.secret_key = config.SECRET_KEY
def force_logged_in(func):
@wraps(func)
def logged_in_wrapper(*args, **kwargs):
user = users.get_current_user()
if not user:
return redirect(users.create_login_url("/"))
else:
return func(*args, **kwargs)
return logged_in_wrapper
def render_authorized_template(user, template_name, **kwargs):
if user:
signouturl = users.create_logout_url('/')
return render_template(template_name, username=user.nickname(), signouturl=signouturl, **kwargs)
def request_wants_json():
best = request.accept_mimetypes \
.best_match(['application/json; charset=utf-8', 'text/html'])
print best
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
@app.route('/')
def index():
current_user = users.get_current_user()
if current_user:
current_user = users.get_current_user()
userId = current_user.user_id()
latest_session = models.get_latest_practice_session(userId)
if latest_session != None and not latest_session.complete():
return render_authorized_template(current_user, "index.html",
current_session=latest_session.serialize(),
scripts=["/static/js/index.js"], )
else:
return render_authorized_template(current_user, "index.html", scripts=["/static/js/index.js"], )
else:
return render_template("login.html", signinurl=users.create_login_url("/"))
@app.route("/sessions", methods=["POST", "GET"])
@force_logged_in
def practice_sessions():
user = users.get_current_user()
if request.method == "POST":
data = request.get_json()
currentSession = models.add_practice_session(user.user_id(), data)
return jsonify(currentSession.serialize())
elif request.method == "GET":
start = request.args.get("start")
stop = request.args.get("stop")
all_sessions = models.get_practice_sessions(user.user_id(), start=start, stop=stop)
all_serialized = [s.serialize() for s in all_sessions]
if "application/json" in request.accept_mimetypes.values():
return jsonify(all_serialized)
else:
return render_authorized_template(user, "all_sessions.html", scripts=["/static/js/all_sessions.js"],
practice_sessions=all_serialized)
@app.route("/sessions/<id>", methods=["PUT", "GET", "DELETE"])
@force_logged_in
def practice_session(id):
user = users.get_current_user()
if request.method == "PUT":
data = request.get_json()
updated_session = models.update_practice_existing_session(user.user_id(), id, data)
return jsonify(updated_session.serialize())
elif request.method == "GET":
if id == "add":
return render_authorized_template(user, "practice_session.html",
scripts=["/static/js/practice_session.js"],
state="create")
else:
practice_session = models.get_practice_session(user.user_id(), id)
if practice_session:
return render_authorized_template(user, "practice_session.html",
scripts=["/static/js/practice_session.js"],
state="update",
practice_session=practice_session.serialize())
else:
return redirect(url_for("index"))
elif request.method == "DELETE":
if (models.delete_practice_session(user.user_id(), id)):
return
else:
return "Could not delete sessions, not found", 404
@app.route("/charts", methods=["GET"])
@force_logged_in
def charts():
user = users.get_current_user()
return render_authorized_template(user, "charts.html",
scripts=["https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js",
"/static/js/charts.js"])
@app.errorhandler(500)
def handle_internal_server_error(error):
message = "Internal Server Error: {}".format(error.message)
return render_authorized_template(users.get_current_user(), "error.html", errorcode=500, errormessage=message)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)