-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
381 lines (292 loc) · 13.8 KB
/
application.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import re
import os
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session, url_for, send_from_directory,jsonify
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from tempfile import mkdtemp
from datetime import datetime
from helpers import *
from flask_jsglue import JSGlue
import requests
import json
from operator import itemgetter
# configure application
app = Flask(__name__)
JSGlue(app)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config['src-noconflict']="ace-builds/src-noconflict"
app.config['static2']="static/register"
app.config['static3']="static/login"
app.config['static4']="static/API"
Session(app)
# configure CS50 Library to use SQLite database
db = SQL("sqlite:///judge.db")
# Custom static data
@app.route('/ace-builds/src-noconflict/<path:filename>')
def custom_static(filename):
return send_from_directory(app.config['src-noconflict'], filename)
# Custom static data
@app.route('/static/register/<path:filename>')
def custom_static2(filename):
return send_from_directory(app.config['static2'], filename,as_attachment=True)
@app.route('/static/login/<path:filename>')
def custom_static3(filename):
return send_from_directory(app.config['static3'], filename,as_attachment=True)
@app.route('/static/API/<path:filename>')
def custom_static4(filename):
return send_from_directory(app.config['static4'], filename,as_attachment=True)
#admin page
@app.route("/admin", methods=["GET"])
@login_required
def admin():
if session["user_id"]==1:
return render_template("admin.html")
return apology("You are not admin boy!")
@app.route("/change", methods=["GET", "POST"])
@login_required
def change():
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure username was submitted
if not request.form.get("oldpass"):
return apology("must provide username")
# ensure password was submitted
elif not request.form.get("newpass"):
return apology("must provide password")
elif not request.form.get("newpass2"):
return apology("must provide password")
elif request.form.get("newpass") != request.form.get("newpass2"):
return apology("Passwords don't match")
pas=db.execute("SELECT user_password FROM user Where user_id=:id",id=session["user_id"])
if not pwd_context.verify(request.form.get("oldpass"), pas[0]["user_password"]):
return apology("Wrong pass")
db.execute("UPDATE user SET user_password=:hash Where user_id=:id",id=session["user_id"],hash=pwd_context.hash(request.form.get("newpass")))
flash("password Changed Sucesfully")
return redirect(url_for("index"))
return render_template("change.html")
@app.route("/")
@login_required
def index():
return render_template("index.html")
@app.route("/compilearea",methods=["POST","GET"])
@login_required
def compilearea():
return render_template("compilearea.html")
@app.route("/contests",methods=["POST","GET"])
@login_required
def contests():
if request.method == "POST":
if request.form.get("admin") or request.form.get("select"):
if request.form.get("admin")=="Start":
db.execute("UPDATE contestStatus SET Start=1")
db.execute("UPDATE contestStatus SET Stop=0")
if request.form.get("admin")=="Stop":
db.execute("UPDATE contestStatus SET Start=0")
db.execute("UPDATE contestStatus SET Stop=1")
if request.form.get("admin")=="Reset":
db.execute("DELETE FROM solvedC")
db.execute("UPDATE contestStatus SET Start=0")
db.execute("UPDATE contestStatus SET Stop=0")
if request.form.get("select"):
data=db.execute("SELECT * FROM contestProblems WHERE header=:header",header=request.form.get("select"))
return render_template("contests.html",data=data)
else:
flash("choose something!")
return render_template("contests.html")
stat=db.execute("SELECT * FROM contestStatus")
if stat[0]["Start"]==1:
questions=db.execute("SELECT header FROM contestProblems")
return render_template("contests.html",questions=questions)
elif stat[0]["Stop"]==1:
return redirect(url_for("contestResults"))
return apology("Check CPE Facebook Group for contest Time/Date!")
@app.route("/cf",methods=["POST"])
@login_required
def cf():
if request.method=="POST":
content=request.get_json(force=True)
name=db.execute("SELECT user_name FROM user WHERE user_id=:id",id=session["user_id"])
check=db.execute("SELECT user_name FROM solvedProblems WHERE user_name=:name",name=name[0]["user_name"])
if not check:
db.execute("INSERT INTO solvedProblems(:problem,user_name) VALUES(1,:name)",problem=content["name"],name=name[0]["user_name"])
else:
check2=db.execute("SELECT * FROM solvedProblems WHERE user_name=:name",name=name[0]["user_name"])
if check2[0][content["name"]]==0:
db.execute("UPDATE solvedProblems SET :problem=1 WHERE user_name=:name",problem=content["name"],name=name[0]["user_name"])
else:
return "SOLVED"
return "SOLVED"
return "ERROR"
@app.route("/contestResults",methods=["GET"])
@login_required
def contestResults():
r=db.execute("SELECT * FROM solvedC")
data=[]
for item in r:
data.append(dict({'user_name':item["user_name"],'summation':sum(i for i in item.values() if isinstance(i, int))}))
data = sorted(data, key=itemgetter('summation'),reverse=True)
return render_template("contestResults.html",data=data)
@app.route("/ladder")
@login_required
def ladder():
rows=db.execute("SELECT * FROM solvedProblems")
data=[]
for item in rows:
data.append(dict({'user_name':item["user_name"],'summation':sum(i for i in item.values() if isinstance(i, int))}))
data = sorted(data, key=itemgetter('summation'),reverse=True)
return render_template("ladder.html",data=data)
@app.route("/cpptut",methods=["POST"])
@login_required
def cpptut():
if request.method == "POST":
if request.form.get("select"):
tut=db.execute("SELECT * FROM cpptutorials where LessonName=:number",number=request.form.get("select"))
return render_template("tutorials.html",tutor=tut)
@app.route("/tutorials",methods=["POST","GET"])
@login_required
def tutorials():
if request.method == "POST":
if request.form.get("select"):
if request.form.get("select")=="C++":
cppdata=db.execute("SELECT * FROM cpptutorials")
return render_template("tutorials.html",data=cppdata)
else:
flash("Choose PROBLEM!!")
return render_template("tutorials.html")
return render_template("tutorials.html")
@app.route("/aboutus")
@login_required
def aboutus():
return apology("Motivated Students From University Of Jordan / AMMAN")
@app.route("/practice",methods=["POST","GET"])
@login_required
def practice():
if request.method == "POST":
if not request.form.get("select"):
flash("Choose PROBLEM!!")
return render_template("practice.html")
data=db.execute("SELECT * FROM problems WHERE header=:header",header=request.form.get("select"))
return render_template("practice.html",data=data)
questions=db.execute("SELECT header FROM problems")
return render_template("practice.html",questions=questions)
#compile
@app.route("/compiler",methods=["POST"])
@login_required
def compiler():
if request.method=="POST":
rows = db.execute("SELECT * FROM user WHERE user_id = :id",id=session["user_id"])
if not rows:
raise RuntimeError("bad request")
url="http://178.62.118.95:8081/compile"
content=request.get_json(force=True)
content['client_secret']='asdj3q49fjlk345809sfslkdj54039534054#%#@!#()#%$()*^)%$*&)^(&)&()*#@$%)DFDS)*W$KL#$K@LDFGLML$'
r=requests.post(url,json=content,headers={'Content-type':'application/json'})
return jsonify(r.json())
raise "something wrong"
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in."""
# forget any user_id
session.clear()
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure username was submitted
if not request.form.get("email"):
return apology("must provide Email")
# ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password")
# query database for username
rows = db.execute("SELECT * FROM user WHERE user_email = :email", email=request.form.get("email"))
# ensure username exists and password is correct
if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["user_password"]):
return apology2("YOU SHALL NOT PASS!")
# remember which user has logged in
session["user_id"] = rows[0]["user_id"]
flash('You were successfully logged in')
# redirect user to home page
return redirect(url_for("index"))
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/Contestsolved",methods=["POST"])
@login_required
def Contestsolved():
if request.method=="POST":
content=request.get_json(force=True)
name=db.execute("SELECT user_name FROM user WHERE user_id=:id",id=session["user_id"])
check=db.execute("SELECT user_name FROM solvedC WHERE user_name=:name",name=name[0]["user_name"])
if not check:
db.execute("INSERT INTO solvedC(:problem,user_name) VALUES(1,:name)",problem=content["name"],name=name[0]["user_name"])
else:
check2=db.execute("SELECT * FROM solvedC WHERE user_name=:name",name=name[0]["user_name"])
if check2[0][content["name"]]==0:
db.execute("UPDATE solvedC SET :problem=1 WHERE user_name=:name",problem=content["name"],name=name[0]["user_name"])
else:
return "SOLVEDD"
return "SOLVEDD"
return "ERROR"
@app.route("/logout")
def logout():
"""Log user out."""
# forget any user_id
session.clear()
flash('You were successfully logged out')
# redirect user to login form
return redirect(url_for("login"))
@app.route("/register", methods=["GET", "POST"])
def register():
session.clear()
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure username was submitted
if not request.form.get("username"):
return apology("must provide username")
# ensure email was submitted
if not request.form.get("email"):
return apology("must provide Email")
# ensure univId was submitted
if not request.form.get("univid"):
return apology("must provide University ID")
# ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password")
elif not request.form.get("password2"):
return apology("must provide password")
elif request.form.get("password") != request.form.get("password2"):
return apology("Passwords don't match")
elif len(request.form.get("password")) < 8:
return apology("Password too short")
rows = db.execute("SELECT * FROM user WHERE user_name = :username OR user_email= :email OR user_univID = :univid", username=request.form.get("username"),email=request.form.get("email"),univid=request.form.get("univid"))
if rows:
if request.form.get("username") == rows[0]["user_name"]:
return apology("UserName already Exists")
elif request.form.get("email") == rows[0]["user_email"]:
return apology("Email already Exists")
elif request.form.get("univid") == rows[0]["user_univID"]:
return apology("University ID already exists")
insertion = db.execute("INSERT INTO user (user_name,user_password,user_email,user_univID) VALUES(:username,:hash,:email,:univID)",username=request.form.get("username"),hash=pwd_context.hash(request.form.get("password")),email=request.form.get("email"),univID=request.form.get("univid"))
if not insertion:
return apology("Username/Email is already Exist")
rows = db.execute("SELECT * FROM user WHERE user_name = :username", username=request.form.get("username"))
# remember which user has logged in
session["user_id"] = rows[0]["user_id"]
flash('You were successfully Registerd!')
# redirect user to home page
return redirect(url_for("index"))
else:
return render_template("register.html")