forked from Team-PoorVillagers/CodeHike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·359 lines (306 loc) · 13.9 KB
/
app.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import datetime
from flask import Flask, render_template, jsonify, request, redirect, url_for, flash, session, abort
from ranklist_extraction import ranking,dashboard
from api_return_scripts import *
import json
import os
from db_conn import production
app = Flask(__name__)
@app.route("/")
def main_page():
try:
if not session.get('logged_in'):
app_data = db['app_data'].find_one({"production": production})
return render_template("home-no-login.html", client_id = app_data["client_id"], redirect_uri = app_data["redirect_uri"])
else :
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
v_contest_start_time = user_data['v_contest_start_time']
contest_start_time = user_data['contest_start_time']
duration = user_data['duration']
is_running = user_data['is_running']
contest_code = user_data['contest_code']
contest_name = user_data['contest_name']
if(is_running == False):
return render_template("home.html", username = username, contest_code_display = False)
else:
return redirect(url_for('contest_page',contest_code = contest_code))
except:
return render_template('error.html')
@app.route('/auth', methods=['GET'])
def do_login():
try:
auth_token = request.args.get("code")
x = verify_login(auth_token)
if (x == True):
session.permanent = True
session['logged_in'] = True
# get_my_details()
else:
pass
# flash('wrong password!')
return main_page()
except:
return render_template('error.html')
@app.route("/logout")
def logout():
try:
session['logged_in'] = False
session.clear()
return main_page()
except:
return render_template('error.html')
@app.route("/aboutus")
def aboutus():
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
is_running = user_data['is_running']
return render_template("aboutus.html", is_running = is_running)
except:
return render_template("aboutus.html", is_running = False)
@app.route("/friends")
def friends():
try:
activate_access_token()
display_contest_code = True
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
contest_code = user_data['contest_code']
if contest_code:
#DO nothing
display_contest_code = True
else:
display_contest_code = False
contest_code = False
username = session['username']
friends_data = db['user_data'].find_one({'_id':username})
friends = friends_data['friends']
for i in range(0 ,len(friends)):
friends[i].insert(0 , i+1)
return render_template("friends.html", friends = friends , username = username , contest_code_display = display_contest_code , contest_code = contest_code)
except:
return render_template('error.html')
@app.route("/contestpage/<contest_code>")
def contest_page(contest_code):
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
v_contest_start_time = user_data['v_contest_start_time']
contest_start_time = user_data['contest_start_time']
duration = user_data['duration']
is_running = user_data['is_running']
contest_code = user_data['contest_code']
contest_name = user_data['contest_name']
problems = user_data['problems']
if(is_running == False):
username = session['username']
return render_template("home.html", username = username, contest_code_display = False)
fmt = '%Y-%m-%d %H:%M:%S'
time_now = time_slice(datetime.datetime.now())
v_contest_start_time = datetime.datetime.strptime(str(v_contest_start_time), fmt + ".%f")
end_time = v_contest_start_time + datetime.timedelta(minutes = int(float(duration)))
v_contest_start_time = time_slice(v_contest_start_time)
end_time = time_slice(end_time)
tstamp1 = datetime.datetime.strptime(str(time_now) , fmt)
tstamp2 = datetime.datetime.strptime(str(end_time) , fmt)
p = tstamp2 - tstamp1
p = p.total_seconds()
fetch_submission()
obj = ranking(contest_code , problems , contest_start_time , v_contest_start_time , time_now , False)
#Fetches the user details to display the status of the problems
#in the dashboard . Green tick for correct answer , red cross for wrong answer
username = session['username']
user = {}
user['rank'] = 0
user['name'] = '*' + username
user['entry'] = False
user['Total Score'] = 0
user['Penalty'] = 0
user['Total'] = 0
for problem in problems:
user[problem] = 0
user[problem+"Time"] = 0
friend = user
for val in obj:
if val['name'] == '*' + username:
friend = val
break
obj = dashboard(contest_code , problems , contest_start_time , v_contest_start_time , time_now)
return render_template("contestpage.html",contest_code = contest_code, name = contest_name, mins = duration, problems = problems , obj = obj , time = p, username = username, contest_code_display = True , friend = friend)
except:
return render_template('error.html')
@app.route("/standings", methods=['GET'])
def current_standing():
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
v_contest_start_time = user_data['v_contest_start_time']
contest_start_time = user_data['contest_start_time']
duration = user_data['duration']
is_running = user_data['is_running']
contest_code = user_data['contest_code']
contest_name = user_data['contest_name']
problems = user_data['problems']
if(is_running == False):
username = session['username']
return render_template("home.html", username = username, contest_code_display = False)
friends = request.args.get("friends")
# if friends == True, then user wants friends ranklist, each case you need
# send a variable named friends, whose value can be True and False
# if friends variable is True, that means data you sent is of friends.
# there might be confusion as both GET variable and varible which we are sending
# both are named as friends.
fmt = '%Y-%m-%d %H:%M:%S'
v_contest_start_time = datetime.datetime.strptime(v_contest_start_time, fmt + ".%f")
v_contest_start_time = time_slice(v_contest_start_time)
now = time_slice(datetime.datetime.now())
# print(v_contest_start_time , now)
fetch_submission()
obj = ranking(contest_code , problems , contest_start_time , v_contest_start_time , now , friends)
username = session['username']
user = {}
user['rank'] = 0
user['name'] = '*' + username
user['entry'] = False
user['Total Score'] = 0
user['Penalty'] = 0
user['Total'] = 0
for problem in problems:
user[problem] = 0
user[problem+"Time"] = 0
#Fetching the user details to show seperately in the ranklist on the top
friend = user
for val in obj:
if val['name'] == '*' + username:
friend = val
break
return render_template("rankings.html", obj=obj , problems = problems, username = username, contest_code = contest_code, contest_code_display = True , friends = friends , friend = friend)
except:
return render_template('error.html')
@app.route("/problem/<contest_code>/<problem_code>")
def problem_details(contest_code, problem_code):
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
v_contest_start_time = user_data['v_contest_start_time']
contest_start_time = user_data['contest_start_time']
duration = user_data['duration']
is_running = user_data['is_running']
contest_code = user_data['contest_code']
contest_name = user_data['contest_name']
problems = user_data['problems']
if(is_running == False):
username = session['username']
return render_template("home.html", username = username, contest_code_display = False)
x = return_problem_details(contest_code, problem_code)
return render_template('problem.html', name = x['name'], timelimit = x['timelimit'], \
sizelimit = x['sizelimit'], statement = x['body'], username = username, problem_code = problem_code ,contest_code = contest_code, contest_code_display = True)
except:
return render_template('error.html')
@app.route("/contest_welcome", methods=['GET'])
def welcome_page():
try:
activate_access_token()
username = session['username']
contest_code = request.args.get("contestcode")
contest_code = contest_code.upper()
obj = return_contest_details(contest_code)
fmt = '%Y-%m-%d %H:%M:%S'
s_d = datetime.datetime.strptime(obj['start_date'], fmt)
e_d = datetime.datetime.strptime(obj['end_date'], fmt)
duration = e_d - s_d
contest_name = obj['name']
duration = duration.total_seconds()/60
submissions = {}
for i in obj['problems']:
submissions[i] = []
db['user_data'].update_one({'_id': username}, {'$set': {'problems' : obj['problems'] , 'contest_start_time' : s_d , 'contest_end_time' : e_d , 'duration' : duration , 'contest_code' : contest_code , 'contest_name' : contest_name , 'submissions' : submissions}})
return render_template("contest_welcome.html" ,contest_code = contest_code,\
name = obj['name'], mins = duration, s_d = s_d, e_d = e_d , username = username ,contest_code_display = False )
except:
return render_template('error.html')
@app.route("/begin_contest", methods=['POST'])
def begin_contest():
try:
activate_access_token()
contest_code = request.form['contestcode']
v_contest_start_time = str(datetime.datetime.now())
username = session['username']
db['user_data'].update_one({'_id': username}, {'$set': {'v_contest_start_time': v_contest_start_time , 'is_running' : True}})
return redirect(url_for('contest_page',contest_code = contest_code, username = username, contest_code_display = True))
except:
return render_template('error.html')
@app.route("/add_friend" , methods = ['GET'])
def add_friend():
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
headers = {
'content-type': 'application/json',
'Authorization': 'Bearer {}'.format(user_data["access_token"])
}
name = request.args.get('username1')
url = "https://api.codechef.com/users/" + name
data = requests.get(url=url,headers=headers)
data = data.json()
isvalid = False
if(data['result']['data']['code'] == 9001):
fullname = data['result']['data']['content']['fullname']
friends = user_data['friends']
if [name,fullname] not in friends and name not in [username]:
# print(name)
friends.append([name , fullname])
db['user_data'].update_one({'_id': username}, {'$set': {'friends': friends}})
if(isvalid == False):
flash('Oh snap! No such user exists.')
return redirect(url_for('friends'))
except:
return render_template('error.html')
@app.route("/delete_friend" , methods = ['GET'])
def delete_friend():
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find_one({'_id':username})
name = request.args.get('username')
friends = user_data['friends']
for i in range(0 , len(friends)):
if friends[i][0] == name:
del friends[i:i+1]
break
db['user_data'].update_one({'_id': username}, {'$set': {'friends': friends}})
return redirect(url_for('friends'))
except:
return render_template('error.html')
@app.route("/compare", methods=['GET'])
def compare():
try:
activate_access_token()
compare_with = request.args.get("compare_with")
contestcode = request.args.get("contestcode")
u1data= compare_results(compare_with, contestcode , datetime.datetime.now())
username = session['username']
return render_template("compare.html", username = username, u1data = u1data, compare_with = compare_with, contestcode = contestcode)
except:
return render_template('error.html')
@app.route("/end_contest")
def end_contest():
try:
activate_access_token()
username = session['username']
user_data = db['user_data'].find({'_id':username})
db['user_data'].update_one({'_id': username}, {'$set': {'problems' : list() , 'contest_start_time' : None , 'contest_end_time' : None , 'duration' : None , 'contest_code' : None , 'contest_name' : None , 'v_contest_start_time' : None , 'is_running' : False ,'submissions' : dict()}})
return redirect(url_for('main_page'))
except:
return render_template('error.html')
app.secret_key = "this is super secret wanna lubba dub dub"
if __name__ == "__main__":
app.secret_key = "this is super secret wanna lubba dub dub"
app.run(debug=True)