-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp-oop.py
139 lines (117 loc) · 5.3 KB
/
webapp-oop.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
from flask import *
from flask_wtf import FlaskForm
from flask_login import LoginManager, login_required, logout_user, login_user, current_user
from ProgrammingForm import ProgrammingForm
from xmlreader import Reader as rd
import saver
import os
from runner import *
import Users
class webapp2:
def __init__(self):
self.app = Flask(__name__)
self.app.config['SECRET_KEY'] = 'hard to guess string'
self.path = ('/Users/apple/Desktop/questions')
self.login_manager = LoginManager()
self.login_manager.login_view = 'login'
self.login_manager.login_message_category = 'info'
self.login_manager.login_message = 'Access denied.'
self.login_manager.init_app(self.app)
def init_app(self):
self.login_manager.user_loader(self.load_user)
self.app.route('/favicon.ico')(self.favicon)
self.app.route('/')(self.index)
self.app.route('/questions/<questionName>', methods=['GET', 'POST'])(login_required(self.question))
self.app.route('/login', methods=['GET', 'POST'])(self.login)
self.app.route('/logout')(login_required(self.logout))
def load_user(self, userId):
if Users.getUser(userId) is not None:
myUser = Users.User()
myUser.id = userId
return myUser
def favicon(self):
return send_from_directory(os.path.join(self.app.root_path, 'static'),'favicon.ico', mimetype='image/vnd.microsoft.icon')
def index(self):
#return 'hello world'
questionList = []
for item in os.listdir(self.path):
if item.split('.')[-1] == 'xml':
questionList.append(item[:-4])
return render_template('index-template.html', questions=questionList)
def question(self, questionName):
myRunner = runner()
# get question text
myQuestionFile = rd(self.path, questionName + '.xml')
myQuestionText = myQuestionFile.getQuestion()
# get requirements
myRequirements = ''
for item in myQuestionFile.getExpectedOutputs():
myRequirements += item[0]
myRequirements += ' ---> '
myRequirements += item[1]
myRequirements += '|'
# programming field as form
programForm = ProgrammingForm()
# handle incoming data
if request.method == 'POST':
# get proper inputs and outputs
myInput = []
myOutput = []
for item in myQuestionFile.getExpectedOutputs():
myInput.append(item[0])
myOutput.append(item[1])
# send to online IDE
myResult = myRunner.checkValue(programForm.programField.data, myInput)
# check if correct and calculate result
displayResult = ''
processedMyResult = myResult['output'].split('\n')
processedMyResult.pop()
print("myresult: " + str(len(processedMyResult)))
print ("myoutput length: " + str(len(myOutput)))
flagAllCorrect = True
if len(processedMyResult) == len(myOutput):
for i in range(0, len(processedMyResult)):
displayResult += myInput[i] + ': '
if processedMyResult[i] == myOutput[i]:
displayResult += 'correct'
else:
displayResult += 'incorrect'
flagAllCorrect = False
displayResult += '|'
else:
displayResult = 'error:|'
flagAllCorrect = False
displayResult += myResult['output']
pass
# save student result
if flagAllCorrect:
saver.AppendStudentReport(programForm.programField.data + '\n\nResult:\n' + displayResult, self.path + '/StudentReports/' + questionName, current_user.get_id() + '.correct')
else:
saver.AppendStudentReport(programForm.programField.data + '\n\nResult:\n' + displayResult, self.path + '/StudentReports/' + questionName,
current_user.get_id() + '.incorrect')
#render result
return render_template('question-template.html', questionName=questionName, questionText=myQuestionText, requirements=myRequirements,
form=programForm, result=displayResult)
# render html
return render_template('question-template.html', questionName=questionName, questionText=myQuestionText, requirements=myRequirements, form=programForm)
def login(self):
if request.method == 'POST':
userId = request.form.get('userid')
myUser = Users.getUser(userId)
if myUser is not None and request.form['password'] == myUser['password']:
currUser = Users.User()
currUser.id = userId
# login with flask-login
login_user(currUser)
return redirect(url_for('index'))
# login fails
flash('Wrong username or password!')
return render_template('login.html')
def logout(self):
logout_user()
return render_template('LoggedOut.html')
if __name__ == '__main__':
myapp = webapp2()
myapp.init_app()
myapp.app.run(host='0.0.0.0', port=80)
#myapp.app.run(debug=True)