-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
208 lines (161 loc) · 6.65 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import requests
import json
import csv
from flask import Flask, render_template, redirect, request, session, abort
from flask_login.utils import login_user, logout_user
from google_auth_oauthlib.flow import Flow
import google.auth.transport.requests
from google.oauth2 import id_token
from flask_login import LoginManager, current_user
from pip._vendor import cachecontrol
from flask.helpers import url_for
import pandas
from user import User
from db_handler import db
import pages.compete
import pages.leadeboard
import periodic.refresh_recom
import periodic.refresh_user
import periodic.refresh_stats
import Graph.ques_light, Graph.rank_light, Graph.xp_light
# End of imports
app = Flask(__name__)
login_manager = LoginManager()
with open("secure/app_secrets.json") as appSecrets:
data = json.loads(appSecrets.read())
app.secret_key = bytes(data["secret_key"], 'utf-8')
# Why is security such a nightmare to deal with
login_manager.init_app(app)
database = db()
#CHANGE THIS ON RELEASE
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = '1'
CLIENT_ID = None
with open("secure/client_secrets.json") as file:
data = json.loads(file.read())
CLIENT_ID = data['web']['client_id']
flow = Flow.from_client_secrets_file(
client_secrets_file='secure/client_secrets.json',
scopes=["https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email", "openid"],
redirect_uri="http://localhost:5000/signin/callback"
)
problem_set = {}
with open('./data/problem_data.csv', 'r', encoding='utf-8') as file:
rd = csv.reader(file)
for row in rd:
problem_set[int(row[0])] = [row[1], row[2], row[3]]
@login_manager.user_loader
def loadUser(userid):
return User.get(userid, database)
@app.route("/", methods=['GET'])
def index():
return render_template('index.html')
@app.route("/get_handle", methods=['GET', 'POST'])
def get_handle():
user = User.get(request.args['g_id'], database)
return render_template("get_handle.html", id=request.args['g_id'], name=user._name, email=user._email)
@app.route("/submit", methods=["GET", "POST"])
def submit():
id = request.form['id']
handle = request.form['handle']
name = request.form['name']
email = request.form['email']
user = User.update(id, name, email, handle, database)
login_user(user)
return redirect("/dashboard")
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/compete", methods=['GET'])
def compete():
if not current_user.is_authenticated:
return redirect('/signin')
level = User.get_level(current_user.id, database)
dat = pages.compete.get_recommendations(current_user.id, database)
leg = User.get_league(current_user.id, database)
_xp = User.get_xp(current_user.id, database)
next_league = (((level - 1) // 5 + 1) * 5) + 1
next_xp = (next_league ** 3) * 100
diff = next_xp - _xp
return render_template('compete.html', data=dat, lev=level, league=leg, xp=_xp, difference=diff)
@app.route("/leaderboard")
def leaderboard():
if not current_user.is_authenticated:
return redirect('/signin')
res = pages.leadeboard.create_league_leadeboard(current_user.id, database)
leg = User.get_league(current_user.id, database)
return render_template('leaderboard.html', data=res, league=leg)
@app.route("/practice")
def practice():
if not current_user.is_authenticated:
return redirect('/signin')
filename = 'data/sorted_problems.csv'
data = pandas.read_csv(filename, header=0).iloc[:,1:]
data.columns = data.columns.str.strip()
easytbl = list(data[(data['Rating']<=1200) & (data['Rating']>=800)].sample(20).values)
midtbl = list(data[(data['Rating']<=1900) & (data['Rating']>1200)].sample(20).values)
hardtbl = list(data[(data['Rating']<=2500) & (data['Rating']>1900)].sample(20).values)
insanetbl = list(data[data['Rating']>2500].sample(20).values)
return render_template('practice.html', easytbl=easytbl, midtbl=midtbl, hardtbl=hardtbl, insanetbl=insanetbl)
@app.route("/signin", methods=["GET", "POST"])
def signin():
if current_user.is_authenticated:
return redirect("/dashboard")
authorization_url, state = flow.authorization_url()
session["state"] = state
return redirect(authorization_url)
@app.route("/trending")
def trending():
if not current_user.is_authenticated:
return redirect('/signin')
return render_template('trending.html')
@app.route("/dashboard", methods=['GET'])
def dashboard():
if not current_user.is_authenticated:
return redirect("/signin")
level = User.get_level(current_user.id, database)
xp = User.get_xp(current_user.id, database)
league = User.get_league(current_user.id, database)
xp_to_next = ((level + 1) ** 3) * 100
perc = (xp / xp_to_next) * 100
questions = User.get_q_per_day(current_user.id, database)
signup = User.get_signup_date(current_user.id, database)
qGraph = Graph.ques_light.light_ques(signup, questions)
rankGraph = Graph.rank_light.light_rank(signup, User.get_rank_per_day(current_user.id, database))
xpGraph = Graph.xp_light.light_xp(signup, User.get_xp_per_dat(current_user.id, database))
with open("problem_day.txt", 'r') as file:
link = str(file.read())
return render_template("dashboard.html", name=current_user._name, lev=level,
xp=xp, leag=league, next=int(perc), questions=qGraph, xp_graph=xpGraph, ranks=rankGraph, potdlink=link)
@app.route("/signin/callback", methods=["GET"])
def callback():
flow.fetch_token(authorization_response=request.url)
if not session["state"] == request.args["state"]:
abort(500)
credentials = flow.credentials
requestSession = requests.session()
cache_session = cachecontrol.CacheControl(requestSession)
token_request = google.auth.transport.requests.Request(session=cache_session)
idToken = id_token.verify_oauth2_token(
id_token=credentials.id_token,
request=token_request,
audience=CLIENT_ID
)
if not User.get(idToken["sub"], database): # Brand new user
User.create_no_handle(idToken['sub'], idToken['name'], idToken['email'], database)
return redirect(url_for("get_handle", g_id=idToken["sub"]))
if not User.get_handle(idToken['sub'], database):
return redirect(url_for("get_handle", g_id=idToken['sub']))
user = User.get(idToken["sub"], database)
login_user(user)
return redirect("/dashboard")
@app.route("/logout")
def logout():
logout_user()
return redirect("/")
@app.errorhandler(404)
def page_not_found(e):
return render_template("error.html"), 404
if __name__ == '__main__':
app.run()