-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
238 lines (211 loc) · 10 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
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
import string
import mysql.connector.errorcode
from flask import *
import mysql.connector
import logging
app = Flask(__name__)
app.secret_key = 'login'
mydb = mysql.connector.connect(host="localhost", user="Kishore", passwd="Kishore@123",
auth_plugin='mysql_native_password', database="BusBooking")
Cursor = mydb.cursor()
Cursor.execute("Use BusBooking")
Book_Status = {} # to maintain booking status with session and database
check = [] # to maintain the current user booked seats
User_partial = {} # to display the partial booked user
Gen_Check = {} # used to check the gender and show available seats
reserved = []
# Starting route
@app.route('/')
def home():
logging.basicConfig(filename="/home/local/ZOHOCORP/kishore-pt5635/PycharmProjects/Bus_Ticket_booking/Logger.log",
format='%(asctime)s %(message)s', filemode='a', level=logging.DEBUG,
datefmt='%m/%d/%Y %I:%M:%S %p')
return redirect(url_for('Dashboard'))
@app.route('/login', methods=['post', 'get'])
def login():
if 'Mobile_num' in session:
return redirect(url_for('Dashboard'))
if request.method == 'POST':
Mobile_Num = request.form['Mobile_num']
Password = request.form['Pass']
get_data = f"SELECT Password,Gender,Name FROM User WHERE MobileNum = {Mobile_Num}"
Cursor.execute(get_data)
result = Cursor.fetchall()
if len(result):
if result[0][0] == Password:
session['Mobile_num'] = Mobile_Num
session['Gender'] = result[0][1]
session['user'] = result[0][2]
print(session, "Session printed")
return redirect(url_for('Dashboard'))
else:
return render_template('login.html', msg='Authentication Failed, Enter Valid Credentials !!!')
else:
return render_template('login.html', msg='User Does Not Exist')
else:
return render_template('login.html')
# Signup route
@app.route('/Signup', methods=['post', 'get'])
def Signup():
if 'Mobile_num' in session:
return redirect(url_for('Dashboard'))
if request.method == 'POST':
user_name = request.form['user_Name']
User_Age = int(request.form['User_Age'])
Gender = request.form['Gender']
Email = request.form['Email']
Mobile_number = request.form['Mobile_number']
Password = request.form['Password']
C_password = request.form['C_password']
upper_case = any([1 if c in string.ascii_uppercase else 0 for c in Password])
lower_case = any([1 if c in string.ascii_lowercase else 0 for c in Password])
Special = any([1 if c in string.punctuation else 0 for c in Password])
Digits = any([1 if c in string.digits else 0 for c in Password])
if not upper_case or not lower_case or not Special or not Digits or not (8 <= len(Password) <= 12):
return render_template('Signup.html',
msg='Password Must Contain 8 to 12 Characters, one Upper, one Lower, one Special character and one Digit')
if Password != C_password:
return render_template('Signup.html', msg='Entered Password and Confirm-password are not Same')
if Gender == '0':
return render_template('Signup.html', msg='Gender Not Selected')
if len(Mobile_number) != 10:
return render_template('Signup.html', msg='Enter Valid Mobile Number!!!')
else:
data = (user_name, User_Age, Gender, Email, Mobile_number, Password)
add_Data = '''INSERT INTO User (Name, Age, Gender, Email, MobileNum, Password) VALUES ('%s', %d, '%s', '%s', '%s', '%s');''' % data
Cursor.execute(add_Data)
mydb.commit()
return redirect(url_for('login'))
else:
return render_template('Signup.html')
@app.route('/Logout')
def Logout():
session.clear()
return redirect(url_for('login'))
@app.route('/SearchBus', methods=['post', 'get'])
def SearchBus():
if request.method == 'POST' and "Mobile_num" in session:
route_from = request.form['From']
route_to = request.form['To']
if route_from == '' or route_to == '':
return redirect(url_for('Dashboard'))
else:
user = session['user']
return render_template('index.html', From=route_from, To=route_to, msg='BusSearched', user_name=user)
else:
return redirect(url_for('login'))
@app.route('/ViewSeats', methods=['post', 'get'])
def ViewSeats():
if request.method == 'POST' and "Mobile_num" in session:
Cursor.execute(
'UPDATE BookStatus SET PassengerName = DEFAULT(PassengerName), PassengerAge = DEFAULT(PassengerAge), PassengerGender = DEFAULT(PassengerGender), BookingStatus = DEFAULT(BookingStatus), TimeStamp = DEFAULT(TimeStamp) WHERE TimeStamp < (NOW() - INTERVAL 2 MINUTE) AND BookingStatus = "Partial"')
mydb.commit()
Data()
check.clear()
reserved.clear()
return render_template('Booking.html', Book_Status=Book_Status, length=len(check), Gen_Check=Gen_Check,
Gender=session["Gender"])
else:
return redirect(url_for('login'))
@app.route('/Button', methods=['post', 'get'])
def Button():
if request.method == 'POST' and 'Mobile_num' in session:
Btn = int(request.form['btn'])
Cursor.execute(f"SELECT SeatNo, No FROM BookStatus WHERE No = {Btn}")
Result = Cursor.fetchall()
if User_partial[Btn] == 'Partial':
return render_template('Booking.html', Book_Status=Book_Status, length=len(check), Gen_Check=Gen_Check,
Gender=session["Gender"])
if Book_Status[Btn] == 'unBooked':
if len(check) > 5:
return render_template('Booking.html', Book_Status=Book_Status,
msg='The maximum number of seats that can be selected is 6', length=len(check),
Gen_Check=Gen_Check, Gender=session["Gender"])
check.append(Result[0][0])
reserved.append(Result[0][1])
Book_Status[Btn] = 'Partial'
elif Book_Status[Btn] == 'Partial':
check.remove(Result[0][0])
reserved.append(Result[0][1])
Book_Status[Btn] = 'unBooked'
print(reserved)
return render_template('Booking.html', Book_Status=Book_Status, length=len(check), Gen_Check=Gen_Check,
Gender=session["Gender"])
else:
return redirect(url_for('login'))
@app.route('/Block', methods=['post', 'get'])
def Block():
if request.method == 'POST' and 'Mobile_num' in session:
user = session["Mobile_num"]
get_data = f"SELECT Name,Age,Gender FROM User WHERE MobileNum = {user}"
Cursor.execute(get_data)
Result = Cursor.fetchall()
length = len(check)
for i in range(0, length):
data = (Result[0][0], int(Result[0][1]), Result[0][2], check[i])
User_partial[check[i]] = 'Partial'
add_Data = ''' UPDATE BookStatus SET PassengerName = '%s', PassengerAge = %d, PassengerGender = '%s', BookingStatus = 'Partial', TimeStamp = now() WHERE SeatNo = '%s';''' % data
Cursor.execute(add_Data)
mydb.commit()
return render_template('PassengerDetails.html', check=check, length=len(check), Gen_Check=Gen_Check, reserved=reserved)
else:
return redirect(url_for('login'))
@app.route('/Confirm', methods=['post', 'get'])
def Confirm():
if request.method == 'POST' and 'Mobile_num' in session:
length = len(check)
for i in range(1, length + 1):
Book_Status[check[i - 1]] = 'Booked'
data = (request.form['N' + str(i)], int(request.form['A' + str(i)]), request.form['G' + str(i)],
request.form['G' + str(i)], check[i - 1])
add_Data = '''UPDATE BookStatus SET PassengerName = '%s', PassengerAge = %d, PassengerGender = '%s', BookingStatus = 'Booked', Gen = '%s' WHERE SeatNo = '%s';''' % data
Cursor.execute(add_Data)
mydb.commit()
for i in check:
get_data = "SELECT * FROM BookStatus WHERE SeatNo = '%s'" % i
Cursor.execute(get_data)
Result = Cursor.fetchall()
if Result[0][7] == 'Female':
if (1 <= Result[0][6] <= 6) or (19 <= Result[0][6] <= 24):
val = Result[0][6] + 6
add_Data = ''' UPDATE BookStatus SET Gen = 'Reserved' WHERE No = %d AND Gen = 'Nothing';''' % val
Cursor.execute(add_Data)
mydb.commit()
elif (7 <= Result[0][6] <= 12) or (25 <= Result[0][6] <= 30):
val = Result[0][6] - 6
add_Data = ''' UPDATE BookStatus SET Gen = 'Reserved' WHERE No = %d AND Gen = 'Nothing';''' % val
Cursor.execute(add_Data)
mydb.commit()
user = session['user']
return render_template('index.html', msg="Bus Booked Successfully", user_name=user)
else:
return redirect(url_for('login'))
@app.route('/Dashboard', methods=['post', 'get'])
def Dashboard():
if 'Mobile_num' in session:
user = session['user']
return render_template('index.html', user_name=user)
else:
return redirect(url_for('login'))
@app.route('/ADMIN', methods=['post', 'get'])
def ADMIN():
if request.method == 'POST':
admin_user = request.form['admin_user']
admin_pass = request.form['admin_pass']
if admin_user == 'Kishore' and admin_pass == 'Kishore':
return render_template('admin.html')
else:
return redirect(url_for('ADMIN'))
else:
return render_template('Admin_login.html')
# data from database
def Data():
Cursor.execute("select * from BookStatus")
data = Cursor.fetchall()
for row in data:
Book_Status[row[6]] = row[4]
User_partial[row[6]] = row[4]
Gen_Check[row[6]] = row[7]
if __name__ == '__main__':
Data()
app.run(debug=True, port=8000)