-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
746 lines (699 loc) · 25.3 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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
from flask import Flask,request,Response
import sqlite3
import database
from func import *
app = Flask(__name__)
@app.route('/',methods=['GET'])
def res():
return read_file('frontend/login.html')
@app.route('/login',methods=['POST'])
def myuser():
data = request.form.to_dict()
global idd
idd = data['User']
pas = data['Pass']
role = data['Role']
log = database.login_check(idd,pas,role)
page = read_file('frontend/login_fail.html')
if log:
print(role)
if role=='Admin':
return read_file('frontend/admin.html').format(idd,idd)
elif role=='Employee':
return read_file('frontend/employee.html').format(idd,idd,idd,idd)
else:
return read_file('frontend/user.html').format(idd,idd,idd,idd,idd,idd,idd,idd)
else:
return page
@app.route('/search/<admin_id>',methods=['GET'])
def search_users(admin_id):
return read_file('frontend/search_user.html').format(admin_id,'')
@app.route('/search/<admin_id>',methods=['POST'])
def search(admin_id):
data = request.form.to_dict()
query = data['search']
typ = data['table']
atr = data['attr']
out = database.search_case(typ,query,atr)
string = ""
if typ == 'user':
string = """<tr><th>User ID</th><th>First Name</th><th>Last Name</th><th>Password</th><th>Email</th><th>Address</th></tr>"""
else:
string = "<tr><th>Employee ID</th><th>First Name</th><th>Last Name</th><th>Password</th><th>Gender</th><th>Age</th><th>Email</th><th>Address</th><th>Position</th></tr>"
for i in out:
string += """<tr>"""
for j in i:
string += """<th>{}</th>""".format(j)
string += """<tr>"""
return read_file('frontend/search_user.html').format(admin_id,string)
@app.route('/add_emp',defaults={'typ':'none'})
@app.route('/add_emp/<typ>',methods=['GET','POST'])
def add_emp(typ):
if request.method == 'GET':
return read_file('frontend/add_emp.html')
elif request.method == 'POST':
if typ == 'add':
data = request.form.to_dict()
for i in data:
if len(data[i])==0:
return read_file('frontend/add_emp.html')+"""<br> <br> <p> values can't be empty</p>"""
if len(select_employee(data['User']))>0:
return read_file('frontend/add_emp.html')+"""<br> <br> <p> User already exists</p>"""
insert_data_emp_table(data['User'],data['fname'],data['lname'],data['Pass'],data['gender'],data['age'],data['email'],data['address'],data['pos'])
return read_file('frontend/add_emp.html') + """<br><p> Added the user</p>"""
elif typ == 'del':
data = request.form.to_dict()
if len(select_employee(data['User']))==0:
return read_file('frontend/add_emp.html')+"""<br> <br> <p> No such user exists</p>"""
delete_employee(data['User'])
return read_file('frontend/add_emp.html') + """<br> <p> deleted the employee {}</p>""".format(data['User'])
@app.route('/register',methods=['GET'])
def register():
return read_file('frontend/register.html')
@app.route('/admin/profile/<admin_id>',methods=['GET','POST'])
def admin_prof(admin_id):
if request.method == 'GET':
my = select_admin(admin_id)
out = my[0]
return read_file('frontend/admin_profile.html').format(admin_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7])
elif request.method == 'POST':
data = request.form.to_dict()
my = select_admin(admin_id)
out = my[0]
for val in data:
if len(val)==0:
return read_file('frontend/admin_profile.html').format(emp_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]) + """<br><br><p> Values can't be empty </p>"""
update_adm(data['fname'],data['lname'],data['Pass'],data['gender'],data['age'],data['email'],data['address'],data['User'])
my = select_admin(admin_id)
out = my[0]
return read_file('frontend/admin_profile.html').format(admin_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]) + """<br><p>Updated!</p>"""
@app.route('/user/profile/<user_id>',methods=['GET','POST'])
def user_prof(user_id):
if request.method == 'GET':
my = select_user(user_id)
out = my[0]
return read_file('frontend/user_profile.html').format(user_id,out[0],out[1],out[2],out[3],out[4],out[5])
elif request.method == 'POST':
data = request.form.to_dict()
my = select_user(user_id)
out = my[0]
for val in data:
if len(val)==0:
return read_file('frontend/user_profile.html').format(emp_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]) + """<br><br><p> Values can't be empty </p>"""
update_user(data['fname'],data['lname'],data['Pass'],data['email'],data['address'],data['User'])
my = select_user(user_id)
out = my[0]
return read_file('frontend/user_profile.html').format(user_id,out[0],out[1],out[2],out[3],out[4],out[5])+"""<br><br><p> Updated the new info!</p>"""
@app.route('/emp/profile/<emp_id>',methods=['GET','POST'])
def emp_prof(emp_id):
if request.method == 'GET':
my = select_employee(emp_id)
out = my[0]
return read_file('frontend/emp_profile.html').format(emp_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7])
elif request.method == 'POST':
data = request.form.to_dict()
is_empty=False
my = select_employee(emp_id)
out = my[0]
for val in data:
if len(val)==0:
return read_file('frontend/emp_profile.html').format(emp_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]) + """<br><br><p> Values can't be empty </p>"""
update_emp(data['fname'],data['lname'],data['Pass'],data['gender'],data['age'],data['email'],data['address'],data['User'])
my = select_employee(emp_id)
out = my[0]
return read_file('frontend/emp_profile.html').format(emp_id,out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]) + """<br><p> Updated successfully </p> """
@app.route('/allusers',methods=['GET'])
def allusers():
b = database.print_use()
string = ''
for i in b:
for j in i:
try:
string = string + j
except:
string = string + str(j)
string = string + "\t"
string = string + '\n'
return string
@app.route('/register',methods=['POST'])
def regi():
data = request.form.to_dict()
idd = data['User']
fname = data['fname']
lname = data['lname']
pas = data['Pass']
email = data['email']
address = data['address']
log = database.add_user(idd,fname,lname,pas,email,address)
if log:
return read_file('frontend/regi_succ.html')
else:
return read_file('frontend/regi_fail.html')
@app.route('/reviews/<user_id>',methods=['GET','POST'])
def review(user_id):
if request.method == 'GET':
output = database.get_rev()
string = """<!DOCTYPE html>
<html>
<body><h1 style="text-align: center;"> Reviews </h1><br><br>"""
for i in output:
string += """<fieldset>
<p> User ID: {} </p>
<p> Name : {} {} </p>
<p> Wrote: {} </p></fieldset>""".format(i[0],i[1],i[2],i[3])
string += """<form action="http://127.0.0.1:5000/reviews/{}" method="post">
<p> Write a review </p>
<input type="text" id="tex" name="tex"><br>
<input type="submit" value="Send"></form></body></html>""".format(user_id)
return string
elif request.method == 'POST':
data = request.form.to_dict()
t = data['tex']
insert_rev(user_id,t)
output = database.get_rev()
string = """<!DOCTYPE html>
<html>
<body><h1 style="text-align: center;"> Reviews </h1><br><br>"""
for i in output:
string += """<fieldset>
<p> User ID: {} </p>
<p> Name: {} {} </p>
<p> Wrote: {} </p></fieldset>""".format(user_id,i[1],i[2],i[3])
string += """<form action="http://127.0.0.1:5000/reviews/{}" method="post">
<p> Write a review </p>
<input type="text" id="tex" name="tex"><br>
<input type="submit" value="Send"></form></body></html>""".format(user_id)
return string
@app.route('/view_jobs/<emp_id>',methods=['GET'])
def view_jobs(emp_id):
my = select_employee(emp_id)
out = my[0]
jobs = search_jobs(emp_id)
string = ""
if len(jobs)==0:
string = "No jobs assigned to you"
for i in jobs:
for j in i:
string += """<li>{}</li>""".format(j)
return read_file('frontend/view_jobs.html').format(out[1],out[2],out[8],string)
@app.route('/assign/<typ>',methods=['GET','POST'])
def assign(typ):
if request.method == 'GET':
return read_file('frontend/assign_job.html').format("","","")
elif request.method == 'POST':
if typ == 'view':
data = request.form.to_dict()
eid = data['eid']
jobs = search_jobs(eid)
if len(jobs)==0:
return read_file('frontend/assign_job.html').format("Employee does not exist or no jobs assigned","","")
string = "Jobs : <br>"
for i in jobs:
for j in i:
string += """<li>{}</li>""".format(j)
return read_file('frontend/assign_job.html').format(string,"","")
elif typ == 'asig':
data = request.form.to_dict()
eid = data['eid']
job = data['job']
inserted = insert_job(eid,job)
if inserted:
return read_file('frontend/assign_job.html').format("","New job inserted","")
else:
return read_file('frontend/assign_job.html').format("","Employee id is not correct","")
elif typ == 'del':
data = request.form.to_dict()
jid = data['jid']
deleted = delete_job(jid)
if deleted:
return read_file('frontend/assign_job.html').format("","","Jobs deleted")
else:
return read_file('frontend/assign_job.html').format("","","Job id is incorrect")
else:
return "error 404"
@app.route('/report/employee/<emp_id>',methods=['GET','POST'])
def report_by_emp(emp_id):
if request.method == 'GET':
return read_file('frontend/emp_rep.html').format(emp_id,"")
elif request.method == 'POST':
data = request.form.to_dict()
a = data['reason']
if len(a)<10:
return read_file('frontend/user_rep.html').format(user_id,'Reason should be more than 10 characters')
insert_report_emp(emp_id,a)
return read_file('frontend/emp_rep.html').format(emp_id,'Reported!')
@app.route('/report/user/<user_id>',methods=['GET','POST'])
def report_by_user(user_id):
if request.method == 'GET':
return read_file('frontend/user_rep.html').format(user_id,"")
elif request.method == 'POST':
data = request.form.to_dict()
a = data['reason']
if len(a)<10:
return read_file('frontend/user_rep.html').format(user_id,'Reason should be more than 10 characters')
insert_report_user(user_id,a)
return read_file('frontend/user_rep.html').format(user_id,'Reported!')
@app.route('/view_reports',methods=['GET'])
def view_reps():
fine = print_fine()
emp_rep = print_report_emp()
user_rep = print_report_user()
str1 = """<tr>
<th>Fine ID</th>
<th>Employee</th>
<th>User</th>
<th>Reason</th>
<th>Amount</th>
</tr>"""
str2 = """<tr>
<th>Report ID</th>
<th>Employee</th>
<th>Problem</th>
</tr>"""
str3 = """<tr>
<th>Report ID</th>
<th>User</th>
<th>Problem</th>
</tr>"""
for i in fine:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """<tr>"""
for i in emp_rep:
str2 += """<tr>"""
for j in i:
str2 += """<th>{}</th>""".format(j)
str2 += """<tr>"""
for i in user_rep:
str3 += """<tr>"""
for j in i:
str3 += """<th>{}</th>""".format(j)
str3 += """<tr>"""
return read_file('frontend/view_reports.html').format(str1,str2,str3)
@app.route('/balance/<user_id>/<typ>',methods=['GET','POST'])
def balance(user_id,typ):
if request.method == 'GET':
my = print_account(user_id)
if len(my)==0:
return read_file('frontend/card_details.html').format(user_id,"")
else:
out = my[0]
return read_file('frontend/bal_page.html').format(out[1],out[2],out[3],out[4],user_id)
elif request.method == 'POST':
if typ == 'card':
data = request.form.to_dict()
cc = data['cc']
ed = data['ed']
cvc = data['cvc']
if len(cc)!=16 or len(ed)!=5 or len(cvc)!=3:
return read_file('frontend/card_details.html').format(user_id,"Values not entered correcelty")
ins = insert_balance(user_id,cc,ed,cvc)
if ins:
my = print_account(user_id)
out = my[0]
return read_file('frontend/bal_page.html').format(out[1],out[2],out[3],out[4],user_id)
else:
return read_file('frontend/card_details.html').format(user_id,"Values not entered correcelty")
elif typ == 'update':
up = update_balance(user_id,0)
if not up:
return "error"
my = print_account(user_id)
out = my[0]
return read_file('frontend/bal_page.html').format(out[1],out[2],out[3],out[4],user_id)+"""<p> Updated</p>"""
@app.route('/fine/<emp_id>',methods=['GET','POST'])
def fine_emp(emp_id):
if request.method == 'GET':
return read_file('frontend/emp_fine.html').format(emp_id,"")
elif request.method == 'POST':
data = request.form.to_dict()
uid = data['user']
fine = data['fine']
reason = data['reason']
if len(reason)==0 or len(fine)==0 or len(uid)==0:
return read_file('frontend/emp_fine.html').format(emp_id,"Values not entered correcelty")
try:
amount = int(fine)
except:
return read_file('frontend/emp_fine.html').format(emp_id,"Fine is a number.")
Updated = update_balance(uid,amount)
if Updated:
insert_fine(emp_id,uid,reason,amount)
return read_file('frontend/emp_fine.html').format(emp_id,"Amount added to the user account")
else:
return read_file('frontend/emp_fine.html').format(emp_id,"user does not exist")
@app.route('/trains',methods=['GET','POST'])
def train():
if request.method == 'GET':
out = print_train()
str1 = """<tr><th>Train ID</th><th>Name</th><th>Class</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """<tr>"""
return read_file('frontend/trains.html').format(str1,"")
elif request.method == 'POST':
data = request.form.to_dict()
name = data['name']
cla = data['class']
if len(name)==0 or len(cla)==0:
out = print_train()
str1 = """<tr><th>Train ID</th><th>Name</th><th>Class</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/trains.html').format(str1,"can't be empty")
inser_train(name,cla)
out = print_train()
str1 = """<tr><th>Train ID</th><th>Name</th><th>Class</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/trains.html').format(str1,"added")
@app.route('/stations',methods=['GET','POST'])
def stations():
if request.method == 'GET':
out = print_station()
str1 = """<tr><th>Station ID</th><th>Name</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/stations.html').format(str1,"")
elif request.method == 'POST':
data = request.form.to_dict()
name = data['name']
if len(name)==0:
out = print_station()
str1 = """<tr><th>Station ID</th><th>Name</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/stations.html').format(str1,"can't be empty")
k = insert_staton_name(name)
out = print_station()
str1 = """<tr><th>Station ID</th><th>Name</th></tr>"""
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """<tr>"""
s = "station added or already there! id is {}".format(k)
return read_file('frontend/stations.html').format(str1,s)
@app.route('/trip',methods=['GET'])
def trip_S():
return read_file('frontend/trip.html').format("")
@app.route('/trip/add/<typ>',methods=['GET','POST'])
def add_trip(typ):
if request.method == 'GET':
return read_file('frontend/trip_add1.html').format("")
elif request.method == 'POST':
if typ == 'add1':
data = request.form.to_dict()
tid = data['tid']
n = data['num']
cap = data['capacity']
price = data['price']
st = select_train(tid)
if len(st)==0:
return read_file('frontend/trip_add1.html').format("Invalid Train ID")
try:
num = int(n)
except:
return read_file('frontend/trip_add1.html').format("Number of stations must be a number")
try:
capacity = int(cap)
except:
return read_file('frontend/trip_add1.html').format("capacity must be integer")
try:
p = int(price)
except:
return read_file('frontend/trip_add1.html').format("price must be number")
trip_id = insert_train_info(tid,num,capacity,p)
count = 0
string = ""
for i in range(1,num+1):
string += """<label for="s{}">Station {} ID:</label>
<input type="text" id="s{}" name="s{}"><br>
<label for="st1{}">Start Time {}:</label>
<input type="text" id="st1{}" name="st1{}">
<input type="text" id="st2{}" name="st2{}">
<label for="sap{}">AM/PM:</label>
<select name="sap{}" id="sap{}">
<option value="A">AM</option>
<option value="P">PM</option>
</select>
<label for="et1{}">Time to next station {} ID:</label>
<input type="text" id="et1{}" name="et1{}">
<input type="text" id="et2{}" name="et2{}">
<label for="eap{}">AM/PM:</label>
<select name="eap{}" id="eap{}">
<option value="A">AM</option>
<option value="P">PM</option>
</select><br><br>""".format(i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i)
return read_file('frontend/trip_add2.html').format(trip_id,num,string)
elif typ == 'add2':
data = request.form.to_dict()
trip_id = data['tid']
num = data['num']
try:
n = int(num)
except:
return "error in numer of stations"
dic = {}
a = []
for i in range(1,n+1):
st = 's{}'.format(i)
a.append(data[st])
st = 'st1{}'.format(i)
a.append(data[st])
st = 'st2{}'.format(i)
a.append(data[st])
st = 'sap{}'.format(i)
a.append(data[st])
st = 'et1{}'.format(i)
a.append(data[st])
st = 'et2{}'.format(i)
a.append(data[st])
st = 'eap{}'.format(i)
a.append(data[st])
dic[i]=a
a = []
b = []
for i in range(1,n):
a = dic[i]
start = a[0]
st1 = hour_to_min(a[1],a[2],a[3])
et1 = hour_to_min(a[4],a[5],a[6])
b = dic[i+1]
end = b[0]
insert_station(start,trip_id,end,st1,et1)
return read_file('frontend/trip.html').format("Added the trip")
@app.route('/trip/update',methods=['GET','POST'])
def up_trip():
if request.method == 'GET':
return read_file('frontend/update_trip.html').format("")
elif request.method == 'POST':
data = request.form.to_dict()
tid = data['tid']
k = check_trip_id(tid)
if len(k)==0:
return read_file('frontend/update_trip.html').format("Invalid trip Id")
val = data['val']
select = data['select']
try:
v = int(val)
except:
return read_file('frontend/update_trip.html').format("value must be a number")
if select=='price':
update_price(tid,v)
return read_file('frontend/update_trip.html').format("Update the price!")
elif select=='cap':
update_cap(tid,v)
return read_file('frontend/update_trip.html').format("Capacity updated!")
@app.route('/view_timetable',methods=['GET'])
def view_t():
out = database.view_sch()
str1 = """<tr><th>Station Name</th><th>Trip Id</th><th>Train Name</th><th>Next Station</th><th>Start Time</th><th>End Time</th><th>Capacity</th><th>Price</th></tr>"""
dic = {}
a = []
for i in out:
a.append(get_station_name(i[0]))
a.append(i[1])
a.append(get_train_name(i[1]))
a.append(get_station_name(i[2]))
a.append(min_to_hour(i[3]))
a.append(min_to_hour(i[4]))
a.append(get_price_cap(i[1])[0])
a.append(get_price_cap(i[1])[1])
dic[i] = a
a = []
for i in dic:
str1 += """<tr>"""
a = dic[i]
for j in a:
str1 += """<th> {} </th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/view.html').format(str1)
@app.route('/book/<user_id>/<typ>',methods=['GET','POST'])
def book_ticket(user_id,typ):
if request.method == 'GET':
bal = print_account(user_id)
if len(bal)==0:
return read_file('frontend/inserted.html').format(user_id)
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,"")
elif request.method == 'POST':
if typ == 'search':
data = request.form.to_dict()
start = data['from']
st_id = get_staton_id(start)
if st_id==0:
s1 = "No train leaving from {} ".format(start)
return read_file('frontend/book_ticket.html').format(user_id,s1,user_id,"")
end = data['to']
ed_id = get_staton_id(end)
if ed_id==0:
s1 = "No train going to {} ".format(end)
return read_file('frontend/book_ticket.html').format(user_id,s1,user_id,"")
t1 = data['st']
t2 = data['st2']
t3 = data['sap']
time = hour_to_min(t1,t2,t3)
route = find_route(int(st_id),int(ed_id),time)
if len(route)==0:
return read_file('frontend/book_ticket.html').format(user_id,"""<br>No Trains Leaving for selected route""",user_id,"")
str1 = """<tr><th>Trip ID</th><th>FROM</th><th>TIME</th><th>TO</th><th>TIME</th><th>TRAIN NAME</th><th>CAP</th><th>Filled</th><th>Price</th></tr>"""
a = []
dic = {}
for i in route:
a.append(i)
a.append(start)
a.append(min_to_hour(route[i][1]))
a.append(end)
a.append(min_to_hour(route[i][3]))
a.append(get_train_name(i))
a.append(get_price_cap(i)[0])
a.append(get_filed(i))
a.append(get_price_cap(i)[1])
dic[i] = a
a = []
s = str1
for i in dic:
s += """<tr>"""
a = dic[i]
for j in a:
s += """<th> {} </th>""".format(j)
s += """<tr>"""
return read_file('frontend/book_ticket.html').format(user_id,s,user_id,"")
if typ == 'book':
data = request.form.to_dict()
a = data['tid']
f = data['from']
t = data['to']
if not check_trip_id(a):
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,"Trip id is invalid")
book = for_book(f,t,a)
if len(book) != 2:
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,"No trip exists for given entries")
cap,price = get_price_cap(a)
filled = get_filed(a)
if filled<cap:
done = update_balance(user_id,price)
if done:
ticket = insert_booking(a,user_id,f,book[0],t,book[1])
s = "<br>Trip booked. Your ticket number is {}".format(ticket)
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,s)
else:
s = "<br>Encountered error while billing! No booking!"
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,s)
else:
s = "Trip is fully booked! <br>"
return read_file('frontend/book_ticket.html').format(user_id,"",user_id,s)
@app.route('/tickets/<user_id>',methods=['GET','POST'])
def view_tic(user_id):
if request.method == 'GET':
str1 = """<tr><th>Ticket ID</th><th>Trip ID</th><th>User ID</th><th>FROM</th><th>TIME</th><th>TO</th><th>TIME</th></tr>"""
out = view_ticket(user_id)
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/view_tickets.html').format(str1,user_id,"")
elif request.method == 'POST':
data = request.form.to_dict()
tid = data['tid']
str1 = """<tr><th>Ticket ID</th><th>Trip ID</th><th>User ID</th><th>FROM</th><th>TIME</th><th>TO</th><th>TIME</th></tr>"""
out = view_ticket(user_id)
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
ret = check_ticket(tid)
if len(ret)==0:
return read_file('frontend/view_tickets.html').format(str1,user_id,"Ticket ID is invalid")
d = del_ticket(tid)
str1 = """<tr><th>Ticket ID</th><th>Trip ID</th><th>User ID</th><th>FROM</th><th>TIME</th><th>TO</th><th>TIME</th></tr>"""
out = view_ticket(user_id)
for i in out:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/view_tickets.html').format(str1,user_id,"Ticket Removed!")
@app.route('/cticket',methods=['GET','POST'])
def cticker():
if request.method == 'GET':
return read_file('frontend/cticket.html').format("","")
elif request.method == 'POST':
data = request.form.to_dict()
t = data['tid']
a = check_ticket(t)
if len(a)==0:
return read_file('frontend/cticket.html').format("The Ticket number is Invalid","")
user = a[0][2]
out = select_user(user)
str1 = """<tr><th>User ID</th><th>First Name</th><th>Last Name</th><th>Email</th><th>Address</th></tr>"""
count = 0
for i in out:
str1 += """<tr>"""
for j in i:
count +=1
if count!=4:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
count = 0
s = "The ticket belongs to {}".format(user)
return read_file('frontend/cticket.html').format(s,str1)
@app.route('/trip_users',methods=['GET','POST'])
def trip_users():
if request.method == 'GET':
return read_file('frontend/trip_user.html').format("Search Results will appear below","")
elif request.method == 'POST':
data = request.form.to_dict()
d = data['tid']
r = cticket(d)
if len(r)==0:
return read_file('frontend/trip_user.html').format("NO Results Found for the given trip id","")
str1 = """<tr><th>Tickets Bought</th><th>User ID</th><th>First Name</th><th>Last Name</th></tr>"""
for i in r:
str1 += """<tr>"""
for j in i:
str1 += """<th>{}</th>""".format(j)
str1 += """</tr>"""
return read_file('frontend/trip_user.html').format("Users for trip",str1)
@app.route('/test',methods=['GET'])
def test():
return read_file('frontend/panel.html')
if __name__ == '__main__':
app.run(debug=True)