-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
414 lines (324 loc) · 14.6 KB
/
__init__.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
#!usr/bin/env python
#########################################
################ Imports ################
#########################################
from flask import (Flask, render_template, url_for, redirect,
flash, g, request, session)
from hashlib import md5
from functools import wraps
import sqlite3
import gc
import models
import forms
from courses import (bio_one_one, bio_one_two, bio_two, chem_one_one,
chem_one_two,chem_one_three, chem_one_four, chem_one_five,
chem_one_six, chem_two_one, chem_two_two, chem_two_three,
physics_one_one, physics_one_two, physics_one_three,
physics_two_one, physics_two_two, architecture,
humanities_one, humanities_two, psychology_one,
psychology_two, political_science, compsci_one, compsci_two)
#########################################
############### Setup ###################
#########################################
app = Flask(__name__)
app.secret_key = 'REDACTED'
@app.before_request
def before_request():
'''Connect to a database before each request.'''
g.db = models.DATABASE
g.db.connect()
@app.after_request
def after_request(response):
'''Close the database connection after each request.'''
g.db.close()
return response
def login_required(f):
'''decorator to check if user is logged in, if they're not it redirects them to the login page'''
@wraps(f)
def decorated_function(*args, **kwargs):
try:
if session['username'] == False:
flash('You need to login inorder to view this page')
return redirect(url_for('login', next=request.url))
except KeyError:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
conn = sqlite3.connect("classmates.db", check_same_thread=False)
c = conn.cursor()
#########################################
############## Routing ##################
#########################################
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about/')
def about_page():
return render_template('about.html')
@app.route('/register/', methods=['GET', 'POST'])
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
flash("Yay, you've registered succesfully!", "success")
models.Classmate.create_user(
first_name = form.first_name.data,
last_name = form.last_name.data,
username = form.username.data,
password = form.password.data,
first = form.first.data,
second = form.second.data,
third = form.third.data,
fourth = form.fourth.data,
fifth = form.fifth.data,
sixth = form.sixth.data)
session['logged_in'] = True
session['username'] = form.username.data
return redirect(url_for('class_check'))
return render_template('register.html', form=form)
@app.route('/login/', methods=['GET', 'POST'])
def login():
form = forms.LoginForm()
if form.validate_on_submit():
try:
user = models.Classmate.get(models.Classmate.username == form.username.data)
except models.DoesNotExist:
flash('Your username or password do not match!', 'error')
else:
if user.password == md5(form.password.data).hexdigest():
session['logged_in'] = True
session['username'] = form.username.data
flash("You've successfully logged in!")
return redirect(url_for('class_check'))
else:
flash('Your username or password do not match!', 'error')
return render_template('login.html', form=form)
@app.route('/logout/')
def logout():
session.pop('logged_in', None)
session.clear()
flash('You have been logged out.')
gc.collect()
return redirect(url_for('index'))
@app.route('/class_codes/')
def class_codes():
return render_template('class_codes.html')
@app.route('/class_check/')
@login_required
def class_check():
student = (session['username'],)
student_courses_string = []
for row in c.execute('SELECT first, second, third, fourth, fifth, sixth FROM classmate WHERE username=?', student):
student_courses_string.append(str(row))
student_courses = []
for lst in student_courses_string:
student_courses.append(lst.strip("()").translate(None, "' ").split(','))
first_class = student_courses[0][0][1:]
second_class = student_courses[0][1][1:]
third_class = student_courses[0][2][1:]
fourth_class = student_courses[0][3][1:]
fifth_class = student_courses[0][4][1:]
if student_courses[0][5] != "none":
sixth_class = student_courses[0][5][1:]
else:
sixth_class = 'somethingcompletelyrandomnoonewillwritedown'
user = session['username']
#--------------SQL query design ----------------------
def make_second_query(lst, usr, first, third, fourth, fifth, sixth):
'''query for when only the second class is in science class list'''
query = """SELECT first_name, last_name, first, second, third, fourth, fifth, sixth
FROM classmate
WHERE first = '{}'
AND username <> '{}'
""".format(first, usr)
for i in lst:
query += "OR second = '" + i + "'" + " AND username <> '{}' ".format(usr)
query += '''OR third = '{}'
AND username <> '{}'
OR fourth = '{}'
AND username <> '{}'
OR fifth = '{}'
AND username <> '{}'
OR sixth = '{}'
AND username <> '{}';'''.format(third, usr,
fourth, usr,
fifth, usr,
sixth, usr)
return query
def make_sixth_elective_query(lst, usr, first, second, third, fourth, fifth):
'''query for when only the sixth class is in elective list'''
query = """SELECT first_name, last_name, first, second, third, fourth, fifth, sixth
FROM classmate
WHERE first = '{}'
AND username <> '{}'
OR second = '{}'
AND username <> '{}'
OR third = '{}'
AND username <> '{}'
OR fourth = '{}'
AND username <> '{}'
OR fifth = '{}'
AND username <> '{}'
""".format(first, usr, second, usr, third, usr, fourth, usr, fifth, usr)
for i in lst:
query += "OR sixth = '" + i + "'" + " AND username <> '{}' ".format(usr)
query += ";"
return query
def make_second_sixth_query(lst1, lst2, usr, first, third, fourth, fifth):
'''query for when second and sixth class are from list'''
query = """SELECT first_name, last_name, first, second, third, fourth, fifth, sixth
FROM classmate
WHERE first = '{}'
AND username <> '{}'
""".format(first, usr)
for i in lst1:
query += "OR second = '" + i + "'" + " AND username <> '{}' ".format(usr)
query += """OR third = '{}'
AND username <> '{}'
OR fourth = '{}'
AND username <> '{}'
OR fifth = '{}'
AND username <> '{}' """.format(third, usr, fourth, usr, fifth, usr)
for i in lst2:
query += "OR sixth = '" + i + "'" + " AND username <> '{}' ".format(usr)
query += ";"
return query
def default_query(usr, first, second, third, fourth, fifth, sixth):
'''query for when none of the classes are in a list'''
query = """SELECT first_name, last_name, first, second, third, fourth, fifth, sixth
FROM classmate
WHERE first = '{}'
AND username <> '{}'
OR second = '{}'
AND username <> '{}'
OR third = '{}'
AND username <> '{}'
OR fourth = '{}'
AND username <> '{}'
OR fifth = '{}'
AND username <> '{}'
OR sixth = '{}'
AND username <> '{}';""".format(first, usr, second, usr, third, usr,
fourth, usr, fifth, usr, sixth, usr)
return query
#----------------- End query design ------------------
SCIENCE_LIST = [bio_one_one, bio_one_two, bio_two, chem_one_one,
chem_one_two,chem_one_three, chem_one_four, chem_one_five,
chem_one_six, chem_two_one, chem_two_two, chem_two_three,
physics_one_one, physics_one_two, physics_one_three,
physics_two_one, physics_two_two, compsci_one, compsci_two]
ELECTIVE_LIST = [architecture, humanities_one, humanities_two, psychology_one,
psychology_two, political_science]
def check_proper_query(usr, first, second, third, fourth, fifth, sixth):
'''check which query to useon db'''
is_second = False
correct_second_list = None
for lst in SCIENCE_LIST:
if second in lst:
is_second = True
correct_second_list = lst
is_sixth = False
correct_sixth_list = None
for lst in ELECTIVE_LIST:
if sixth in lst:
is_sixth = True
correct_sixth_list = lst
query_list = [usr, first, second, third, fourth, fifth, sixth, "0"] # default case
if is_second and not is_sixth:
query_list = [correct_second_list, usr, first, third, fourth, fifth, sixth, "2"] # if only 2
elif is_sixth and not is_second:
query_list = [correct_sixth_list, usr, first, second, third, fourth, fifth, "6"] # if only 6
elif is_second and is_sixth:
query_list = [correct_second_list, correct_sixth_list, usr, first, third, fourth, fifth, "26"] # if 2 and 6
return query_list
#---------------------------------------------------
list_of_args = check_proper_query(user, first_class, second_class, third_class, fourth_class, fifth_class, sixth_class)
matched_students_list = []
if list_of_args[-1] == "0":
for row in c.execute(default_query(list_of_args[0], list_of_args[1], list_of_args[2], list_of_args[3],
list_of_args[4], list_of_args[5], list_of_args[6])):
matched_students_list.append(str(row))
elif list_of_args[-1] == "2":
for row in c.execute(make_second_query(list_of_args[0], list_of_args[1], list_of_args[2], list_of_args[3],
list_of_args[4], list_of_args[5], list_of_args[6])):
matched_students_list.append(str(row))
elif list_of_args[-1] == "6":
for row in c.execute(make_sixth_elective_query(list_of_args[0], list_of_args[1], list_of_args[2], list_of_args[3],
list_of_args[4], list_of_args[5], list_of_args[6])):
matched_students_list.append(str(row))
elif list_of_args[-1] == "26":
for row in c.execute(make_second_sixth_query(list_of_args[0], list_of_args[1], list_of_args[2], list_of_args[3],
list_of_args[4], list_of_args[5], list_of_args[6])):
matched_students_list.append(str(row))
else:
pass
matched_students = []
for lst in matched_students_list:
matched_students.append(lst.strip("()").translate(None, "' ").split(','))
students_in_math_class = []
for student in matched_students:
if student[2][1:] == first_class:
students_in_math_class.append(student)
student_science_list = []
for lst in SCIENCE_LIST:
if second_class in lst:
student_science_list = lst
break
students_in_science_class = []
for student in matched_students:
for person in student_science_list:
if student[3][1:] in person:
students_in_science_class.append(student)
students_in_english_class = []
for student in matched_students:
if student[4][1:] == third_class:
students_in_english_class.append(student)
students_in_speech_class = []
for student in matched_students:
if student[5][1:] == fourth_class:
students_in_speech_class.append(student)
students_in_fifth = []
for student in matched_students:
if student[6][1:] == fifth_class:
students_in_fifth.append(student)
student_six_list = []
for lst in ELECTIVE_LIST:
if sixth_class in lst:
student_six_list = lst
break
else:
student_six_list = []
students_in_sixth = []
for student in matched_students:
if student[7][1:] in student_six_list or student[7][1:] == sixth_class:
students_in_sixth.append(student)
return render_template('class_check.html', user = student_courses, one = students_in_math_class,
two = students_in_science_class, three = students_in_english_class,
four = students_in_speech_class, five = students_in_fifth, six = students_in_sixth)
#########################################
########### Error Handling ##############
#########################################
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html")
@app.errorhandler(500)
def internal_error(e):
return render_template("500.html")
if __name__ == '__main__':
models.initialize()
try:
models.Classmate.create_user(
username = "Test",
password='REDACTED',
admin=False,
first_name='test',
last_name="test",
first = "nononecanchoosethisvaluesoitwontpair",
second = "nononecanchoosethisvaluesoitwontpair",
third = "nononecanchoosethisvaluesoitwontpair",
fourth = "nononecanchoosethisvaluesoitwontpair",
fifth = "nononecanchoosethisvaluesoitwontpair",
sixth = "nononecanchoosethisvaluesoitwontpair"
)
except ValueError:
pass
app.run(debug = True)