-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
387 lines (308 loc) · 14.5 KB
/
main.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
from flask import Flask, redirect, url_for, request, render_template, session, flash
from flask_sqlalchemy import SQLAlchemy
import random
# custom package import
from email_helper.send_mail import sendEmail
# create the flask app object
app = Flask(__name__)
# set the secret key for the session
app.secret_key = "hello"
# settings for sql database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# create the database object
db = SQLAlchemy(app)
teacherPassword = '314159'
class Student(db.Model):
# create columns
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column(db.String(100))
period = db.Column(db.Integer())
email = db.Column(db.String(100))
password = db.Column(db.String(6))
rp = db.Column(db.Integer())
def __init__(self, name, period, email):
self.name = name
self.period = period
self.email = email
# password is initialized to a random 6-digit number
random_password = random.randint(000000, 999999)
# make sure the password is unique
# get all the passwords from the database
passwords = Student.query.with_entities(Student.password).all()
# check if the random password is already in the database
while str(random_password) in passwords:
random_password = random.randint(000000, 999999)
# add leading zeroes to the password if it's less than 6 digits
random_password = str(random_password)
while len(random_password) < 6:
random_password = "0" + random_password
# set the password
self.password = random_password
self.rp = 0
def __repr__(self):
return f"Name ({self.name}), Password ({self.password}), Points ({self.rp}), Email ({self.email}), Period ({self.period})\n"
# TODO: add a show password button to the password column in teacher view
# route for the home page (it's just the login page)
@app.route("/", methods=["POST", "GET"])
@app.route("/home", methods=["POST", "GET"])
@app.route("/login", methods=["POST", "GET"])
def login():
# first check if you're already logged in
if 'student' in session:
# redirect to the student view
flash("You are already logged in!")
return redirect(url_for("student"))
elif 'teacher' in session:
# redirect to the teacher view
flash("You are already logged in!")
return redirect(url_for("teacher"))
else:
# user is not logged in
# check if the user is trying to log in
if request.method == "POST":
# get the form data
password = request.form["password"]
# prompt password again if it's not a number
if not password.isdigit():
flash("Please enter a number!")
return redirect(url_for("login"))
# prompt password again if it's not 6 digits
if len(password) != 6:
flash("Please enter a 6-digit number!")
return redirect(url_for("login"))
# check if the teacher is trying to log in
if password == teacherPassword:
# log the teacher in
session['teacher'] = 'teacher'
flash("Logged in!")
return redirect(url_for("teacher"))
# if password found, log the user in
found_user = Student.query.filter_by(password=password).first()
if found_user:
# log the student in
print('logging in student')
session['student'] = found_user.name
# flash("Logged in!")
return redirect(url_for("student"))
# if password not found, display an error message
else:
flash("Password not found!")
return redirect(url_for("login"))
else:
# user is not trying to log in -> display the login page
return render_template("login.html")
# route for the register page
# *to be used only by students
@app.route("/signup", methods=["POST", "GET"])
@app.route("/register", methods=["POST", "GET"])
def register():
# first check if the user is logged in
if 'student' in session:
# redirect to the student view
flash("You are already logged in!")
return redirect(url_for("student"))
elif 'teacher' in session:
# redirect to the teacher view
flash("You are already logged in!")
return redirect(url_for("teacher"))
else:
# user is not logged in
# check if the user is trying to register
if request.method == "POST":
session.permanent = True
# user is trying to register
# get the form data
name_in = request.form["name"]
period_in = request.form["period"]
email_in = request.form["email"].lower()
# make sure the email does not already exist in the database
emails_found = Student.query.filter_by(email=email_in).first()
if emails_found:
flash("Email already exists!")
return redirect(url_for("register"))
# create a new student object
new_student = Student(name_in, int(period_in), email_in)
# add the new student to the database
db.session.add(new_student)
db.session.commit()
print('added to database')
# send the new student's password to their email
sendEmail(reciever_email=email_in, user_password=new_student.password, forgot_password=False)
flash("Registered! Your password has been sent to your school email.")
print('redirecting to login page')
return redirect(url_for("login"))
else:
# user is not trying to register
# display the register page
return render_template("register.html")
@app.route("/logout")
def logout():
# first check if the user is logged in
if 'student' in session:
# log the student out
session.pop('student', None)
flash("Logged out!")
return redirect(url_for("login"))
elif 'teacher' in session:
# log the teacher out
session.pop('teacher', None)
flash("Logged out!")
return redirect(url_for("login"))
else:
# user is not logged in
flash("You are not logged in!")
return redirect(url_for("login"))
# route for the student view
@app.route("/student")
@app.route("/s")
def student():
print('student view')
# first check if the student is logged in
if 'student' in session:
# if logged in, display the student's info
found_student = Student.query.filter_by(name=session['student']).first()
return render_template("student.html", student=found_student, random_tail_length=random.randint(1, 10))
else:
# user is not logged in -> login page
flash("You are not logged in!")
return redirect(url_for("login"))
# route for the teacher view
@app.route("/teacher", methods=["POST", "GET"])
@app.route("/t", methods=["POST", "GET"])
def teacher():
# first check if the teacher is logged in
if 'teacher' in session:
# if logged in, first check if the save button was pressed
if request.method == "POST":
users = []
for i in range(len(Student.query.all())):
str_id = "studentid" + str(i)
str_name = "studentname" + str(i)
str_period = "studentperiod" + str(i)
str_email = "studentemail" + str(i)
str_password = "studentpassword" + str(i)
str_rp = "studentrp" + str(i)
id_input = request.form[str_id]
name_input = request.form[str_name]
try:
period_input = int(request.form[str_period])
except:
# someone put an "e" in the period input (we don't know how to prevent them from doing it)
flash(f"Please enter a valid period number for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
# make sure period is a number between 1 and 8, inclusive
if period_input < 1 or period_input > 8:
flash(f"Please enter a valid period number for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
email_input = request.form[str_email]
# first check if the email has been changed/edited
if email_input != Student.query.filter_by(_id=id_input).first().email:
# make sure email is unique
emails_found = Student.query.filter_by(email=email_input).first()
if emails_found and emails_found._id != id_input: # also making sure the matching email is not the same student
flash(f"The email you're trying to change for {name_input} (id: {id_input}) is already taken by {emails_found.name} (id: {emails_found._id})")
return redirect(url_for("teacher"))
# make sure email is valid
# for now, just checking if it's empty
if email_input == "":
flash(f"Please enter a valid email for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
password_input = request.form[str_password]
# LOGIC FOR VALIDATING NEW PASSWORD:
# 1. check if the password has been changed/edited
if password_input != Student.query.filter_by(_id=id_input).first().password:
# 2. make sure password is made up of only numbers
if not password_input.isdigit():
flash(f"Please enter a valid password for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
# 3. remove leading zeroes
password_input = str(int(password_input))
# 4. make sure password is 6 digits or less
if len(password_input) > 6:
flash(f"Please enter a valid password for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
# 4a. if it's less than 6 digits, add leading zeroes
if len(password_input) < 6:
password_input = password_input.zfill(6)
# 5. make sure password is unique (doesn't already belong to another student)
password_found = Student.query.filter_by(password=password_input).first()
if password_found and password_found._id != id_input: # also making sure the matching password is not the same student
flash(f"The password you're trying to change for {name_input} (id: {id_input}) is already taken by {password_found.name} (id: {password_found._id})")
return redirect(url_for("teacher"))
try:
rp_input = int(request.form[str_rp])
except:
# someone put an "e" in the rp input (we don't know how to prevent them from doing it)
flash(f"Please enter a valid number of RP for {name_input} (id: {id_input})")
return redirect(url_for("teacher"))
users.append([id_input, name_input, period_input, email_input, password_input, rp_input])
# update the database
for i in range(len(users)):
# using the id for the filter because that can't be changed
found_user = Student.query.filter_by(_id=users[i][0]).first()
# IF YOU MAKE ALL TABLE CELLS INTO INPUTS, UNCOMMENT THIS
found_user.name = users[i][1]
found_user.period = users[i][2]
found_user.email = users[i][3]
found_user.password = users[i][4]
found_user.rp = users[i][5]
db.session.commit()
flash("Saved!")
return redirect(url_for("teacher"))
# display the table of students
# sort the students by period, then name when you pass it to the html
return render_template("teacher.html", students=Student.query.order_by(Student.period, Student.name).all())
else:
# user is not logged in -> login page
flash("You are not logged in!")
return redirect(url_for("login"))
@app.route("/forgotpassword", methods=["GET", "POST"])
@app.route("/forgot", methods=["GET", "POST"])
def forgotpassword():
if request.method == "POST":
# get the email from the form
email_in = request.form["email"]
# make sure the email exists in the database
user_found = Student.query.filter_by(email=email_in).first()
if user_found:
# send the user an email containing their password
sendEmail(reciever_email=email_in, user_password=user_found.password, forgot_password=True)
flash("An email containing your password has been sent to you!")
return redirect(url_for("login"))
else:
flash("The email you entered is not registered!")
return redirect(url_for("forgotpassword"))
else:
return render_template("forgotpassword.html")
# FOR DEVELOPMENT PURPOSES ONLY, DELETE/COMMENT THESE IN PRODUCTION
# route for clearing the table
@app.route("/clear-table")
def clear_table():
# clear the database
db.drop_all()
return "Table cleared!"
# route for adding a student to the table
@app.route("/add-student")
def add_student():
# create a new student object
new_student = Student('name', 7, '[email protected]')
# add the new student to the database
db.session.add(new_student)
db.session.commit()
return "Student added!"
# route to see the student view without logging in
@app.route("/student-view")
def student_view():
return render_template("student.html", student=Student.query.first())
# run the app
if __name__ == "__main__":
with app.app_context():
print('Creating Database...')
# TODO: Uncomment this line in production
db.drop_all()
db.create_all()
# TODO: Comment this loop in production
for i in range(3):
add_student()
app.run(debug=True)