-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
269 lines (187 loc) · 9.29 KB
/
application.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
import os
from flask import Flask, render_template, session, request, flash
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
#@app.after_request
#def after_request(response):
# response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
# return response
@app.route("/")
def index():
#flights = db.execute("SELECT * FROM login_signup").fetchall()
session['login'] = None
#message = None
return render_template("homepage.html")
@app.route("/registration_signup", methods = ["POST", "GET"])
def registration_login():
first_name = request.form.get('f_name')
last_name = request.form.get('l_name')
j_num = request.form.get('j_num')
email = request.form.get('email')
password = request.form.get('psw')
re_password = request.form.get('re_psw')
message = None
if request.method == "POST":
#check if the password is valid and the length of j_number is equal to 9
if len(password) < 5 or len(password) > 30 or len(j_num) != 9:
message = "Invalid password or J#"
return render_template("signup.html", message=message)
#check if the password is equal to the re-written password
elif password != re_password:
message = "Please, make sure your passwords are same!"
return render_template("signup.html", message=message)
elif "@" not in email or ".com" not in email:
message = "Invalid address! Pleae enter the correct email."
return render_template("signup.html", message=message)
else:
if db.execute("SELECT j_num FROM login_signup WHERE j_num=:j_num",{'j_num': j_num}).rowcount == 0:
db.execute('INSERT INTO login_signup (first_name, last_name, j_num, email, password) VALUES(:first_name, :last_name,\
:j_num, :email, :password)',{"first_name":first_name, "last_name":last_name, "j_num":j_num, "email":email, "password":password})
db.commit()
session['login'] = True
message = "Congratulations %s , Account successfully created!!!" % (first_name)
return render_template("registered.html", message=message)
else:
message = "The account already exists!!! Please login!"
return render_template("signup.html", message=message)
else:
return render_template("signup.html")
@app.route("/registration_signup_tutor", methods = ["POST", "GET"])
def registration_login_tutor():
first_name = request.form.get('f_name')
last_name = request.form.get('l_name')
j_num = request.form.get('j_num')
email = request.form.get('email')
secretcode = request.form.get('secretcode')
uniquecode = request.form.get('uniquecode')
password = request.form.get('psw')
re_password = request.form.get('re_psw')
message = None
if request.method == "POST":
#check if the password is valid and the length of j_number is equal to 9
if len(password) < 5 or len(password) > 30 or len(j_num) != 9:
message = "Invalid password or J#"
return render_template("signUpTutor.html", message=message)
#check if the password is equal to the re-written password
elif password != re_password:
message = "Please, make sure your passwords are same!"
return render_template("signUpTutor.html", message=message)
elif "@" not in email or ".com" not in email:
message = "Invalid address! Pleae enter the correct email."
return render_template("signUpTutor.html", message=message)
else:
if db.execute("SELECT j_num FROM login_signup WHERE j_num=:j_num",{'j_num': j_num}).rowcount == 0 and \
db.execute("SELECT secretcode, uniquecode FROM student_code JOIN login_signup_tutor ON student_code.uniquecode = login_signup_tutor.secretcode", {'uniquecode':uniquecode, 'secretcode':secretcode}).rowcount == 1:
db.execute('INSERT INTO login_signup_tutor (first_name, last_name, j_num, secretcode, email, password) VALUES(:first_name, :last_name,\
:j_num, :secretcode, :email, :password)',{"first_name":first_name, "last_name":last_name, "j_num":j_num, "secretcode":secretcode, "email":email, "password":password})
db.commit()
session['login'] = True
message = "Congratulations %s , Account successfully created!!!" % (first_name)
return render_template("registered.html", message=message)
else:
message = "The account already exists or invalid secret code. Make sure your code is right."
return render_template("signUpTutor.html", message=message)
else:
return render_template("signUpTutor.html")
#This method can be useful when we don't use the "POST" and "GET" method that is mentioned below.
"""@app.route("/login", methods = ["POST", "GET"])
def login():
return render_template("login.html")"""
@app.route("/login_submit", methods = ["POST", "GET"])
def login_submit():
message = None
if request.method == "POST":
l_email = request.form.get('email')
l_password = request.form.get('password')
if db.execute("SELECT * FROM login_signup WHERE email=:email and password=:password", {'email':l_email, 'password':l_password}).fetchone():
username = db.execute("SELECT first_name FROM login_signup WHERE email=:email", {'email':l_email}).fetchone()
message = "Welcome %s" % (username)
session['login'] = True
return render_template("dashboardOne.html", message=message)
else:
message = "Incorrect Password or the the username doesn't exist."
return render_template("login.html", message=message)
else:
return render_template("login.html")
@app.route("/admin_login", methods = ["POST", "GET"])
def admin_login():
message = "This page is used by admins only. ADMINS ONLY!!!"
if request.method == "POST":
first_name = request.form.get('first_name')
j_num = request.form.get('login_id')
password = request.form.get('password')
if db.execute("SELECT * FROM admin WHERE first_name=:first_name and j_num=:j_num and password=:password", {'first_name':first_name, 'j_num':j_num, 'password':password}).fetchone():
message = "Welcome Adminstrator!!!"
session['login'] = True
return render_template("adminDashboard.html", message=message)
else:
message = "Incorrect Password or the the username doesn't exist."
return render_template("adminLanding.html", message=message)
else:
return render_template("adminLanding.html", message=message)
#
#db.execute('INSERT INTO login_signup (first_name, last_name, j_num, email, password) VALUES(:first_name, :last_name,\
# :j_num, :email, :password)',{"first_name":first_name, "last_name":last_name, "j_num":j_num, "email":email, "password":password})
@app.route("/view_credentials", methods = ["POST", "GET"])
def view_credentials():
if request.method == "GET":
# if request.form['submit_button'] == 'View Admin':
# output = db.execute('SELECT * FROM admin').fetchall()
# return render_template("adminDashboard.html", output=output)
# elif request.form['submit_button'] == 'View Tutor':
tutors = db.execute('SELECT first_name, last_name, j_num, email FROM login_signup_tutor').fetchall()
admins = db.execute('SELECT first_name, j_num FROM admin').fetchall()
return render_template("adminDashboard.html", tutors=tutors, admins=admins)
@app.route("/add_user", methods=["POST", "GET"])
def add_user():
#return render_template("addDeleteUser.html"
if request.method == "POST":
first_name = request.form.get('f_name')
last_name = request.form.get('l_name')
j_num = request.form.get('j_num')
uniquecode = request.form.get('secretcode')
if db.execute("SELECT j_num FROM student_code WHERE j_num=:j_num",{'j_num': j_num}).rowcount == 0 and db.execute("SELECT uniquecode FROM student_code WHERE uniquecode=:uniquecode",{'uniquecode': uniquecode}).rowcount == 0:
db.execute('INSERT INTO student_code (first_name, last_name, j_num, uniquecode) VALUES(:first_name, :last_name,\
:j_num, :uniquecode)',{"first_name":first_name, "last_name":last_name, "j_num":j_num, "uniquecode":uniquecode})
db.commit()
message = "Successfully added a new user!!!"
return render_template("success.html", message=message)
else:
return render_template("addDeleteUser.html")
@app.route("/delete_user", methods=["POST", "GET"])
def delete_user():
if request.method == "POST":
j_num = request.form.get('j_num')
if db.execute("SELECT j_num FROM student_code WHERE j_num=:j_num",{'j_num': j_num}).rowcount == 1:
db.execute("DELETE FROM student_code WHERE j_num=:j_num", {'j_num':j_num})
db.execute("DELETE FROM login_signup_tutor WHERE j_num=:j_num", {'j_num':j_num})
db.commit()
message = "Successfully deleted an existing tutor!!!"
return render_template("success.html", message=message)
else:
return render_template("addDeleteUser.html")
@app.route("/success")
def success_message():
message = "Congratulations, you successfully created an account!!!"
return render_template("registered.html", message = message)
@app.route("/contact")
def contact_us():
return render_template("contactus.html")
@app.route("/logout")
def logout():
message = "You have been successfully logged out!"
session['login'] = False
flash("You are logged out.")
return render_template("homepage.html", message=message)