-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
178 lines (146 loc) · 7.18 KB
/
app.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
from flask import Flask, render_template, session, redirect, url_for, request, flash
from ads import ads, able_to_graduate
from helpers import login_required, myDb
from students import programTypes
from apps import apps, appIncomplete
from reports import reports
from regs import regs
from chat import chat
app = Flask(__name__)
app.secret_key = "eot"
app.register_blueprint(ads, url_prefix='/ads')
app.register_blueprint(apps, url_prefix='/apps')
app.register_blueprint(regs, url_prefix="/regs")
app.register_blueprint(reports, url_prefix="/reports")
app.register_blueprint(chat, url_prefix="/chat")
@app.route('/')
@login_required
def index():
cursor = myDb.cursor(dictionary=True)
userId = session['userId']
#Get the requested user.
cursor.execute("SELECT `first_name`, `last_name`, `type` FROM user WHERE id = %s", (userId,))
user = cursor.fetchone()
session['userType'] = user['type']
# Redirect to login if user is not found
if user == None:
myDb.commit()
cursor.close()
return redirect(url_for("login"))
#Show the student home view if user if of type student
if user["type"] == "student":
# Check if the student has filled out form 1
cursor.execute("SELECT COUNT(`course_id`) FROM `student_courses_planned` WHERE `user_id` = %s", (userId,))
form1 = cursor.fetchone()['COUNT(`course_id`)'] > 0
# Check if the student has an approved advising form
cursor.execute("SELECT `advising_form_approved` FROM `student_info` WHERE `user_id` = %s", (userId,))
advising_form_approved = cursor.fetchone()['advising_form_approved']
# Check if the student has filled out advising form
cursor.execute("SELECT COUNT(`course_id`) FROM `student_courses_planned` WHERE `user_id` = %s AND `form` = 'advising'", (userId,))
advising_form = cursor.fetchone()['COUNT(`course_id`)'] > 0
myDb.commit()
cursor.close()
return render_template("dashboard.html",
user = user,
form1 = form1, able_to_graduate = able_to_graduate(userId),
advising_form_approved = advising_form_approved,
advising_form = advising_form,)
#Show the alum home view if user is of type alum
if user["type"] == "alum":
cursor.execute("SELECT `program`, `grad_year` FROM `alumni_info` WHERE `user_id` = %s", (userId,))
alum_info = cursor.fetchone()
myDb.commit()
cursor.close()
return render_template("dashboard.html", user=user, alum_info=alum_info, programTypes=programTypes)
#Show the faculty advisor view if user is of type faculty advisor
if user["type"] == "faculty":
cursor.execute("SELECT `is_advisor`, `is_reviewer`, `is_instructor`, `is_admissions_chair` FROM `faculty_info` WHERE `user_id` = %s", (userId,))
faculty_info = cursor.fetchone()
myDb.commit()
cursor.close()
return render_template("dashboard.html", user=user, faculty=faculty_info)
# Applicant home view
if user["type"] == "applicant":
# Get materials needed
materials = appIncomplete(session["userId"])
# Get application status
appStatus = None
cursor.execute("SELECT appStatus FROM applicant WHERE university_id = %s", (session["userId"],))
result = cursor.fetchone()
if cursor.rowcount > 0:
appStatus = result["appStatus"]
# Check if an application has been submitted
cursor.execute("SELECT university_id, transcriptStatus, r1status, r2status, r3status FROM applicationForm WHERE university_id = %s", (session["userId"],))
result = cursor.fetchone()
appSubmitted = cursor.rowcount > 0
myDb.commit()
cursor.close()
return render_template("dashboard.html", user=user, appStatus=appStatus, appSubmitted=appSubmitted, materialsNeeded=materials)
return render_template("dashboard.html", user=user)
@app.route("/login", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
cursor = myDb.cursor(dictionary=True)
cursor.execute("SELECT `id`, `type`, `username`, `password` FROM user WHERE username=%s and password=%s", (request.form['username'],request.form['password']))
user = cursor.fetchone()
if user == None:
flash("User/password is incorrect. Please try again.", "warning")
myDb.commit()
cursor.close()
return render_template('login.html')
session['userId'] = user['id']
session['id'] = user['id']
session['username'] = user['username']
session['userType'] = user['type']
session['type'] = user['type']
session['registration'] = []
myDb.commit()
cursor.close()
if 'next' in session:
return redirect(session.pop('next', None))
return redirect(url_for("index"))
return render_template('login.html')
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for("login"))
@app.route("/create_acc_page")
def create_acc_page():
if 'userId' not in session:
return render_template("create_account.html")
else:
return redirect(url_for('login'))
@app.route("/create_acc", methods=['GET', 'POST'])
def create_acc():
cursor = myDb.cursor(dictionary=True)
if request.method == 'POST':
cursor.execute("SELECT username FROM user WHERE username=%s", (request.form["userName"],))
name = cursor.fetchone()
if name == None:
cursor.execute("INSERT INTO `user` (`type`, `email`, `username`, `password`, `first_name`,`last_name`, `street_address`, `city`, `state`, `zip`) VALUES (%s, %s,%s, %s, %s, %s, %s, %s, %s, %s)", ("applicant", request.form['email'], request.form['userName'], request.form['pass'], request.form['firstName'], request.form['lastName'], request.form['streetAdd'], request.form['city'], request.form['state'], request.form['zip'] ))
myDb.commit()
cursor.execute("SELECT id FROM user WHERE username=%s", (request.form["userName"],))
id = cursor.fetchone()
# print (id)
# print (id['id'])
university_id = id['id']
# insert data into applicant table
# assume everyone making an account is an applicant
appStatus = "Incomplete"
mail_transcript = "No"
appStatus = "Pending"
ssn = request.form["ssn"]
username = request.form["userName"]
cursor.execute("INSERT INTO applicant (ssn, appStatus, university_id, mail_transcript) VALUES (%s, %s, %s, %s)",
(ssn, appStatus, university_id, mail_transcript,))
myDb.commit()
cursor.close()
flash("You have successfully created your account! Please sign in.", "success")
return redirect(url_for('login'))
flash("This username already exists. Please choose a different one.", "danger")
myDb.commit()
cursor.close()
return redirect(url_for('create_acc_page'))
@app.errorhandler(401)
def forbidden(e):
return render_template('401.html')