-
Notifications
You must be signed in to change notification settings - Fork 0
/
reservation_system.py
278 lines (229 loc) · 9.89 KB
/
reservation_system.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
from flask import Flask, render_template, request, redirect, url_for, session, flash
import hashlib
import re
import pymysql
import email.utils
import datetime
import os
from user import setUser, login_required, admin_required
from db import Database
app = Flask(__name__, template_folder='templates', static_folder='resources')
app.secret_key = os.urandom(24)
db = Database()
def getMD5(pwd):
pwd = pwd.encode('ascii') # ascii utf-8
result = hashlib.md5(pwd).hexdigest()
return result
def is_valid_email(email_str):
if not re.match(r"[^@]+@[^@]+\.[^@]+", email_str):
return False
email_address = email.utils.parseaddr(email_str)[1]
if not email.utils.formataddr((email_address, email_address)):
return False
return True
@app.route('/')
def login():
return render_template('login.html')
@ app.route('/adminlogin')
def adminlogin():
return render_template('adminLogin.html')
@ app.route('/adminlogin_validation', methods=['POST'])
def adminlogin_validation():
username = request.form.get('username')
password = getMD5(request.form.get('password'))
param = (username, password)
sql = "SELECT * FROM adminaccount WHERE username LIKE %s AND password LIKE %s"
db.cur.execute(sql, param)
users = db.cur.fetchall()
if len(users) == 0:
errorMsg = "Incorrect Username or Password"
return render_template('adminLogin.html', errorMsg=errorMsg)
else:
now = datetime.datetime.now()
timenowSql = now.strftime('%Y-%m-%d %H:%M:%S')
user = users[0]
firstname, lastname, username = user['firstname'], user['lastname'], user['username']
setUser(user, 'admin')
return redirect(url_for('adminhome', firstname=firstname, lastname=lastname, username=username, timenowSql=timenowSql))
@ app.route('/userlogin_validation', methods=['POST'])
def userlogin_validation():
username = request.form.get('username')
password = getMD5(request.form.get('password'))
param = (username, password)
sql = "SELECT * FROM useraccount WHERE username LIKE %s AND password LIKE %s"
db.cur.execute(sql, param)
users = db.cur.fetchall()
if len(users) == 0:
errorMsg = "Incorrect Username or Password"
return render_template('login.html', errorMsg=errorMsg)
else:
user = users[0]
setUser(user, 'student')
return redirect(url_for('userhome'))
@ app.route('/userregistration')
def userregister():
return render_template('userRegister.html')
@ app.route('/add_user', methods=['POST'])
def adduser():
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
username = request.form.get('username')
password = getMD5(request.form.get('password'))
email = request.form.get('email')
status = request.form.get('status')
emailCheck = is_valid_email(email)
param = (firstname, lastname, username, password, email, status)
sql = "INSERT INTO `useraccount` (`firstname`,`lastname`,`username`,`password`,`email`,`status`) VALUES (%s,%s,%s,%s,%s,%s)"
if emailCheck == True:
try:
db.cur.execute(sql, param)
db.cur.commit()
except pymysql.connector.Error as error:
errorMsg = error
return render_template('userRegister.html', errorMsg=errorMsg)
else:
okMsg = "User Registration was Successfull"
return render_template('userRegister.html', okMsg=okMsg)
else:
errorMsg = "Incorrect Email"
return render_template('userRegister.html', errorMsg=errorMsg)
@ app.route('/adminregistration')
def adminregister():
return render_template('adminRegister.html')
@ app.route('/add_admin', methods=['POST'])
def addadmin():
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
username = request.form.get('username')
password = getMD5(request.form.get('password'))
email = request.form.get('email')
emailCheck = is_valid_email(email)
param = (firstname, lastname, username, password, email,)
sql = "INSERT INTO `adminaccount` (`firstname`,`lastname`,`username`,`password`,`email`) VALUES (%s,%s,%s,%s,%s)"
if emailCheck == True:
try:
db.cur.execute(sql, param)
db.cur.commit()
except pymysql.connector.Error as error:
errorMsg = error
return render_template('adminRegister.html', errorMsg=errorMsg)
else:
okMsg = "Admin Registration was Successfull"
return render_template('adminRegister.html', okMsg=okMsg)
else:
errorMsg = "Incorrect Email"
return render_template('adminRegister.html', errorMsg=errorMsg)
db.cur.connect()
@ app.route('/userhome')
@login_required
def userhome(user):
firstname, lastname, username = user['firstname'], user['lastname'], user['username']
_, reservations = db.get_reservations(username)
return render_template('userHome.html', firstname=firstname, lastname=lastname, username=username, reservations=reservations)
@ app.route('/adminhome')
@ admin_required
def adminhome(user):
if 'username' in session:
firstname, lastname, username = user['firstname'], user['lastname'], user['username']
_, content = db.get_all_reservations()
return render_template('adminHome.html', firstname=firstname, lastname=lastname, username=username, content=content)
else:
return redirect(url_for('adminlogin'))
@ app.route('/adminlogout')
def adminlogout():
session.clear()
return redirect(url_for('adminlogin'))
@ app.route('/userlogout')
def userlogout():
session.clear()
return redirect(url_for('login'))
@ app.route('/reservation', methods=['GET', 'POST'])
@login_required
def reservation(user):
success, rooms = db.get_all_rooms()
if not success:
return render_template('reservation.html', errorMsg=rooms)
reservation_id = request.args.get('id')
if request.method == 'POST':
data = request.form
date = datetime.datetime.strptime(data['date'], '%Y-%m-%d').date()
roomNo = data['room-number']
startTime = datetime.datetime.strptime(
data['start-time'], '%H:%M').time()
endTime = datetime.datetime.strptime(data['end-time'], '%H:%M').time()
occupancy = data['occupancy']
if reservation_id:
success, data = db.modify_reservation(
reservation_id, roomNo, startTime, endTime, date, occupancy, user['username'])
else:
success, data = db.make_reservation(
roomNo, startTime, endTime, date, occupancy, user['username'])
if success:
return redirect(url_for('userhome'))
else:
return render_template('reservation.html', reservation=None, rooms=rooms, errorMsg=data)
if reservation_id:
success, data = db.get_reservation(reservation_id)
if not success:
return redirect(url_for('userhome'))
data['start_time'] = str(data['start_time'])[:5]
data['end_time'] = str(data['end_time'])[:5]
return render_template('reservation.html', reservation=data, rooms=rooms)
return render_template('reservation.html', reservation=None, rooms=rooms)
# route for admin add reservation for a selected user & modify reservation
@ app.route('/adreservation', methods=['GET', 'POST'])
@ admin_required
def adreservation(user):
success, rooms = db.get_all_rooms()
if not success:
return render_template('adreservation.html', errorMsg=rooms)
reservation_id = request.args.get('id')
if request.method == 'POST':
data = request.form
date = datetime.datetime.strptime(data['date'], '%Y-%m-%d').date()
roomNo = data['room-number']
startTime = datetime.datetime.strptime(
data['start-time'], '%H:%M').time()
endTime = datetime.datetime.strptime(data['end-time'], '%H:%M').time()
occupancy = data['occupancy']
username = data['username']
if reservation_id:
success, data = db.admin_modify_reservation(
reservation_id, roomNo, startTime, endTime, date, occupancy, username)
else:
success, data = db.make_reservation(
roomNo, startTime, endTime, date, occupancy, username)
if success:
return redirect(url_for('adminhome'))
else:
return render_template('adreservation.html', reservation=None, rooms=rooms, errorMsg=data)
success, users = db.get_all_users()
if not success:
return render_template('adreservation.html', errorMsg=users)
if reservation_id:
success, data = db.get_reservation(reservation_id)
if not success:
return redirect(url_for('adminhome'))
data['start_time'] = str(data['start_time'])[:5]
data['end_time'] = str(data['end_time'])[:5]
return render_template('adreservation.html', reservation=data, rooms=rooms, users=users)
return render_template('adreservation.html', reservation=None, rooms=rooms, users=users)
@ app.route('/cancel/<int:reservation_id>', methods=['GET'])
@login_required
def cancel(user, reservation_id):
success, data = db.cancel_reservation(reservation_id, user['username'])
if success:
return redirect(url_for('userhome'))
else:
flash(data)
return redirect(url_for('userhome'))
# route for admin to cancel reservation
@ app.route('/adcancel/<int:reservation_id>', methods=['GET'])
@ admin_required
def adcancel(user, reservation_id):
success, data = db.admin_cancel_reservation(reservation_id)
if success:
return redirect(url_for('adminhome'))
else:
flash(data)
return redirect(url_for('adminhome'))