-
Notifications
You must be signed in to change notification settings - Fork 7
/
controller.py
275 lines (206 loc) · 6.41 KB
/
controller.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
from flask import Flask, render_template, request
from utils import create_question_object, create_score_object, get_field
app = Flask(__name__, template_folder="templates")
ADMIN_EMAIL = "[email protected]"
ADMIN_PASSWORD = "12789"
email_list = []
password_list = []
wholeCredentials = []
email = ""
password = ""
name = ""
authentic = ""
@app.route("/quiz", methods=["POST", "GET"])
def quiz():
qno = 0
quiz = []
questions = []
questions_file = open("questions.txt", "r")
questions = questions_file.read().splitlines()
questions_file.close()
for question in questions:
qno += 1
question_object = create_question_object(question, qno)
quiz.append(question_object)
qno = 0
return render_template("quiz.html", array=quiz)
@app.route("/index")
def home():
global email
global password
global authentic
if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
return render_template("admin.html")
elif verify(email, password):
return render_template("user.html", var=authentic)
return render_template("index.html")
@app.route("/onsignup", methods=["POST", "GET"])
def submit():
global email
global password
global name
name = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")
user = (
str(name)
+ ","
+ str(email)
+ ","
+ str(password)
+ ","
+ "0"
+ ","
+ "0"
+ ","
+ "0"
)
users_data_file = open("users_data.txt", "a")
print(user, file=users_data_file, sep="\n")
users_data_file.close()
return render_template("index.html")
@app.route("/onlogin", methods=["POST", "GET"])
def user_verify():
global email
global password
global wholeCredentials
global authentic
email = request.form.get("email")
password = str(request.form.get("password"))
if verify(email, password):
return render_template("user.html", var=authentic)
elif email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
return render_template("admin.html")
return render_template("invalid.html")
def verify(email, password):
global authentic
email_list = []
password_list = []
users = []
users_data_file = open("users_data.txt", "r")
users = users_data_file.read().splitlines()
users_data_file.close()
for idx in range(0, len(users)):
email_list.append(get_field(users[idx], 1))
password_list.append(get_field(users[idx], 2))
for idx in range(0, len(email_list)):
if email == email_list[idx] and password == password_list[idx]:
authentic = get_field(users[idx], 0)
return True
return False
@app.route("/showall", methods=["POST", "GET"])
def show_all():
scores_list = []
users = []
user_data_file = open("users_data.txt", "r")
users = user_data_file.read().splitlines()
user_data_file.close()
for user in users:
user_score = create_score_object(user)
scores_list.append(user_score)
return render_template("showall.html", list=scores_list)
@app.route("/addquestion", methods=["POST", "GET"])
def add_question():
question_string = request.form.get("question")
option_1 = request.form.get("op1")
option_2 = request.form.get("op2")
option_3 = request.form.get("op3")
option_4 = request.form.get("op4")
correct_option = request.form.get("corop")
question = (
question_string
+ ","
+ option_1
+ ","
+ option_2
+ ","
+ option_3
+ ","
+ option_4
+ ","
+ correct_option
)
questions_file = open("questions.txt", "a")
print(question, file=questions_file, sep="\n")
questions_file.close()
return render_template("admin.html")
@app.route("/submit", methods=["POST", "GET"])
def submit_quiz():
global email
users = []
attempts = []
score = 0
quiz = []
questions_file = open("questions.txt", "r")
questions = questions_file.read().splitlines()
questions_file.close()
number = 0
for question in questions:
question_object = create_question_object(question, number)
quiz.append(question_object)
for idx in range(0, len(quiz)):
mcq = "mcq" + str(idx + 1)
attempts.append(request.form.get(mcq))
for idx in range(0, len(quiz)):
if quiz[idx].correct == attempts[idx]:
score += 1
user_data_file = open("users_data.txt", "r")
users = user_data_file.read().splitlines()
user_data_file.close()
for idx in range(0, len(users)):
if email == get_field(users[idx], 1):
users[idx] = (
str(get_field(users[idx], 0))
+ ","
+ str(get_field(users[idx], 1))
+ ","
+ str(get_field(users[idx], 2))
+ ","
+ str(score)
+ ","
+ str(len(attempts))
+ ","
+ str(len(quiz))
)
user_data_file = open("users_data.txt", "w")
for user in users:
print(user, file=user_data_file, sep="\n")
user_data_file.close()
return render_template("user.html")
@app.route("/login", methods=["POST", "GET"])
def validation():
return render_template("login.html")
@app.route("/show", methods=["POST", "GET"])
def results():
global email
users = []
attempts = 0
users_data_file = open("users_data.txt", "r")
users = users_data_file.read().splitlines()
users_data_file.close()
score = 0
for user in users:
check = get_field(user, 1)
if email == check:
score = str(get_field(user, 3))
attempts = str(get_field(user, 4))
return render_template("result.html", var1=score, var2=attempts)
@app.route("/register", methods=["POST", "GET"])
def register():
return render_template("register.html")
@app.route("/quizstrt", methods=["POST", "GET"])
def strt():
return render_template("quizstrt.html")
@app.route("/contact", methods=["POST", "GET"])
def get_social():
return render_template("contact.html")
@app.route("/add", methods=["POST", "GET"])
def add():
return render_template("addques.html")
@app.route("/logout", methods=["POST", "GET"])
def logout():
global email
global password
email = ""
password = ""
return render_template("index.html")