-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
445 lines (355 loc) · 12.4 KB
/
Copy pathapp.py
File metadata and controls
445 lines (355 loc) · 12.4 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from flask import Flask, render_template, request, redirect, url_for, flash, g
from form import Feedback
from sendMail import send_mail
from flask_login import logout_user, login_required, current_user
import subprocess, os
from subprocess import PIPE
from config import Config
from models import db, login_manager, app, User, Contest, Practice, Profile
from oauth import blueprint
from cli import create_db
from flask_migrate import Migrate
import datetime
from time import strptime
from profile import codechef_info, leetcode_info, hackerearth_info
from threading import Thread
from sqlalchemy.exc import IntegrityError
#################################
#### SETTING UP APP ############
################################
Migrate(app, db)
app.config.from_object(Config) #app.config
app.register_blueprint(blueprint, url_prefix="/login")
app.cli.add_command(create_db) #creating database
db.init_app(app) # equivalent to "flask db init"
login_manager.init_app(app)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
##################################
########### ROUTES ###############
##################################
@app.route("/", methods = ['GET','POST'])
def home():
home = 1
form = Feedback()
profile_pic = get_profile_pic()
try:
email = get_email().split("@")[0]
profile = Profile()
current_user_profile = profile.query.filter_by(username=email).first()
except :
current_user_profile = None
try:
if request.method == "POST":
name = request.form['user_name']
current_user_profile.name = name
db.session.add(current_user_profile)
db.session.commit()
except:
pass
if form.validate_on_submit():
email = form.email.data
message = form.message.data
send_mail(email, message)
flash('Thanks for the feedback!')
return redirect(url_for('home'))
return render_template("home.html", form = form, home = home, profile_pic = profile_pic, current_user_profile=current_user_profile)
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("You have logged out")
return redirect(url_for("home"))
@app.route('/compile')
def compiler():
profile_pic = get_profile_pic()
check = ''
flag = 1 # none of the language is selected before-hand
return render_template('compiler.html',
flag = flag,
check = check,
profile_pic = profile_pic,
data = [{'language':'C'}, {'language':'Python'}, {'language':'Java'}]
)
@app.route('/submit', methods = ['GET','POST'])
def submit():
flag = 0
profile_pic = get_profile_pic()
if request.method == "POST":
code = request.form['code']
inp = request.form['input']
chk = request.form.get('check')
lan = request.form.get('comp_select')
if not chk == '1':
inp = ""
check = ''
else:
check = 'checked'
output = complier_output(code, inp, chk, lan)
return render_template('compiler.html',
lang = lan,
flag = flag,
code = code,
input = inp,
output = output,
check = check,
profile_pic = profile_pic,
data = [{'language':'C'}, {'language':'Python'}, {'language':'Java'}]
)
@app.route('/contest')
@login_required
def contest():
profile_pic = get_profile_pic()
contest = Contest()
allContests = contest.query.all()
for time in allContests:
if time.start is not None:
start = time.start.split(' - ')
startDateTime = get_dateTime(start)
started = startDateTime < datetime.datetime.now()
if started == True and time.contest_status == "future":
time.contest_status = "active"
db.session.add(time)
db.session.commit()
if time.end is not None:
end = time.end.split(' - ')
endDateTime = get_dateTime(end)
ended = endDateTime < datetime.datetime.now()
if ended == True and time.contest_status != "in-active":
time.contest_status = "in-active"
db.session.add(time)
db.session.commit()
active_contests = contest.query.filter_by(contest_status = "active").all()
in_active_contests = contest.query.filter_by(contest_status = "in-active").all()
in_active_contests.reverse() # for arraging the ended conests in reverse order
future_contests = contest.query.filter_by(contest_status = "future").all()
return render_template('contest.html',
profile_pic = profile_pic,
active_contests = active_contests,
future_contests = future_contests,
in_active_contests = in_active_contests
)
@app.route('/practice')
@login_required
def practice():
profile_pic = get_profile_pic()
practice = Practice()
page = request.args.get("page", 1, type=int)
practice_info = practice.query.paginate(page=page, per_page=5)
return render_template('practice.html',
profile_pic = profile_pic,
practice_info = practice_info
)
@app.route('/refresh_codechef')
def refreshCodechef():
print("Started refresh codechef")
email = get_email().split("@")[0]
profile = Profile()
current_user_profile = profile.query.filter_by(username=email).first()
user_name = current_user_profile.codechef_username
background_thread = Thread(target=codechef_info, args=(user_name, email,))
background_thread.start()
print("Doing Refresh codechef")
return ("nothing")
@app.route('/refresh_leetcode')
def refreshLeetcode():
print("Started refresh leetcode")
email = get_email().split("@")[0]
profile = Profile()
current_user_profile = profile.query.filter_by(username=email).first()
user_name = current_user_profile.leetcode_username
background_thread = Thread(target=leetcode_info, args=(user_name, email,))
background_thread.start()
print("Doing Refresh leetcode")
return ("nothing")
@app.route('/refresh_hackerearth')
def refreshHackerearth():
print("Started refresh hackerearth")
email = get_email().split("@")[0]
profile = Profile()
current_user_profile = profile.query.filter_by(username=email).first()
user_name = current_user_profile.hackerearth_username
background_thread = Thread(target=hackerearth_info, args=(user_name, email,))
background_thread.start()
print("Doing Refresh hackerearth")
return ("nothing")
@app.route('/profile', methods = ['GET','POST'])
@login_required
def profile_page():
profile_pic = get_profile_pic()
email = get_email().split("@")[0]
profile = Profile()
current_user_profile = profile.query.filter_by(username=email).first()
if current_user_profile.codechef_username_correct == "0":
codechef_is_connected = 0
current_user_profile.codechef_username = None
db.session.add(current_user_profile)
db.session.commit()
if current_user_profile.leetcode_username_correct == "0":
leetcode_is_connected = 0
current_user_profile.leetcode_username = None
db.session.add(current_user_profile)
db.session.commit()
if current_user_profile.hackerearth_username_correct == "0":
hackerearth_is_connected = 0
current_user_profile.hackerearth_username = None
db.session.add(current_user_profile)
db.session.commit()
if current_user_profile is not None:
if current_user_profile.codechef_username is not None:
codechef_is_connected = 1
else:
codechef_is_connected = 0
if request.method == "POST":
try:
current_user_profile.codechef_username = request.form['codechef_username']
try:
db.session.add(current_user_profile)
db.session.commit()
flash("Your codechef profile will be linked within 9-10 minutes.")
codechef_is_connected = 1
user_name = current_user_profile.codechef_username
background_thread = Thread(target=codechef_info, args=(user_name, email,))
background_thread.start()
except IntegrityError:
print("FAILED")
db.session.rollback()
flash("This codechef profile is already linked to some other account.")
except :
pass
if current_user_profile.leetcode_username is not None:
leetcode_is_connected = 1
else:
leetcode_is_connected = 0
if request.method == "POST":
try:
current_user_profile.leetcode_username = request.form['leetcode_username']
try:
db.session.add(current_user_profile)
db.session.commit()
flash("Your leetcode profile will be linked within 4-5 minutes.")
leetcode_is_connected = 1
user_name = current_user_profile.leetcode_username
background_thread = Thread(target=leetcode_info, args=(user_name, email,))
background_thread.start()
except IntegrityError:
print("FAILED")
db.session.rollback()
flash("This leetcode profile is already linked to some other account.")
except :
pass
if current_user_profile.hackerearth_username is not None:
hackerearth_is_connected = 1
else:
hackerearth_is_connected = 0
if request.method == "POST":
try:
current_user_profile.hackerearth_username = request.form['hackerearth_username']
try:
db.session.add(current_user_profile)
db.session.commit()
flash("Your hackerearth profile will be linked within 4-5 minutes.")
hackerearth_is_connected = 1
user_name = current_user_profile.hackerearth_username
background_thread = Thread(target=hackerearth_info, args=(user_name, email,))
background_thread.start()
except IntegrityError:
print("FAILED")
db.session.rollback()
flash("This hackerearth profile is already linked to some other account.")
except :
pass
return render_template('profile_page.html',
profile_pic=profile_pic,
codechef_is_connected=codechef_is_connected,
leetcode_is_connected=leetcode_is_connected,
hackerearth_is_connected=hackerearth_is_connected,
current_user_profile=current_user_profile
)
@app.errorhandler(404)
def page_not_found(error):
profile_pic = get_profile_pic()
return render_template('404.html', profile_pic = profile_pic), 404
##################################
######### FUNCTIONS #############
#################################
def get_dateTime(dateTime):
dateList = str(dateTime[0]).split(" ") # dateList = [date, month, year]
timeList = str(dateTime[1]).split(":") # timeList = [hour, minutes, second]
finalDateTime = datetime.datetime(int(dateList[2]), # year
strptime(dateList[1], '%b').tm_mon, # month
int(dateList[0]), # date
int(timeList[0]), # hour
int(timeList[1]), # minutes
int(timeList[2]), # seconds
0 # microseconds
)
return finalDateTime
def get_email():
email = ""
g.user = current_user.get_id()
if g.user:
id = int(g.user)
email = User.query.get(id).email
return email
def get_profile_pic():
profile_pic = ""
g.user = current_user.get_id()
if g.user:
id = int(g.user)
profile_pic = User.query.get(id).profile_pic
return profile_pic
def createFile(code, lan):
if lan == "Python":
extension = ".py"
elif lan == 'C':
extension = ".c"
else:
extension = ".java"
fileName = "try" + extension
if not os.path.exists(fileName):
os.open(fileName, os.O_CREAT)
fd = os.open(fileName, os.O_WRONLY)
os.truncate(fd, 0)
fileadd = str.encode(code)
os.write(fd, fileadd)
os.close(fd)
def complier_output(code, inp, chk, lan):
if lan == 'Python':
createFile(code, lan)
process = subprocess.Popen(['python3', 'try.py'], stdout = PIPE, stdin = PIPE, stderr = PIPE)
#communicate() returns a tuple (stdoutdata, stderrdata)
process_out = process.communicate(input = inp.encode())
output = process_out[0]
error = process_out[1]
check = process.returncode
if check == 0:
return output.decode("utf-8")
else:
return error.decode("utf-8")
elif lan == "C":
createFile(code, lan)
process = subprocess.run(['gcc','-o','out','try.c'], stderr = PIPE,)
check = process.returncode
if check == 0:
if chk == '1':
run = subprocess.run(["./out"], input = inp.encode(), stdout = PIPE)
else:
run = subprocess.run(["./out"], stdout = PIPE)
return run.stdout.decode("utf-8")
else:
return process.stderr.decode("utf-8")
else:
createFile(code, lan)
process = subprocess.run(['javac','try.java'], stderr = PIPE,)
check = process.returncode
if check == 0:
if chk == '1':
run = subprocess.run(["java", "main"], input = inp.encode(), stdout = PIPE)
else:
run = subprocess.run(["java", "main"], stdout = PIPE)
return run.stdout.decode("utf-8")
else:
return process.stderr.decode("utf-8")
if __name__ == '__main__':
app.run(debug = True)