forked from SE-19/autograding-of-cs-courses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
481 lines (435 loc) · 20.8 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from flask import Flask, render_template, request, redirect, session, url_for, flash, send_file, jsonify
from flask.helpers import url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc, desc
import math
import random
import os
from werkzeug.security import check_password_hash, generate_password_hash
import secrets
import smtplib
from random import randint
from mark_assignment import extract_assignments, generate_plag_report, \
clean_assignment_dir, get_directories, test_function, archive, clean_reports_dir
from create_template import create_template
SECRET_KEY = secrets.token_urlsafe(16)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://lplssjusbwlymo:31bd54bec9a37daf6cc9f0e03c30bb2cffb45fa3645565a0efab3ffbd4586615@ec2-52-7-30-112.compute-1.amazonaws.com:5432/d6nvpv17et6qni"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = SECRET_KEY
db = SQLAlchemy(app)
class Teacher(db.Model):
teacher_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
cnic = db.Column(db.String(13), unique=True, nullable=False)
phone_no = db.Column(db.String(11), unique=True, nullable=False)
department = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(500), nullable=False)
otp = db.Column(db.String(25), nullable=False)
def __repr__(self) -> str:
return f"{self.teacher_id}, {self.name}"
class TA(db.Model):
ta_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
head_id = db.Column(db.Integer,db.ForeignKey('teacher.teacher_id'))
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
cnic = db.Column(db.String(13), unique=True, nullable=False)
phone_no = db.Column(db.String(11), unique=True, nullable=False)
department = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(500), nullable=False)
def __repr__(self) -> str:
return f"{self.ta_id}, {self.name}"
class Assignment(db.Model):
assignment_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(1000), nullable=False)
graded=db.Column(db.Boolean,default=False,nullable=False)
teacher_id = db.Column(db.Integer,db.ForeignKey('teacher.teacher_id'))
# functions=db.relationship('Function',backref="assignment")
def __repr__(self) -> str:
return f"<Assignment {self.title}, {self.assignment_id}>"
class Function(db.Model):
function_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
assignment_id = db.Column(db.Integer,db.ForeignKey('assignment.assignment_id'))
docstring = db.Column(db.String(2000))
parameters = db.Column(db.String(2000))
func_name = db.Column(db.String(200),nullable=False)
marks = db.Column(db.Integer,nullable=False)
def __repr__(self) -> str:
return f"<Function {self.func_name}, {self.function_id}>"
class TestFunctions(db.Model):
func_param_rel_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
function_id = db.Column(db.Integer,db.ForeignKey('function.function_id'))
def __repr__(self) -> str:
return f"<FunctionParameterRelation {self.func_param_rel_id}>"
class Parameter(db.Model):
param_id=db.Column(db.Integer, autoincrement=True, primary_key=True)
func_param_rel_id= db.Column(db.Integer,db.ForeignKey('test_functions.func_param_rel_id'))
parameter = db.Column(db.String(1000))
def __repr__(self) -> str:
return f"<Parameter {self.parameter}, {self.datatype}, {self.param_id}>"
class ExpectedValue(db.Model):
expected_value_id=db.Column(db.Integer, autoincrement=True, primary_key=True)
func_param_rel_id= db.Column(db.Integer,db.ForeignKey('test_functions.func_param_rel_id'))
expected_value= db.Column(db.String(1000))
def __repr__(self) -> str:
return f"<ExpectedValue {self.expected_value}, {self.datatype}, {self.expected_value_id}>"
@app.route("/")
def homepage():
return render_template("index.html")
@app.route("/auth", methods=["GET", "POST"])
def login_register():
if 'logged_in_teacher_id' in session:
return redirect(url_for("homepage"))
if "logged_in_ta_id" in session:
return redirect(url_for("homepage"))
if request.method == "POST":
error = None
if 'email' in request.form:
# meaning first form fill kia hai
email = request.form['email']
password = request.form['password']
# print(email, password)
teacher = Teacher.query.filter_by(email=email).first()# get record from database.
if teacher == None:
ta = TA.query.filter_by(email=email).first()
if ta == None:
error = "Email not found!"
flash(error)
else:
if check_password_hash(ta.password, password):
# print("logged in successfully")
session['logged_in_ta_id'] = ta.ta_id
flash("Log-in Successful!")
return redirect(url_for("homepage"))
else:
error = "The Password did not match! Please try again!"
flash(error)
else:
if check_password_hash(teacher.password, password):
# print("logged in successfully")
session['logged_in_teacher_id'] = teacher.teacher_id
flash("Log-in Successful!")
return redirect(url_for("homepage"))
else:
error = "The Password did not match! Please try again!"
flash(error)
elif request.form['name'] != "":
if request.form['role'] == '1':
name = request.form['name']
cnic = request.form['cnic']
department = request.form['department']
phone_no = request.form['phone_no']
email_add = request.form['email_add']
password = request.form['pass']
otp = OTPgenerator()
new_teacher = Teacher(name=name, cnic=cnic,department=department, phone_no=phone_no, email=email_add, password=generate_password_hash(password), otp=otp)
try:
db.session.add(new_teacher)
db.session.commit()
except exc.IntegrityError:
error = "CNIC, Phone No or Email address already exist!"
flash(error)
elif request.form['role'] == '2':
name = request.form['name']
cnic = request.form['cnic']
department = request.form['department']
phone_no = request.form['phone_no']
email_add = request.form['email_add']
password = request.form['pass']
otp = request.form['otp']
teacher = Teacher.query.filter_by(otp=otp).first()
if teacher == None:
error = "OTP is incorrect"
flash("OTP is incorrect")
else:
teacher.otp = OTPgenerator()
new_TA = TA(name=name,head_id=teacher.teacher_id, cnic=cnic,department=department, phone_no=phone_no, email=email_add, password=generate_password_hash(password))
try:
db.session.add(new_TA)
db.session.commit()
except exc.IntegrityError:
error = "CNIC, Phone No or Email address already exist!"
flash(error)
return render_template("logreg.html")
@app.route("/create_assignment", methods=["GET", "POST"])
def create_assignment():
# print(session)
session['assignment'] = 'an assignment'
if 'logged_in_teacher_id' not in session and "logged_in_ta_id" not in session:
return redirect(url_for("login_register"))
if "logged_in_ta_id" in session:
error = "TA does not have access to create Assignments!"
flash(error)
return redirect(url_for('homepage'))
if request.method == 'POST':
if not os.path.isdir("./template"):
os.mkdir("./template")
if os.path.isfile("./template/assignment.py"):
os.remove("./template/assignment.py")
assignment_var = request.get_json()
assignment = Assignment(title=assignment_var['assignment_title'], teacher_id=session['logged_in_teacher_id'])
db.session.add(assignment)
db.session.commit()
functions = assignment_var['functions_testcases']
#'functions_testcases': [{'func_name': '', 'params': [], 'marks': 0, 'docstring': '', 'test_cases': []}]
for func in range(len(functions)-1):# skip the last one..
f = functions[func]
# print(f)
function = Function(assignment_id=assignment.assignment_id, parameters=f['params'], docstring=f['docstring'], marks=f['marks'], func_name=f['func_name'])
db.session.add(function)
db.session.commit()
# print(function)
create_template(f['func_name'], f['docstring'], f['params'])
for t in f['test_cases']:
test_case = TestFunctions(function_id=function.function_id)
db.session.add(test_case)
db.session.commit()
for i in t['test_params']:
test_param = Parameter(func_param_rel_id=test_case.func_param_rel_id, parameter=i)
db.session.add(test_param)
db.session.commit()
ex_val = ExpectedValue(func_param_rel_id=test_case.func_param_rel_id, expected_value=t['ex_value'])
db.session.add(ex_val)
db.session.commit()
return jsonify({"success":"./template/assignment.py"})
# return send_file("./template/assignment.py", as_attachment=True)
return render_template("create_assignment.html")
@app.route("/download_template")
def download_template():
return send_file("./template/assignment.py", as_attachment=True)
@app.route("/mark_assignment", methods=["GET", "POST"])
def mark_assignment():
if 'logged_in_teacher_id' not in session and "logged_in_ta_id" not in session:
return redirect(url_for("login_register"))
if request.method == "POST":
if not os.path.isdir("./assignment"):
os.mkdir("./assignment")
if not os.path.isdir("./reports"):
os.mkdir("./reports")
if not os.path.isdir("./report"):
os.mkdir("./report")
if not os.path.isdir("./log"):
os.mkdir("./log")
assignment_id = request.form['select_assignment']
# print(assignment_id)
plagiarism_check = 'off'
if 'plagiarism' in request.form:
plagiarism_check = request.form['plagiarism']
# print(plagiarism_check)
logfile = 'off'
if 'logfile' in request.form:
logfile = request.form['logfile']
# print(plagiarism_check)
file = request.files["filename"]
if not check_extension(file.filename):
flash("The extension must be a .zip or .rar file!")
else:
file_name = ""
if file.filename.endswith(".zip"):
file_name = "assignments.zip"
if file.filename.endswith(".rar"):
file_name = "assignments.rar"
file.save("./assignment/" + file_name)
# print(file_name)
extract_assignments(file_name)
if plagiarism_check == "on":
generate_plag_report()
if logfile == "on" and plagiarism_check == "on":
test_function(get_test_cases(assignment_id), get_directories(), True, True)
archive("log")
elif logfile == "on" and not (plagiarism_check == "on"):
test_function(get_test_cases(assignment_id), get_directories(), True)
archive("log")
elif not (logfile == "on") and plagiarism_check == "on":
test_function(get_test_cases(assignment_id), get_directories(), False, True)
# archive("log")
else:
test_function(get_test_cases(assignment_id), get_directories())
# clean_assignment_dir()
archive("reports")
# clean_reports_dir()
return send_file("./reports/reports.zip", as_attachment=True)
if 'logged_in_teacher_id' in session:
all_assignments = reversed(Assignment.query.filter_by(teacher_id=session['logged_in_teacher_id']).all())
if 'logged_in_ta_id' in session:
ta = TA.query.filter_by(ta_id=session['logged_in_ta_id']).first()
all_assignments = reversed(Assignment.query.filter_by(teacher_id=ta.head_id).all())
# print(all_assignments)
return render_template("mark_assignment.html", assignments=all_assignments)
def check_extension(filename):
return filename.endswith(".zip") or filename.endswith(".rar")
@app.route("/assignments", methods=["GET", "POST"])
def assignments():
if 'logged_in_teacher_id' not in session and "logged_in_ta_id" not in session:
return redirect(url_for("login_register"))
if 'logged_in_teacher_id' in session:
all_assignments = reversed(Assignment.query.filter_by(teacher_id=session['logged_in_teacher_id']).all())
# print(all_assignments)
if 'logged_in_ta_id' in session:
ta = TA.query.filter_by(ta_id=session['logged_in_ta_id']).first()
all_assignments = reversed(Assignment.query.filter_by(teacher_id=ta.head_id).all())
# print(all_assignments)
return render_template("assignments.html", assignments=all_assignments)
@app.route("/delete_assignment/<id>", methods=["POST"])
def delete_assignment(id):
Assignment.query.filter_by(assignment_id=int(id)).delete()
functions = Function.query.filter_by(assignment_id=int(id)).all()
for f in functions:
temp_id = f.function_id
Function.query.filter_by(function_id=int(temp_id)).delete()
testcases = TestFunctions.query.filter_by(function_id=int(temp_id)).all()
for t in testcases:
x_id = t.func_param_rel_id
TestFunctions.query.filter_by(function_id=int(x_id)).delete()
Parameter.query.filter_by(func_param_rel_id=x_id).delete()
ExpectedValue.query.filter_by(func_param_rel_id=x_id).delete()
# assignment.delete()
db.session.commit()
return redirect(url_for("assignments"))
@app.route("/profile")
def profile():
if 'logged_in_teacher_id' in session:
user = Teacher.query.filter_by(teacher_id = session['logged_in_teacher_id']).first()
return render_template("profile.html",c_user=user)
elif 'logged_in_ta_id' in session:
user = TA.query.filter_by(ta_id = session['logged_in_ta_id']).first()
return render_template("profile.html",c_user=user)
else:
redirect(url_for('login_register'))
@app.route("/change_password" ,methods=["GET", "POST"])
def change_password():
if request.method == "POST":
cur_pass = request.form['cur_pass']
new_pass = request.form['new_pass']
conf_pass = request.form['conf_pass']
# print(cur_pass,new_pass,conf_pass)
if 'logged_in_teacher_id' in session:
user = Teacher.query.filter_by(teacher_id = session['logged_in_teacher_id']).first()
elif 'logged_in_ta_id' in session:
user = TA.query.filter_by(ta_id = session['logged_in_ta_id']).first()
if check_password_hash(user.password,cur_pass):
if new_pass == conf_pass:
user.password = generate_password_hash(new_pass)
db.session.commit()
return redirect('logout')
else:
flash("Password is not confirmed")
return render_template("change_password.html")
else:
flash("Wrong current password")
return render_template("change_password.html")
return render_template("change_password.html")
# Here started the forgot password scene
@app.route('/forgotpass')
def forgotpass():
return render_template("forgotpass.html")
def send_email(from_addr, to_addr_list,message,login, password, smtpserver='smtp.gmail.com:587'):
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login,password)
server.sendmail(from_addr, to_addr_list,message)
server.quit()
@app.route("/accountfound",methods=['GET','POST'])
def accountfound():
errornf=False
accf=False
if request.method=="POST":
email=request.form["email"]
query= Teacher.query.filter_by(email=email).first()
if(query is None):
errornf=True
return render_template("forgotpass.html",errornf=errornf)
else:
accf=True
otp=f"{randint(100,999)}-{randint(100,999)}"
session["otp"]=otp
session["emailtochange"]=email
msg=f"Dear Sir/Ma'am,\n\t\tWe are glad that your account can be recovered. Your verification code is: {otp}"
send_email("Auto Grade",f"{email}",msg,"[email protected]","123testing!")
return render_template("forgotpass.html",accf=accf)
return redirect(url_for("forgot_password"))
@app.route("/otpver",methods=['GET','POST'])
def otp_verification():
errorincotp=False
if request.method=="POST":
otp=request.form["otp"]
if(session["otp"]!=otp):
errorincotp=True
return render_template("forgotpass.html",errorincotp=errorincotp)
else:
otpiscorrect=True
session.pop("otp",None)
return render_template("forgotpass.html",otpiscorrect=otpiscorrect)
if "otp" in session and "emailtochange" in session:
session.pop("otp",None)
session.pop("emailtochange",None)
return redirect(url_for("forgot_password"))
@app.route("/updatepassword",methods=['GET','POST'])
def update_pass():
if request.method=="POST":
newpass=request.form["newpass"]
query=Teacher.query.filter_by(email=session["emailtochange"]).first()
query.password = generate_password_hash(newpass)
db.session.commit()
session.pop("emailtochange",None)
# print("Password changed successfully.", newpass)
return redirect(url_for("login_register"))
if "emailtochange" in session:
session.pop("emailtochange",None)
return redirect(url_for("forgot_password"))
@app.route("/TA_manage", methods=['GET','POST'])
def TA_manage():
if request.method == 'POST':
ta_id = request.form['ta_id']
TA.query.filter_by(ta_id = ta_id).delete()
if "logged_in_teacher_id" not in session:
return redirect(url_for("homepage"))
if "logged_in_ta_id" in session:
error = "Access Denied!"
flash(error)
return redirect(url_for(homepage))
teacher = Teacher.query.filter_by(teacher_id=session['logged_in_teacher_id']).first()
data = {}
data['Teacher_otp'] = teacher.otp
data['TAs'] = TA.query.filter_by(head_id = session['logged_in_teacher_id']).all()
return render_template("TA_manage.html",data=data)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('login_register'))
def OTPgenerator():
string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
OTP = ""
for c in range(22):
OTP += string[math.floor(random.randint(0,len(string)-1))]
return OTP
def get_test_cases(assignment_id):
# assignment = Assignment.query.filter_by(assignment_id=assignment_id).first()
# print("-"*10 + "Assignment: " + assignment.title + "-"*10)
functions = Function.query.filter_by(assignment_id=assignment_id).all()
test_cases = []
for function in functions:
# print("-"*10 + "Function: " + function.func_name + "-"*10)
relations = TestFunctions.query.filter_by(function_id=function.function_id).all()
marks = function.marks/len(relations)
for r in relations:
temp = [function.func_name]
# print("-"*10 + "Relations: " + str(r) + "-"*10)
params = Parameter.query.filter_by(func_param_rel_id=r.func_param_rel_id).all()
parameters = []
for p in params:
parameters.append(eval(p.parameter))
# print(parameters)
temp.append(parameters)
ev = ExpectedValue.query.filter_by(func_param_rel_id=r.func_param_rel_id).first()
expected_value = eval(ev.expected_value)
# print(expected_value)
temp.append(expected_value)
temp.append(marks)
test_cases.append(temp)
# print(test_cases)
return test_cases
if __name__ == "__main__":
print("God is the greatest")
app.run(debug=False)