Repository navigation
Expand file tree
/
Copy pathapplication.py
More file actions
606 lines (532 loc) · 23.9 KB
/
Copy pathapplication.py
File metadata and controls
606 lines (532 loc) · 23.9 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/env python
import flask
from flask import request
import database_files.student_database as student_database
import database_files.courses_database as courses_database
import database_files.minors_database as minors_database
import json
import dotenv
import auth
import recommendation
import courses as course_dicts
import recommender2
import math
app = flask.Flask(__name__, template_folder='templates')
app.secret_key = '12345'
'''
unsure what these mean, were in ref prog
dotenv.load_dotenv()
app.secret_key = os.environ['APP_SECRET_KEY']
'''
def login_required(previous_page_url):
flask.session['previous_page'] = previous_page_url
return flask.redirect('/login')
#-----------------------------------------------------------------------
def restricted_access(func):
def wrapper(*args, **kwargs):
if flask.request.referrer is None:
return flask.redirect('/error')
return func(*args, **kwargs)
wrapper.__name__ = func.__name__
return wrapper
#-----------------------------------------------------------------------
@app.route('/', methods=['GET'])
@app.route('/home', methods=['GET'])
def home():
try:
username = flask.session.get('username')
html_code = flask.render_template("homepage.html", username=username)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
#-----------------------------------------------------------------------
#username and classes retrieval
def get_user_info():
username = flask.session.get('username')
if username is None:
return False, None, []
try:
classes = student_database.get_student_classes(username)
name = student_database.get_student_name(username)
if name == '':
name = username
return True, username, classes
except Exception as e:
return False, e, []
def map_major_name_to_id(major):
majors_mapping = {
'African American Studies': 'AAS',
'African Studies': 'AFS',
'Asian American Studies': 'ASA',
'Anthropology': 'ANT',
'Architecture': 'ARC',
'Art and Archaeology': 'ART',
'Astrophysics': 'AST',
'Chemical and Biological Engineering': 'CBE',
'Chemistry': 'CHM',
'Chinese Language': 'CHI',
'Civil and Environmental Engineering': 'CEE',
'Classics': 'CLA',
'Climate Science': 'CS',
'Comparative Literature': 'COM',
'Computer Science': 'COS',
'Creative Writing': 'CWR',
'Dance': 'DAN',
'East Asian Studies': 'EAS',
'Economics': 'ECO',
'Electrical and Computer Engineering': 'ECE',
'English': 'ENG',
'Environmental Studies': 'ENV',
'Ecology and Evolutionary Biology': 'EEB',
'Finance': 'FIN',
'French and Italian': 'FRE',
'Geosciences': 'GEO',
'German': 'GER',
'Global Health and Health Policy': 'GHP',
'Gender Sexuality Studies': 'GSS',
'History': 'HIS',
'Hellenic Studies': 'HLS',
'History of Science, Technology, and Medicine': 'HSTM',
'Humanistic Studies': 'HUM',
'Japanese Language': 'JPN',
'Journalism': 'JRN',
'Korean Language': 'KOR',
'Latino Studies': 'LAO',
'Linguistics': 'LIN',
'Mathematics': 'MAT',
'Materials Science and Engineering': 'MSE',
'Mechanical and Aerospace Engineering': 'MAE',
'Medieval Studies': 'MED',
'Music': 'MUS',
'Music Performance': 'MPP',
'Near Eastern Studies': 'NES',
'Neuroscience': 'NEU',
'Operations Research and Financial Engineering': 'ORF',
'Translation and Intercultural Communication': 'TRA',
'Philosophy': 'PHI',
'Physics': 'PHY',
'Politics': 'POL',
'Princeton School of Public and International Affairs': 'SPI',
'Psychology': 'PSY',
'Quantitative Economics': 'MQE',
'Quantitative and Computational Biology': 'QCB',
'Russian, East European, and Eurasian Studies': 'RES',
'Religion': 'REL',
'Souh Asian Studies': 'SAS',
'Slavic Languages and Literatures': 'SLA',
'Statistics and Machine Learning': 'SML',
'Sociology': 'SOC',
'Spanish and Portuguese': 'SPA',
'Theater and Music Theater': 'TMT',
'Visual Arts': 'VIS',
'Values and Public Life': 'VPL'
}
return majors_mapping.get(major, '')
def map_major_id_to_name(major):
reverse_mapping = {
'AAS': 'African American Studies',
'AFS': 'African Studies',
'ASA': 'Asian American Studies',
'ANT': 'Anthropology',
'ARC': 'Architecture',
'ART': 'Art and Archaeology',
'AST': 'Astrophysics',
'CBE': 'Chemical and Biological Engineering',
'CHM': 'Chemistry',
'CHI': 'Chinese Language',
'CEE': 'Civil and Environmental Engineering',
'CLA': 'Classics',
'CS': 'Climate Science',
'COM': 'Comparative Literature',
'COS': 'Computer Science',
'CWR': 'Creative Writing',
'DAN': 'Dance',
'EAS': 'East Asian Studies',
'ECO': 'Economics',
'ECE': 'Electrical and Computer Engineering',
'ENG': 'English',
'ENV': 'Environmental Studies',
'EEB': 'Ecology and Evolutionary Biology',
'FIN': 'Finance',
'FRE': 'French and Italian',
'GEO': 'Geosciences',
'GER': 'German',
'GHP': 'Global Health and Health Policy',
'GSS': 'Gender Sexuality Studies',
'HIS': 'History',
'HLS': 'Hellenic Studies',
'HSTM': 'History of Science, Technology, and Medicine',
'HUM': 'Humanistic Studies',
'JPN': 'Japanese Language',
'JRN': 'Journalism',
'KOR': 'Korean Language',
'LAO': 'Latino Studies',
'LIN': 'Linguistics',
'MAT': 'Mathematics',
'MSE': 'Materials Science and Engineering',
'MAE': 'Mechanical and Aerospace Engineering',
'MED': 'Medieval Studies',
'MUS': 'Music',
'MPP': 'Music Performance',
'NES': 'Near Eastern Studies',
'NEU': 'Neuroscience',
'ORF': 'Operations Research and Financial Engineering',
'TRA': 'Translation and Intercultural Communication',
'PHI': 'Philosophy',
'PHY': 'Physics',
'POL': 'Politics',
'SPI': 'Princeton School of Public and International Affairs',
'PSY': 'Psychology',
'MQE': 'Quantitative Economics',
'QCB': 'Quantitative and Computational Biology',
'RES': 'Russian, East European, and Eurasian Studies',
'REL': 'Religion',
'SAS': 'South Asian Studies',
'SLA': 'Slavic Languages and Literatures',
'SML': 'Statistics and Machine Learning',
'SOC': 'Sociology',
'SPA': 'Spanish and Portuguese',
'TMT': 'Theater and Music Theater',
'VIS': 'Visual Arts',
'VPL': 'Values and Public Life'
}
return reverse_mapping.get(major, '')
def generate_and_store_recommendations(username):
try:
# Placeholder recommendation logic
courses = student_database.get_student_coursenums(username)
recommended_courses = list(recommendation.recommend(courses, username))
print("recommended!")
# Store recommended courses in the database
student_database.store_recommendations(username, recommended_courses)
print("stored!")
return recommended_courses
except Exception as e:
raise e
def distance_to_minor(username, class_list, minor):
try:
courses = recommendation.categorize_courses(class_list, course_dicts.get_courses(minor))
subrequirements = course_dicts.get_minor_requirements(minor)
_, combinations = recommender2.generate_combinations(courses, subrequirements)
if len(combinations) == 0:
best_combination = []
tree_description = ''
distance = math.inf
else:
best_combination, _, classes_taken_needed, _, tree_description = recommender2.find_best_combination(minor, combinations, subrequirements)
distance = classes_taken_needed[1] - classes_taken_needed[0]
course = {'distance': distance, 'minor': minor, 'best_combination': best_combination, 'tree_description': tree_description}
return course
except Exception as e:
raise e
#-----------------------------------------------------------------------
#Routes for authentication.
@app.route('/login', methods=['GET'])
def login():
username = auth.authenticate()
success, first_time = student_database.handle_student_login(username)
if success:
flask.session['username'] = username
if first_time:
print("First time login")
html_code = flask.render_template("loginpage.html", username=username)
response = flask.make_response(html_code)
return response
else:
print("Returning user")
if flask.session.get('previous_page') == None:
return flask.redirect('/classboard')
else:
return flask.redirect(flask.session['previous_page'])
else:
return flask.abort(401)
@app.route('/logoutapp', methods=['GET'])
def logoutapp():
flask.session.pop('username', None)
return auth.logoutapp()
@app.route('/logoutcas', methods=['GET'])
def logoutcas():
flask.session.pop('username', None)
return auth.logoutcas()
#-----------------------------------------------------------------------
#Routes for profile.
@app.route('/profile', methods=['GET'])
def profile():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/profile')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message)
name = student_database.get_student_name(username)
major = map_major_id_to_name(student_database.get_student_major(username))
html_code = flask.render_template("profile.html", username=username
, name=name, major=major)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/update_profile', methods=['POST'])
@restricted_access
def reset_profile():
try:
username = flask.session.get('username')
if username is None:
return login_required(previous_page_url='/update_profile')
name = request.form.get('name')
if name == '':
name = student_database.get_student_name(username)
# Retrieve the selected major id from the form data
major_name = request.form.get('major')
if not major_name:
# If major_id is not provided (shouldn't happen), use default logic
major = map_major_id_to_name(student_database.get_student_major(username))
else:
# update major if you get new input
major = map_major_name_to_id(major_name)
student_database.update_student_profile(username, name, major)
return flask.redirect('/profile')
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/set_profile', methods=['POST'])
def set_profile():
try:
username = flask.session.get('username')
if username is None:
return flask.redirect('/login')
name = request.form.get('name')
major = map_major_name_to_id(request.form.get('major'))
student_database.update_student_profile(username, name, major)
return flask.redirect('/classboard')
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
#-----------------------------------------------------------------------
# Routes for All Minors page
@app.route('/allminors', methods=['GET'])
def allminors():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/allminors')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
minors = minors_database.get_all_minors()
html_code = flask.render_template("allminors.html", username=username, minors=minors)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/distance', methods=['GET'])
def minor():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/minor')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
# find the distance to the clicked on minor
minor_code = request.args.get('code')
print(minor_code)
# recommendation logic using recommender2
class_list = student_database.get_student_coursenums(username)
recommendation = distance_to_minor(username, class_list, minor_code)
recommendation['minorid'] = recommendation['minor']
recommendation['minor'] = map_major_id_to_name(recommendation['minor'])
#reminder to self to add truncation of description if too long
recommendation['desc'] = minors_database.get_desc(recommendation['minorid'])
recommendation['urls'] = minors_database.get_urls(recommendation['minorid'])
return flask.render_template("minor.html", username=username, course=recommendation)
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
#-----------------------------------------------------------------------
#Routes for classboard.
@app.route('/classboard', methods=['GET'])
def classboard():
try:
success, username, classes = get_user_info()
if username is None:
return login_required(previous_page_url='/classboard')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500 # Return a 500 Internal Server Error status code
html_code = flask.render_template("classboard.html", username=username, classes=classes)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/searchresults', methods=['GET'])
@restricted_access
def search_results():
try:
course = flask.request.args.get('course')
if course is None:
course = ''
course = course.strip()
if course == '':
result = []
try:
result = courses_database.search_courses(course)
if result is None:
result = []
html_code = flask.render_template('course.html', courses=result)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500 # Return a 500 Internal Server Error status code
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/addcourse', methods=['POST'])
def add_course():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/addcourse')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
id = request.args.get('courseId')
print(id)
coursenum = request.args.get('courseNum')
print(coursenum)
title = request.args.get('title')
print(title)
course = {'id': id, 'coursenum': coursenum, 'title': title}
try:
student_database.update_student_classes(username, [course], None)
return flask.redirect('/loadarea')
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/removecourse', methods=['POST'])
def remove_course():
try:
print("remove course")
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/removecourse')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
id = request.args.get('courseId')
try:
student_database.update_student_classes(username, classes_to_remove=[id])
return flask.redirect('/loadarea')
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/loadarea', methods=['GET'])
@restricted_access
def load_area():
try:
success, username, classes = get_user_info()
if username is None:
return login_required(previous_page_url='/loadarea')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500 # Return a 500 Internal Server Error status code
html_code = flask.render_template("droparea.html", classes=classes)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
#-----------------------------------------------------------------------
#Routes for recommend.
@app.route('/recommend', methods=['GET'])
def recommend():
try:
success, username, classes = get_user_info()
if username is None:
return login_required(previous_page_url='/recommend')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
_ = generate_and_store_recommendations(username)
# reroute to recommendations page
return flask.redirect('/recommendations')
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.route('/recommendations', methods=['GET'])
def recommendations():
try:
success, username, classes = get_user_info()
if username is None:
return login_required(previous_page_url='/recommendations')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
stored_recommendations = student_database.get_stored_recommendations(username)
if not stored_recommendations:
# No stored recommendations found, generate and store new ones
stored_recommendations = generate_and_store_recommendations(username)
# Map minor IDs to names for display
for course in stored_recommendations:
course['minorid'] = course['minor']
course['minor'] = map_major_id_to_name(course['minor'])
#reminder to self to add truncation of description if too long
course['desc'] = minors_database.get_desc(course['minorid'])
course['urls'] = minors_database.get_urls(course['minorid'])
#course['tree_description'] = json.loads(course['tree_description'])
return flask.render_template("recommend.html", username=username, courses=stored_recommendations)
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
@app.errorhandler(405)
def access_not_allowed(e):
return flask.render_template('error.html', username=flask.session.get('username'), error="You do not have access to this page"), 405
@app.errorhandler(404)
def page_not_found(e):
return flask.render_template('error.html', username=flask.session.get('username'), error="The requested page does not exist"), 404
@app.route('/details', methods = ['GET'])
@restricted_access
def details():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/details')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500 # Return a 500 Internal Server Error status code
minor = flask.request.args.get('minor')
# print(minor)
return flask.render_template("minor.html", username=username, minor = minor)
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500
#-----------------------------------------------------------------------
#Other routes
@app.route('/about', methods=['GET'])
def about():
try:
success, username, _ = get_user_info()
if username is None:
return login_required(previous_page_url='/about')
if not success:
error_message = f"An error occurred: {str(username)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500 # Return a 500 Internal Server Error status code
html_code = flask.render_template("about.html", username = username)
response = flask.make_response(html_code)
return response
except Exception as e:
error_message = f"An error occurred: {str(e)}"
return flask.render_template("error.html", username=flask.session.get('username'), error=error_message), 500