-
Notifications
You must be signed in to change notification settings - Fork 1
/
vehicle.py
696 lines (606 loc) · 26.8 KB
/
vehicle.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import re
from flask import redirect, render_template, session, url_for, Blueprint, flash, request
from datetime import date, datetime, timedelta
import bookingslot
from db import db, cursor
import mysql.connector
from auth import login_required
from utils import requires_role
from flask_paginate import Pagination
from utils import requires_role
import csv
Vehicle = Blueprint('vehicle', __name__)
@Vehicle.route('/vehicle')
@login_required
@requires_role('admin')
def display():
page = request.args.get('page', type=int, default=1)
per_page = 20
if page < 1:
flash('Page does not exist')
return redirect(url_for('vehicle.display', page=1))
offset = (page - 1) * per_page
update_query = '''
UPDATE vehicle v
JOIN bookingslot b ON v.VehicleID = b.BSlotID
SET v.VehicleType = ' ', v.VehicleNumber=' '
WHERE b.TimeFrom is '' and b.TimeTo is ''
'''
try:
cursor.execute(update_query)
db.commit()
except mysql.connector.Error as e:
db.rollback()
flash('Error updating data to null', 'danger')
cursor.execute('SELECT VehicleID, VehicleType, VehicleNumber FROM vehicle limit %s offset %s;', (per_page, offset))
data = cursor.fetchall()
db.commit()
Null_query = '''
SELECT v.VehicleID
FROM vehicle v
JOIN bookingslot b on v.VehicleID = b.BSlotID
WHERE b.TimeFrom = ' ' and b.TimeTo = ' '
'''
cursor.execute(Null_query)
db.commit()
NullID = cursor.fetchone()
has_next = len(data) == per_page
pagination = Pagination(page=page, per_page=per_page)
return render_template('view/vehicle.html', data=data, pagination=pagination, has_next=has_next, page=page,
per_page=per_page, NullID=NullID)
def ValidNumber(VehicleNumber):
pattern = r"^[A-Z]{2}[ -]?\d{2}[ -]?[A-Z]{1,2}[ -]?\d{1,4}$"
return re.search(pattern, VehicleNumber)
@Vehicle.route('/vehicle/add', methods=['POST', 'GET'])
@login_required
def add_data():
with open("static/DataSet.csv") as f:
lines = list(csv.reader(f))
first_elements = [row[0] for row in lines if row]
data = []
username = session.get('username')
try:
SNo = 'SELECT SNo FROM user WHERE username=%s'
cursor.execute(SNo,(username,))
db.commit()
SNo = cursor.fetchone()
S_No = SNo[0]
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('An error occurred. Please try again later','error')
# return redirect(url_for('vehicle.add_data', SNo=SNo))
try:
VID = 'SELECT max(VehicleID)+1 as VID FROM Vehicle'
cursor.execute(VID)
maxVID = cursor.fetchone()[0]
db.commit()
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('An error occured. Please try again','error')
return redirect(url_for('vehicle.add_data', SNo = S_No))
VehicleID = maxVID
if request.method == 'POST':
if not username:
print('username not found')
flash('No username','error')
return redirect(url_for('vehicle.add_data'))
if not VID:
flash('Cannot continue without vehicleID', 'danger')
return redirect(url_for('vehicle.add_data', SNo=S_No))
session['VehicleID'] = VehicleID
VehicleType = request.form.get('VehicleType')
VehicleNumber = request.form.get('VehicleNumber')
VehicleName = request.form.get('VehicleName')
check_number_query = 'SELECT VehicleNumber FROM vehicle'
cursor.execute(check_number_query)
db.commit()
existing_number = cursor.fetchall()
if VehicleNumber in existing_number:
flash(f'This vehicle is already saved. Please select this vehicle','error')
return redirect(url_for('vehicle.ChooseVehicle'))
if not S_No:
flash('An error occurred. Please try again after sometime', 'error')
return redirect(url_for('vehicle.add_data', SNo=S_No))
if not ValidNumber(VehicleNumber):
flash('Registration Number is not valid')
return redirect(url_for('vehicle.add_data', SNo=S_No))
try:
insert_query = '''
INSERT INTO vehicle (VehicleID, VehicleType, VehicleNumber, VehicleName, SNo) VALUES (%s, %s, %s, %s, %s)
'''
cursor.execute(insert_query, (VehicleID, VehicleType, VehicleNumber, VehicleName, S_No,))
db.commit()
print('')
except mysql.connector.Error as e:
db.rollback()
flash('Error adding data', 'error')
return redirect(url_for('vehicle.add_data', SNo=S_No))
return redirect(url_for('bookingslot.add_data', VehicleID=VehicleID, S_No=S_No))
return render_template('add/vehicle.html', SNo=S_No, data=data, first_elements=first_elements)
@Vehicle.route('/vehicle/admin/add', methods=['GET','POST'])
@login_required
@requires_role('admin')
def AdminVehicle():
if request.method == 'POST':
VehicleID = request.form['VehicleID']
VehicleType = request.form['VehicleType']
VehicleNumber = request.form['VehicleNumber']
name = request.form['name']
address = request.form['address']
contact = request.form['contact']
date = request.form['date']
TimeFrom = request.form['TimeFrom']
duration = request.form['duration']
mode = request.form['mode']
try:
durationStr = int(duration)
except ValueError as ve:
flash('duration must be a valid number', 'error')
return redirect(url_for('bookingslot.AdminVehicle'))
if not VehicleID:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not VehicleType:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not VehicleNumber:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not name:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not address:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not contact:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not date:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not TimeFrom:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not duration:
flash('An issue occurred. Please try after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
rate = 0
if VehicleType == 'car':
rate = 13
if VehicleType == '2-Wheeler':
rate = 8
if VehicleType == 'Heavy-Vehicle':
rate = 15
if VehicleType == 'Luxury-Vehicle':
rate = 18
TotalPrice = rate * durationStr
if not mode:
flash('An issue occurred. Please try again after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
if not TotalPrice:
flash('An issue occurred. Please try again after sometime','error')
return redirect(url_for('vehicle.AdminVehicle'))
print(VehicleID)
cursor.execute('SELECT SNO FROM Vehicle WHERE VehicleID=VehicleID')
db.commit()
S_No = cursor.fetchone()
SNo = S_No[0]
if not S_No:
return "Server Error"
if not SNo:
return "Server Error"
add_query = '''
UPDATE admin a set SNo = SNo
'''
cursor.execute(add_query)
db.commit()
TimeFormat = '%H:%M'
timeFrom_dt = datetime.strptime(TimeFrom, TimeFormat)
timeTo_dt = timeFrom_dt + timedelta(hours=durationStr)
TimeTo = timeTo_dt.strftime(TimeFormat)
try:
update_query = '''
UPDATE vehicle v
INNER JOIN bookingslot b ON v.VehicleID = b.BSlotID
INNER JOIN payment p ON v.VehicleID = p.PaymentID
INNER JOIN admin a ON a.SNo = v.SNo
SET v.VehicleType=%s, v.VehicleNumber=%s,
b.date=%s, b.TimeFrom=%s, b.TimeTo=%s, b.duration=%s,
p.mode=%s, p.TotalPrice=%s
WHERE VehicleID=%s
'''
cursor.execute(update_query, (VehicleType, VehicleNumber, date, TimeFrom, TimeTo, duration, mode, TotalPrice, VehicleID))
db.commit()
except mysql.connector.Error as e:
flash('An error occurred. Please try after sometime','error')
db.rollback()
return redirect(url_for('vehicle.AdminVehicle'))
try:
insert_query = '''
INSERT INTO owner(name, address, contact) values (%s, %s, %s)
'''
cursor.execute(insert_query, (name,address,contact,))
db.commit()
except mysql.connector.Error as e:
flash('An error occurred. Please try again later','error')
return redirect(url_for('vehicle.AdminVehicle'))
PaymentID = VehicleID
return redirect(url_for('payment.Generate_Receipt', PaymentID=PaymentID,duration=duration, TotalPrice=TotalPrice))
cursor.execute("SELECT VehicleID,SNo FROM vehicle WHERE VehicleType='' and VehicleNumber='' ")
db.commit()
availableSlots = cursor.fetchall()
VID = [slot[0] for slot in availableSlots]
if not availableSlots:
flash('No slots are available currently. Please try again later','error')
return redirect(url_for('auth.dashboard'))
try:
fetch_query = '''
SELECT v.VehicleID, v.VehicleType, v.VehicleNumber,
b.Date, b.TimeFrom, b.TimeTo, b.duration,
a.SNo, a.username,
o.name, o.contact,
p.TotalPrice, p.mode
FROM vehicle v
JOIN bookingslot b ON v.VehicleID = b.BSlotID
JOIN admin a ON v.SNo = a.SNo
JOIN owner o ON v.SNo = o.SNo
JOIN payment p ON b.BSlotID = p.PaymentID
ORDER BY b.Date DESC,
b.TimeFrom DESC LIMIT 1
'''
cursor.execute(fetch_query)
data = cursor.fetchone()
if data is None:
flash('No data found', 'error')
return redirect(url_for('vehicle.AdminVehicle'))
VehicleID = data[0]
VehicleType = data[1]
VehicleNumber = data[2]
date = data[3]
TimeFrom = data[4]
TimeTo = data[5]
duration = data[6]
SNo = data[7]
username = data[8]
name = data[9]
contact = data[10]
TotalPrice = data[11]
mode = data[12]
cursor.execute("SELECT SNo FROM vehicle WHERE VehicleID=%s")
db.commit()
SNo = cursor.fetchone()
S_No = SNo[0]
except mysql.connector.Error as e:
print(e)
flash('Error processing your payment', 'error')
db.rollback()
return redirect(url_for('auth.MyBookings'))
try:
insert_query = '''
INSERT INTO allotment(VehicleID, SNo, username, date, TimeFrom, TimeTo, duration, name, contact, TotalPrice, mode, VehicleType, VehicleNumber)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '''
# cursor.execute(insert_query, (VehicleID, username, date, TimeFrom, TimeTo, Duration, name, contact, TotalPrice, mode, VehicleType, VehicleNumber))
cursor.execute(insert_query, (VehicleID, S_No, username, date, TimeFrom, TimeTo, Duration, name, contact, TotalPrice, mode, VehicleType, VehicleNumber))
db.commit()
if(TotalPrice==0):
db.rollback()
return "Server error"
except mysql.connector.Error as e:
print(e)
flash('Error adding allotment', 'error')
return redirect(url_for('vehicle.AdminVehicle'))
return redirect(url_for('payment.Generate_Receipt',duration=duration, TotalPrice=TotalPrice, mode=mode, PaymentID=PaymentID))
return render_template('add/adminVehicleSlot.html', VID=VID)
@Vehicle.route('/user/vehicles', methods=['GET','POST'])
@login_required
# @requires_role('user')≥÷1
def ChooseVehicle():
username = session.get('username')
VehicleID = session.get('VehicleID')
try:
fetchSNo = '''
SELECT v.SNo FROM vehicle v
INNER JOIN user u ON u.SNo = v.SNo
WHERE u.username=%s
'''
cursor.execute(fetchSNo, (username,))
db.commit()
VehicleSNo = cursor.fetchone()
if VehicleSNo is None:
flash('No vehicles found. Please add a vehicle', 'success')
return redirect(url_for('vehicle.add_data'))
SNo = VehicleSNo[0]
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('Server returns null response. Please try again later', 'error')
return redirect(url_for('vehicle.add_data'))
try:
fetch_query = 'SELECT * FROM vehicle v INNER JOIN user u on u.SNo=v.SNo WHERE u.SNo=%s AND u.username=%s'
cursor.execute(fetch_query, (SNo, username,))
db.commit()
vehicles = cursor.fetchall()
vehicleList = [[vehicle[0], vehicle[1], vehicle[2], vehicle[3], vehicle[4]] for vehicle in vehicles]
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('Server returned a null response. Please try again later','error')
return redirect(url_for('index'))
if request.method == 'POST':
SelectedID = request.form.get('VehicleID')
if SelectedID:
flash('Vehicle selected successfully','success')
return redirect(url_for('bookingslot.add_data', VehicleID=SelectedID, SNo=SNo))
else:
flash('please select a vehicle to continue','error')
return redirect(url_for('vehicle.ChooseVehicle'))
if not vehicleList:
flash('No vehicles found. Please add a vehicle', 'error')
return redirect(url_for('vehicle.add_data', SNo=SNo))
# return redirect(url_for('vehicle.ChooseVehicle', SNo=SNo, VehicleType=VehicleType, VehicleNumber=VehicleNumber, VehicleName=VehicleName))
return render_template('view/ChooseVehicle.html', VehicleType=VehicleType, VehicleNumber=VehicleNumber, VehicleName=VehicleName)
if not vehicleList:
flash('No vehicles found. Please add a vehicle ', 'error')
return redirect(url_for('vehicle.add_data', SNo=SNo))
return render_template('view/ChooseVehicle.html', vehicles = vehicleList)
@Vehicle.route('/vehicle/bookingslot/add/<int:VehicleID>', methods=['GET', 'POST'])
@login_required
def bookingslot(VehicleID):
return render_template('add/bookingslot.html', VehicleID=VehicleID, SNo=SNo)
@Vehicle.route('/user/vehicle/edit/<int:VehicleID>', methods=['GET','POST'])
@login_required
def edit_vehicle(VehicleID):
if request.method == 'POST':
VehicleType = request.form['VehicleType']
VehicleNumber = request.form['VehicleNumber']
VehicleName = request.form['VehicleName']
try:
update_query = '''
UPDATE vehicle
SET VehicleType=%s, VehicleNumber=%s, VehicleName=%s
WHERE VehicleID=%s
'''
cursor.execute(update_query, (VehicleType, VehicleNumber, VehicleName, VehicleID))
db.commit()
flash('Data updated successfully', 'success')
return redirect(url_for('vehicle.ChooseVehicle'))
except mysql.connector.Error as e:
db.rollback()
flash('An error occurred. Please try again later', 'error')
return redirect(url_for('vehicle.ChooseVehicle'))
fetch_query = 'SELECT VehicleID, VehicleType, VehicleNumber, VehicleName from vehicle where VehicleID=%s'
cursor.execute(fetch_query, (VehicleID,))
db.commit()
data = cursor.fetchone()
print('data in vehicle is: ', data)
if data is None:
flash('No data found')
return redirect(url_for('vehicle.display'))
return render_template('edit/ChooseVehicle.html', data=data)
@Vehicle.route('/user/vehicle/Add',methods=['GET','POST'])
@login_required
def add_vehicle():
with open("static/DataSet.csv") as f:
lines = list(csv.reader(f))
first_elements = [row[0] for row in lines if row]
print('first_elements: ', first_elements)
data = []
username = session.get('username')
try:
SNo = 'SELECT SNo FROM user WHERE username=%s'
cursor.execute(SNo,(username,))
db.commit()
SNo = cursor.fetchone()
S_No = SNo[0]
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('An error occurred. Please try again later','error')
# return redirect(url_for('vehicle.add_data', SNo=SNo))
try:
VID = 'SELECT max(VehicleID)+1 as VID FROM Vehicle'
cursor.execute(VID)
maxVID = cursor.fetchone()[0]
db.commit()
except mysql.connector.Error as e:
print(e)
db.rollback()
flash('An error occured. Please try again','error')
return redirect(url_for('vehicle.add_data', SNo = S_No))
VehicleID = maxVID
if request.method == 'POST':
if not username:
flash('No username','error')
return redirect(url_for('vehicle.add_data'))
if not VID:
flash('Cannot continue without vehicleID', 'danger')
return redirect(url_for('vehicle.add_data', SNo=S_No))
session['VehicleID'] = VehicleID
VehicleType = request.form.get('VehicleType')
VehicleNumber = request.form.get('VehicleNumber')
VehicleName = request.form.get('VehicleName')
action = request.form.get('action')
check_number_query = 'SELECT VehicleNumber FROM vehicle'
cursor.execute(check_number_query)
db.commit()
Reg_Number = cursor.fetchall()
existing_number = [vehicle[0] for vehicle in Reg_Number]
if VehicleNumber in existing_number:
flash('This vehicle is already saved. Please select this vehicle','error')
return redirect(url_for('vehicle.ChooseVehicle'))
if not S_No:
print('not s no.')
flash('An error occurred. Please try again after sometime', 'error')
return redirect(url_for('vehicle.add_data', SNo=S_No))
if not ValidNumber(VehicleNumber):
flash('Registration Number is not valid')
return redirect(url_for('vehicle.add_data', SNo=S_No))
try:
insert_query = '''
INSERT INTO vehicle (VehicleID, VehicleType, VehicleNumber, VehicleName, SNo) VALUES (%s, %s, %s, %s, %s)
'''
cursor.execute(insert_query, (VehicleID, VehicleType, VehicleNumber, VehicleName, S_No,))
db.commit()
print('')
if action == 'book_slot':
print('book_Slot mai gaya')
return redirect(url_for('bookingslot.add_data',VehicleID=VehicleID, SNo=SNo))
elif action == 'save_vehicle':
print('save_vehicle mai gaya')
flash('Vehicle saved successfully')
return redirect(url_for('auth.dashboard'))
except mysql.connector.Error as e:
db.rollback()
flash('Error adding data', 'error')
return redirect(url_for('vehicle.add_data', SNo=S_No))
return render_template('add/Save_vehicle.html', SNo=S_No, data=data, first_elements=first_elements)
@Vehicle.route('/vehicle/edit/<int:VehicleID>', methods=['GET', 'POST'])
@login_required
def edit_data(VehicleID):
if request.method == 'POST':
VehicleType = request.form['VehicleType']
VehicleNumber = request.form['VehicleNumber']
try:
update_query = '''UPDATE vehicle
SET VehicleType=%s, VehicleNumber=%s
where VehicleID=%s
'''
cursor.execute(update_query, (VehicleType, VehicleNumber, VehicleID))
db.commit()
flash('Data updated successfully')
return redirect(url_for('vehicle.display'))
except mysql.connector.Error as e:
db.rollback()
flash('Error updating data')
fetch_query = 'Select VehicleID, VehicleType, VehicleNumber from vehicle where VehicleID=%s'
cursor.execute(fetch_query, (VehicleID,))
db.commit()
data = cursor.fetchone()
if data is None:
flash('No data found')
return redirect(url_for('vehicle.display'))
return render_template('edit/vehicle.html', data=data)
@Vehicle.route('/vehicle/delete/vehicle/<int:VehicleID>', methods=['GET','POST'])
@login_required
def delete_vehicle(VehicleID):
if request.method == 'POST':
try:
delete_query = '''DELETE FROM vehicle WHERE VehicleID=%s'''
cursor.execute(delete_query, (VehicleID,))
flash('Data deleted successfully')
return redirect(url_for('vehicle.ChooseVehicle'))
except mysql.connector.Error as e:
db.rollback()
flash('Error deleting data')
fetch_query = "SELECT VehicleID, VehicleType, VehicleNumber FROM vehicle WHERE VehicleID=%s"
cursor.execute(fetch_query, (VehicleID,))
data = cursor.fetchone()
if data is None:
flash('Data not found')
return redirect(url_for('vehicle.ChooseVehicle'))
return render_template('delete/ChooseVehicle.html', data=data)
@Vehicle.route('/vehicle/delete/<int:VehicleID>', methods=['GET', 'POST'])
@login_required
def delete_data(VehicleID):
if request.method == 'POST':
try:
delete_query = '''DELETE FROM vehicle WHERE VehicleID=%s'''
cursor.execute(delete_query, (VehicleID,))
flash('Data deleted successfully')
return redirect(url_for('vehicle.display'))
except mysql.connector.Error as e:
db.rollback()
flash('Error deleting data')
fetch_query = "SELECT VehicleID, VehicleType, VehicleNumber, VehicleName FROM vehicle WHERE VehicleID=%s"
cursor.execute(fetch_query, (VehicleID,))
data = cursor.fetchone()
print(data,'daya')
if data is None:
flash('Data not found')
return redirect(url_for('vehicle.display'))
return render_template('delete/vehicle.html', data=data)
@Vehicle.route('/user/Bookings', methods=['GET', 'POST'])
@login_required
def MyBookingsUser():
if request.method == 'GET':
username = session.get('username')
print('username in mybookingsuser', username)
if not username:
flash('An error ocurred. Please try again later', 'error')
return redirect(url_for('index'))
cursor.execute('SELECT SNo FROM user WHERE username=%s', (username,))
db.commit()
S_No = cursor.fetchone()
print('SNo:',S_No)
if not S_No:
flash('An error ocurred. Please try again later', 'error')
# return redirect(url_for('index'))
SNo = S_No[0]
cursor.execute('SELECT VehicleID from vehicle where SNo=%s', (SNo,))
db.commit()
VID = cursor.fetchone()
VehicleID = VID[0]
if not VID:
flash('No current bookings')
if not VehicleID:
flash('No current bookings')
print("VehicleID in mybookingsuser:", VehicleID)
if not SNo:
flash('An error ocurred. Please try again later', 'error')
# return redirect(url_for('index'))
try:
fetch_query = '''
SELECT * from allotment WHERE username=%s AND TimeTo<curtime()
'''
cursor.execute(fetch_query,(username,))
db.commit()
data = cursor.fetchall()
data_list = [[dashboard[0], dashboard[1], dashboard[2], dashboard[3], dashboard[4], dashboard[5], dashboard[6], dashboard[7], dashboard[8], dashboard[9], dashboard[10], dashboard[11], dashboard[12], dashboard[13]] for dashboard in data]
print('data_list past: ', data_list)
if not data_list:
flash('No past bookings', 'success')
except mysql.connector.Error as e:
print('fetch past wale except mai')
db.rollback()
print(e)
flash('An error occurred. Please try again later', 'error')
return redirect(url_for('index'))
print('VehicleID on top of try: ', VehicleID)
try:
print('VehicleID on inside try: ', VehicleID)
fetch_current = '''
SELECT
o.name,
o.contact,
v.VehicleType,
v.VehicleNumber,
b.Date,
b.TimeFrom,
b.TimeTo,
b.duration,
o.address,
b.BSlotID AS slot
FROM owner o
INNER JOIN vehicle v ON o.SNo = v.SNo
INNER JOIN bookingslot b ON o.SNo = b.SNo
INNER JOIN user u ON o.SNo = u.SNo
WHERE
o.SNo = %s AND b.VehicleID=%s AND (b.Date > CURDATE() OR (b.Date = CURDATE() AND b.TimeTo > CURTIME()))ORDER BY DATE DESC;
'''
cursor.execute(fetch_current,(SNo,VehicleID))
data = cursor.fetchall()
print('current data: ',data)
datalist = [[booking[0], booking[1], booking[2], booking[3], booking[4], booking[5], booking[6], booking[7], booking[8], booking[9]] for booking in data]
print('datalist: ', datalist)
if not datalist:
flash('No future bookings', 'success')
except mysql.connector.Error as e:
print('fetch current wale except mai')
print(e)
db.rollback()
flash('An error occured.Please try again later','error')
if 'role' in session:
return render_template('dashboard.html', data=data_list, datalist = datalist,
role = session['role'])