-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
160 lines (136 loc) · 5.42 KB
/
app.py
File metadata and controls
160 lines (136 loc) · 5.42 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
from flask import Flask, render_template, request, redirect, session
import pandas as pd
from GradeDistribution import GradeDistribution
import os
import shutil
#import json
# Can be "testing" or "Netlify"
run_as_netlify = True
app = Flask(__name__)
app.secret_key = 'supersecretkey' # Required for session management
users_data_file = "databases/users.csv"
grades_data_file = "databases/grades.csv"
# Read initial data from CSV
try:
df = pd.read_csv(users_data_file)
except FileNotFoundError:
df = pd.DataFrame(columns=["First Name", "Last Name", "Email", "Password"]) # , "Courses"
df.to_csv(users_data_file, index=False)
grade_dist = GradeDistribution(grades_data_file)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/register', methods=['POST'])
def register_user():
global df
first_name = request.form['first_name']
last_name = request.form['last_name']
email = request.form['email']
password = request.form['password']
confirm_password = request.form['confirm_password']
# Check if passwords match
confirm_password = request.form['confirm_password']
if password != confirm_password:
error = "Passwords do not match. Please try again"
return render_template('register.html', error=error)
# Check if email already exists
if email in df['Email'].values:
error = "This email is already in use"
return render_template('register.html', error=error)
new_user = pd.DataFrame({'First Name': [first_name],
'Last Name': [last_name],
'Email': [email],
'Password': [password]}) #'Courses': {}
df = pd.concat([df, new_user], ignore_index=True)
df.to_csv(users_data_file, index=False)
# Log the user in
session['email'] = email
return redirect('/profile')
@app.route('/login', methods=['GET', 'POST'])
def signin():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
# Check if email and password match
user = df[(df['Email'] == email) & (df['Password'] == password)]
if user.empty:
error = "Invalid username or password"
return render_template('login.html', error=error)
# Log the user in
session['email'] = email
return redirect('/profile')
else:
return render_template('login.html')
@app.route('/profile')
def profile():
global df
if 'email' not in session:
return redirect('/login')
email = session['email']
user = df[df['Email'] == email]
first_name = user['First Name'].iloc[0]
last_name = user['Last Name'].iloc[0]
# Retrieve the user's courses dictionary from DataFrame
#courses_json_str = user['Courses'].iloc[0]
#courses_dict = json.loads(courses_json_str) if not pd.isna(courses_json_str) else {}
return render_template('profile.html', first_name=first_name, last_name=last_name) # , courses=courses_dict
#@app.route('/add_course', methods=['POST'])
#def add_course():
# global df
# if 'email' not in session:
# return redirect('/login')
#
# email = session['email']
# course_dept = request.form['course_dept']
# course_num = request.form['course_num']
# role = request.form['role']
#
# # Construct the course key
# course_key = f"{course_dept} {course_num}"
#
# # Retrieve the user's courses dictionary from DataFrame
# courses_json_str = df[df['Email'] == email]['Courses'].iloc[0]
# courses_dict = json.loads(courses_json_str) if not pd.isna(courses_json_str) else {}
#
# # Add the new course and role to the dictionary
# courses_dict[course_key] = role
#
# # Update the DataFrame with the modified courses dictionary
# df.loc[df['Email'] == email, 'Courses'] = json.dumps(courses_dict)
# df.to_csv(users_data_file, index=False)
#
# return redirect('/profile')
@app.route('/prof_search', methods=['GET', 'POST'])
def prof_search():
return render_template('prof_search.html', ranked_profs=None)
#if request.method == 'POST':
# course_dept = request.form['course_dept']
# course_num = request.form['course_num']
# course_data = grade_dist.search_class(course_dept, course_num)
#
# if not course_data.empty:
# ranked_profs = grade_dist.rank_profs(course_data)
# return render_template('prof_search.html', ranked_profs=ranked_profs)
#else:
# return render_template('prof_search.html', ranked_profs=None)
if __name__ == '__main__':
if run_as_netlify == True:
# Iterate over each file in the source directory
for filename in os.listdir("templates"):
# Construct the source and destination file paths
source_file = os.path.join("templates", filename)
destination_file = os.path.join(".", filename)
# Check if the current item is a file (not a subdirectory)
if os.path.isfile(source_file):
# Copy the file from the source to the destination
shutil.copy(source_file, destination_file)
print(f'Copied {source_file} to {destination_file}')
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
else:
app.run(debug=True)