-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
1524 lines (1359 loc) · 60.5 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
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, session, url_for, redirect, flash
import mysql.connector
import datetime
import json
import decimal
#####################################################################
# COMMON #
# all operations from all sides #
#####################################################################
# initialize the app from Flask
app = Flask(__name__)
# configure MySQL
conn = mysql.connector.connect(host='localhost',
user='root',
password='',
database='flyeasy')
#####################################################################
# HELPER #
# malicious input such as (2 or '1' = '1') #
#####################################################################
def check_apostrophe(x):
assert type(x) == str
if "'" not in x:
return x
db_x = ''
for i in x:
if i == "'":
db_x += "''"
else:
db_x += i
return db_x
#####################################################################
# PUBLIC #
# all operations from the public side #
#####################################################################
@app.route('/')
def publicHome():
return render_template('publicHome.html')
@app.route('/publicSearchFlight', methods=['GET', 'POST'])
def publicSearchFlight():
departure_city = check_apostrophe(request.form['departure_city'])
departure_airport = check_apostrophe(request.form['departure_airport'])
arrival_city = check_apostrophe(request.form['arrival_city'])
arrival_airport = check_apostrophe(request.form['arrival_airport'])
departure_date = request.form['departure_date']
arrival_date = request.form['arrival_date']
cursor = conn.cursor()
query = "SELECT airline_name, flight_num, departure_city, departure_airport, departure_time, arrival_city, arrival_airport, arrival_time, price, airplane_id \
FROM public_search_flight \
WHERE departure_airport = if (%s = '', departure_airport, %s) AND \
arrival_airport = if (%s = '', arrival_airport, %s) AND \
status = 'upcoming' AND \
departure_city = if (%s = '', departure_city, %s) AND \
arrival_city = if (%s = '', arrival_city, %s) AND \
date(departure_time) = if (%s = '', date(departure_time), %s) AND \
date(arrival_time) = if (%s = '', date(arrival_time), %s) \
ORDER BY airline_name, flight_num"
cursor.execute(query, (departure_airport, departure_airport, arrival_airport, arrival_airport, departure_city, departure_city, arrival_city, arrival_city, departure_date, departure_date, arrival_date, arrival_date))
data = cursor.fetchall()
cursor.close()
if (data): # has data
return render_template('publicHome.html', upcoming_flights=data)
else: # does not have data
error = 'Sorry, we can\'t find any flight!'
return render_template('publicHome.html', error1=error)
@app.route('/publicSearchStatus', methods=['GET', 'POST'])
def publicSearchStatus():
airline_name = check_apostrophe(request.form['airline_name'])
flight_num = request.form['flight_num']
arrival_date = request.form['arrival_date']
departure_date = request.form['departure_date']
cursor = conn.cursor()
query = "SELECT * \
FROM public_search_flight \
WHERE flight_num = if (%s = '', flight_num, %s) AND \
date(departure_time) = if (%s = '', date(departure_time), %s) AND \
date(arrival_time) = if (%s = '', date(arrival_time), %s) AND \
airline_name = if (%s = '', airline_name, %s) \
ORDER BY airline_name, flight_num"
cursor.execute(query, (flight_num, flight_num, arrival_date, arrival_date, departure_date, departure_date, airline_name, airline_name))
data = cursor.fetchall()
cursor.close()
if (data): # has data
# upcoming_flights = session['upcoming_flights']
return render_template('publicHome.html', statuses=data)
else: # does not have data
error = 'Sorry, we can\'t find this flight!'
return render_template('publicHome.html', error2=error)
#####################################################################
# CUSTOMER #
# all operations from the customer side #
#####################################################################
# define route for login
@app.route('/cuslogin')
def cuslogin():
return render_template('cuslogin.html')
# define route for register
@app.route('/cusregister')
def cusregister():
return render_template('cusregister.html')
# authenticate customer login
@app.route('/cusloginAuth', methods=['GET', 'POST'])
def cusloginAuth():
if "email" in request.form and 'password' in request.form:
email = request.form['email']
db_email = check_apostrophe(email)
password = request.form['password']
cursor = conn.cursor()
query = "SELECT * FROM customer WHERE email = %s AND password = md5(%s)"
cursor.execute(query, (db_email, password))
data = cursor.fetchone()
cursor.close()
if(data):
session['email'] = email
cursor = conn.cursor()
query = "SELECT ticket_id, airline_name, airplane_id, flight_num, D.airport_city, departure_airport, A.airport_city, arrival_airport, departure_time, arrival_time, status \
FROM flight NATURAL JOIN purchases NATURAL JOIN ticket, airport AS D, airport AS A \
WHERE customer_email = %s AND status = 'upcoming' AND \
D.airport_name = departure_airport AND A.airport_name = arrival_airport"
cursor.execute(query, (db_email,))
data1 = cursor.fetchall()
cursor.close()
return render_template('cushome.html', email=email, emailName=email.split('@')[0], view_my_flights=data1)
else:
# return an error message to the html page
error = 'Error: Invalid email or password!'
return render_template('cuslogin.html', error=error)
else:
session.clear()
return render_template('404.html')
# authenticate customer register
@app.route('/cusregisterAuth', methods=['GET', 'POST'])
def cusregisterAuth():
if "email" in request.form and \
'name' in request.form and \
'password' in request.form and \
'building_number' in request.form and \
'street' in request.form and \
'city' in request.form and \
'state' in request.form and \
'phone_number' in request.form and \
'passport_number' in request.form and \
'passport_expiration' in request.form and \
'passport_country' in request.form and \
'date_of_birth' in request.form:
email = request.form['email']
db_email = check_apostrophe(email)
name = request.form['name']
db_name = check_apostrophe(name)
password = request.form['password']
building_number = check_apostrophe(request.form['building_number'])
street = check_apostrophe(request.form['street'])
city = check_apostrophe(request.form['city'])
state = check_apostrophe(request.form['state'])
phone_number = request.form['phone_number']
passport_number = check_apostrophe(request.form['passport_number'])
passport_expiration = request.form['passport_expiration']
passport_country = check_apostrophe(request.form['passport_country'])
date_of_birth = request.form['date_of_birth']
cursor = conn.cursor()
query = "SELECT * FROM customer WHERE email = %s"
cursor.execute(query, (db_email,))
data = cursor.fetchone()
if(data):
cursor.close()
error = "This user already exists!"
return render_template('cusregister.html', error = error)
else:
new = "INSERT INTO customer VALUES (%s, %s, md5(%s), %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(new, (db_email, db_name, password, building_number, street, city, state, phone_number, passport_number, passport_expiration, passport_country, date_of_birth))
conn.commit()
query = "SELECT ticket_id, airline_name, airplane_id, flight_num, D.airport_city, \
departure_airport, A.airport_city, arrival_airport, departure_time, arrival_time, status \
FROM flight NATURAL JOIN purchases NATURAL JOIN ticket, airport AS D, airport AS A\
WHERE customer_email = %s AND status = 'upcoming' AND \
D.airport_name = departure_airport AND A.airport_name = arrival_airport"
cursor.execute(query, (db_email,))
data1 = cursor.fetchall()
cursor.close()
flash("Welcome to FlyEasy!")
session['email'] = email
return render_template('cushome.html', email=email, emailName=email.split('@')[0], view_my_flights=data1)
else:
session.clear()
return render_template('404.html')
@app.route('/cushome')
def cushome():
if session.get('email'):
email = session['email']
db_email = check_apostrophe(email)
cursor = conn.cursor()
query = "SELECT ticket_id, airline_name, airplane_id, flight_num, D.airport_city, \
departure_airport, A.airport_city, arrival_airport, departure_time, arrival_time, status \
FROM flight NATURAL JOIN purchases NATURAL JOIN ticket, airport AS D, airport AS A\
WHERE customer_email = %s AND status = 'upcoming' AND \
D.airport_name = departure_airport AND A.airport_name = arrival_airport"
cursor.execute(query, (db_email,))
data1 = cursor.fetchall()
cursor.close()
return render_template('cushome.html', email=email, emailName=email.split('@')[0], view_my_flights=data1)
else:
session.clear()
return render_template('404.html')
@app.route('/cusSearchPurchase')
def cusSearchPurchase():
if session.get('email'):
email = session['email']
return render_template('cusSearchPurchase.html', email=email, emailName=email.split('@')[0])
else:
session.clear()
return render_template('404.html')
@app.route('/cusSpending', methods=['POST', 'GET'])
def cusSpending():
if session.get('email'):
email = session['email']
db_email = check_apostrophe(email)
# show total spending in the past period of time
duration = request.form.get("duration")
if duration is None:
duration = "365"
cursor = conn.cursor()
query_total = "SELECT sum(price)\
FROM customer_spending \
WHERE customer_email = %s AND (purchase_date between DATE_ADD(NOW(), INTERVAL -%s DAY) and NOW())"
cursor.execute(query_total, (db_email, duration))
total_spending_data = cursor.fetchone()
cursor.close()
# show month-wise spending in the past 6 months
period = request.form.get("period")
if period is None:
period = '6'
today = datetime.date.today()
past_day = today.day
past_month = (today.month - int(period)) % 12
if past_month == 0:
past_month = 12
past_year = today.year + ((today.month - int(period) - 1) // 12)
past_date = datetime.date(past_year, past_month, past_day) # the day 6 months ago
cursor = conn.cursor()
query_month = "SELECT year(purchase_date) AS year, month(purchase_date) AS month, sum(price) AS monthly_spending \
FROM customer_spending \
WHERE customer_email = %s AND purchase_date >= %s \
GROUP BY year(purchase_date), month(purchase_date)"
cursor.execute(query_month, (db_email, past_date))
monthly_spending_data = cursor.fetchall()
cursor.close()
months = []
monthly_spendings = []
for i in range(int(period)):
month = (past_date.month + i + 1) % 12
if month == 0:
month = 12
year = past_date.year + ((past_date.month + i) // 12)
flag = False
for one_month in monthly_spending_data:
if one_month[0] == year and one_month[1] == month:
flag = True
break
if flag:
monthly_spendings.append(int(one_month[2]))
else:
monthly_spendings.append(0)
months.append(str(year)+'-'+str(month))
return render_template('cusSpending.html', email=email, emailName=email.split('@')[0], total_spending_data=total_spending_data[0], duration=duration, period=period, months=months, monthly_spendings=monthly_spendings)
else:
session.clear()
return render_template('404.html')
@app.route('/cusSearchFlight', methods=['GET', 'POST'])
def cusSearchFlight():
if session.get('email'):
email = session['email']
db_email = check_apostrophe(email)
departure_city = check_apostrophe(request.form['departure_city'])
departure_airport = check_apostrophe(request.form['departure_airport'])
arrival_city = check_apostrophe(request.form['arrival_city'])
arrival_airport = check_apostrophe(request.form['arrival_airport'])
departure_date = request.form['departure_date']
arrival_date = request.form['arrival_date']
cursor = conn.cursor()
query = "SELECT airline_name, airplane_id, flight_num, D.airport_city, departure_airport, A.airport_city, arrival_airport, departure_time, arrival_time, price, status, num_tickets_left \
FROM airport AS D, flight, airport AS A \
WHERE (%s = '' OR D.airport_city = %s) AND \
D.airport_name = departure_airport AND \
(%s = '' OR departure_airport = %s) AND \
(%s = '' OR A.airport_city = %s) AND \
A.airport_name = arrival_airport AND \
(%s = '' OR arrival_airport = %s) AND \
(%s = '' OR date(departure_time) = %s) AND \
(%s = '' OR date(arrival_time) = %s) \
ORDER BY airline_name, flight_num"
cursor.execute(query, (departure_city, departure_city, departure_airport, departure_airport, arrival_city, arrival_city, arrival_airport, arrival_airport, departure_date, departure_date, arrival_date, arrival_date))
data = cursor.fetchall()
cursor.close()
if (data):
return render_template('cusSearchPurchase.html', email = email, emailName=email.split('@')[0], upcoming_flights=data)
else:
error = 'Sorry, we can\'t find any flight!'
return render_template('cusSearchPurchase.html', email = email, emailName=email.split('@')[0], error1=error)
else:
session.clear()
return render_template('404.html')
@app.route('/cusBuyTickets', methods=['GET', 'POST'])
def cusBuyTickets():
if session.get('email'):
email = session['email']
db_email = check_apostrophe(email)
airline_name = check_apostrophe(request.form['airline_name'])
flight_num = request.form['flight_num']
cursor = conn.cursor()
query = "SELECT * \
FROM flight \
WHERE airline_name = %s AND flight_num = %s AND num_tickets_left > 0"
cursor.execute(query, (airline_name, flight_num))
data = cursor.fetchall()
cursor.close()
if(data): # has ticket
cursor = conn.cursor()
# calculate the new ticket id
cursor = conn.cursor()
query_id = "SELECT ticket_id \
FROM ticket \
ORDER BY ticket_id DESC \
LIMIT 1"
cursor.execute(query_id)
ticket_id_data = cursor.fetchone() #
new_ticket_id = int(ticket_id_data[0]) + 1
# first insert into ticket
ins1 = "INSERT INTO ticket VALUES (%s, %s, %s)"
cursor.execute(ins1, (new_ticket_id, airline_name, flight_num))
# then insert into purchases
ins2 = "INSERT INTO purchases VALUES (%s, %s, NULL, CURDATE())"
cursor.execute(ins2, (new_ticket_id, db_email))
conn.commit()
cursor.close()
message1 = 'Ticket bought successfully!'
return render_template('cusSearchPurchase.html', email = email, message1 = message1)
else:
error = 'There\'re no ticket left...'
return render_template('cusSearchPurchase.html', error2=error, email = email, emailName=email.split('@')[0])
else:
session.clear()
return render_template('404.html')
#####################################################################
# BOOKING AGENT #
# all operations from the booking_agent side #
#####################################################################
@app.route('/agentlogin')
def agentlogin():
return render_template('agentlogin.html')
@app.route('/agentregister')
def agentregister():
return render_template('agentregister.html')
@app.route('/agentloginAuth', methods=['GET', 'POST'])
def agentloginAuth():
if "email" in request.form and 'password' in request.form:
email = request.form['email']
db_email = check_apostrophe(email)
password = request.form['password']
airline_name = request.form['airline_name']
airline = check_apostrophe(airline_name)
cursor = conn.cursor()
query = "SELECT * FROM booking_agent WHERE airline_name = %s AND email = %s AND password = md5(%s)"
cursor.execute(query, (airline, db_email, password))
data = cursor.fetchone()
cursor.close()
if(data):
cursor = conn.cursor()
query1 = "SELECT booking_agent_id FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query1, (airline, db_email))
data1 = cursor.fetchone()
query2 = "SELECT * FROM agent_view_flight WHERE airline_name = %s AND email = %s"
cursor.execute(query2, (airline, db_email))
data2 = cursor.fetchall()
cursor.close()
session['BA_email'] = email
session['BA_airline'] = airline
session['BA_id'] = data1
return render_template('agenthome.html', email=email, emailName=email.split('@')[0], view_my_flights=data2, booking_agent_id=data1)
else:
error = 'Error: Invalid email or password!'
return render_template('agentlogin.html', error=error)
else:
session.clear()
return render_template('404.html')
@app.route('/agentregisterAuth', methods=['GET', 'POST'])
def agentregisterAuth():
if "email" in request.form and 'password' in request.form and 'booking_agent_id' in request.form:
email = request.form['email']
db_email = check_apostrophe(email)
password = request.form['password']
booking_agent_id = request.form['booking_agent_id']
airline_name = request.form['airline_name']
airline = check_apostrophe(airline_name)
cursor = conn.cursor()
query = "SELECT * FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query, (airline, db_email))
data = cursor.fetchone()
if(data):
cursor.close()
error = "This user already exists!"
return render_template('agentregister.html', error = error)
else:
new = "INSERT INTO booking_agent VALUES(%s, md5(%s), %s, %s)"
cursor.execute(new, (db_email, password, booking_agent_id, airline))
conn.commit()
query1 = "SELECT booking_agent_id FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query1, (airline, db_email))
data1 = cursor.fetchone()
# get agent_view_flights info from db
query = "SELECT * FROM agent_view_flight WHERE airline_name = %s AND email = %s"
cursor.execute(query, (airline, db_email))
data2 = cursor.fetchall()
cursor.close()
session['BA_email'] = email
session['BA_airline'] = airline
flash("Welcome to FlyEasy!")
return render_template('agenthome.html', email=email, emailName=email.split('@')[0], view_my_flights=data2, booking_agent_id=data1)
else:
session.clear()
return render_template('404.html')
@app.route('/agentHome')
def agentHome():
if session.get('BA_email'):
email = session['BA_email']
db_email = check_apostrophe(email)
airline_name = session['BA_airline']
airline = check_apostrophe(airline_name)
cursor = conn.cursor()
query1 = "SELECT booking_agent_id FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query1, (airline, db_email))
data1 = cursor.fetchone()
query2 = "SELECT * FROM agent_view_flight WHERE airline_name = %s AND email = %s"
cursor.execute(query2, (airline, db_email))
data2 = cursor.fetchall()
cursor.close()
return render_template('agenthome.html', email=email, emailName=email.split('@')[0], view_my_flights=data2, booking_agent_id=data1)
else:
session.clear()
return render_template('404.html')
@app.route('/agentSearchPurchase')
def agentSearchPurchase():
if session.get('BA_email'):
email = session['BA_email']
return render_template('agentSearchPurchase.html', email=email, emailName=email.split('@')[0], )
else:
session.clear()
return render_template('404.html')
@app.route('/agentCommission', methods=['POST', 'GET'])
def agentCommission():
if session.get('BA_email'):
email = session['BA_email']
db_email = check_apostrophe(email)
airline_name = session['BA_airline']
airline = check_apostrophe(airline_name)
cursor = conn.cursor()
duration = request.form.get("duration")
if duration is None:
duration = "30"
query = 'SELECT SUM(ticket_price * 0.1), AVG(ticket_price * 0.1), COUNT(ticket_price * 0.1) \
FROM agent_commission \
WHERE airline_name = %s AND email = %s AND (purchase_date between DATE_ADD(NOW(), INTERVAL -%s DAY) and NOW())'
cursor.execute(query, (airline, db_email, duration))
commission_data = cursor.fetchone()
total_com, avg_com, count_ticket = commission_data
cursor.close()
return render_template('agentCommission.html', email=email, emailName=email.split('@')[0], total_com=total_com, avg_com=avg_com, count_ticket=count_ticket, duration=duration)
else:
session.clear()
return render_template('404.html')
@app.route('/agentTopCustomers')
def agentTopCustomers():
if session.get('BA_email'):
email = session['BA_email']
db_email = check_apostrophe(email)
airline_name = session['BA_airline']
airline = check_apostrophe(airline_name)
cursor = conn.cursor()
query = "SELECT customer_email, COUNT(ticket_id) \
FROM agent_commission \
WHERE airline_name = %s AND email = %s AND \
DATEDIFF(CURDATE(), DATE(purchase_date)) < 183 \
GROUP BY customer_email \
ORDER BY count(ticket_id) DESC"
cursor.execute(query, (airline, db_email))
ticket_data = cursor.fetchall()
cursor.close()
l = len(ticket_data)
if l >= 5:
ppl1 = [ticket_data[i][0] for i in range(5)]
tickets = [ticket_data[i][1] for i in range(5)]
else:
ppl1 = [ticket_data[i][0] for i in range(l)]
tickets = [ticket_data[i][1] for i in range(l)]
for i in range(5 - l):
ppl1.append(' ')
tickets.append(0)
cursor = conn.cursor()
query2 = "SELECT customer_email, SUM(ticket_price) * 0.1 \
FROM agent_commission \
WHERE airline_name = %s AND email = %s and \
DATEDIFF(CURDATE(), DATE(purchase_date)) < 365 \
GROUP BY customer_email \
ORDER BY sum(ticket_price) DESC"
cursor.execute(query2, (airline, db_email))
commission_data = cursor.fetchall()
cursor.close()
l2 = len(commission_data)
if l2 >= 5:
ppl2 = [commission_data[i][0] for i in range(5)]
commissions = [commission_data[i][1] for i in range(5)]
else:
ppl2 = [commission_data[i][0] for i in range(l2)]
commissions = [int(commission_data[i][1]) for i in range(l2)]
for i in range(5 - l):
ppl2.append(' ')
commissions.append(0)
return render_template('agentTopCustomers.html', email=email, emailName=email.split('@')[0], ppl1=ppl1, ppl2=ppl2, tickets=tickets, commissions=commissions)
else:
session.clear()
return render_template('404.html')
@app.route('/agentSearchFlight', methods=['GET', 'POST'])
def agentSearchFlight():
if session.get('BA_email'):
email = session['BA_email']
db_email = check_apostrophe(email)
airline_name = session['BA_airline']
airline = check_apostrophe(airline_name)
departure_city = check_apostrophe(request.form['departure_city'])
departure_airport = check_apostrophe(request.form['departure_airport'])
arrival_city = check_apostrophe(request.form['arrival_city'])
arrival_airport = check_apostrophe(request.form['arrival_airport'])
departure_date = request.form['departure_date']
arrival_date = request.form['arrival_date']
# validate booking agent email
cursor = conn.cursor()
query = "SELECT booking_agent_id FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query, (airline, db_email))
agent_data = cursor.fetchone()
booking_agent_id = agent_data[0]
cursor.close()
if not (agent_data):
agent_id_error = 'Sorry! You don\'t have the access.'
return render_template('agentSearchPurchase.html', error1=agent_id_error)
cursor = conn.cursor()
query = "SELECT airplane_id, flight_num, D.airport_city, departure_airport, A.airport_city, arrival_airport, \
departure_time, arrival_time, status, price, airline_name, num_tickets_left \
FROM airport AS D, flight, airport AS A \
WHERE airline_name = %s AND D.airport_city = if (%s = '',D.airport_city, %s) AND \
D.airport_name = departure_airport AND \
departure_airport = if (%s = '', departure_airport, %s) AND \
A.airport_city = if (%s = '', A.airport_city, %s) AND \
A.airport_name = arrival_airport AND \
arrival_airport = if (%s = '', arrival_airport, %s) AND \
date(departure_time) = if (%s = '', date(departure_time), %s) AND \
date(arrival_time) = if (%s = '', date(arrival_time), %s) \
ORDER BY airline_name, flight_num"
cursor.execute(query, (airline, departure_city, departure_city,departure_airport,departure_airport, arrival_city, arrival_city, arrival_airport, arrival_airport, departure_date, departure_date, arrival_date, arrival_date))
data = cursor.fetchall()
cursor.close()
if (data): # has data
return render_template('agentSearchPurchase.html', email=email, emailName=email.split('@')[0], upcoming_flights=data)
else: # does not have data
error = 'Sorry, we can\'t find any flight!'
return render_template('agentSearchPurchase.html', email=email, emailName=email.split('@')[0], error1=error)
else:
session.clear()
return render_template('404.html')
@app.route('/agentBuyTickets', methods=['GET', 'POST'])
def agentBuyTickets():
if session.get('BA_email'):
email = session['BA_email']
db_email = check_apostrophe(email)
airline_name = session['BA_airline']
airline = check_apostrophe(airline_name)
flight_num = request.form.get("flight_num")
customer_email = check_apostrophe(request.form['customer_email'])
# validate booking agent email
cursor = conn.cursor()
query = "SELECT booking_agent_id FROM booking_agent WHERE airline_name = %s AND email = %s"
cursor.execute(query, (airline, db_email))
agent_data = cursor.fetchone()
booking_agent_id = agent_data[0]
cursor.close()
if not (agent_data):
agent_id_error = 'Sorry! You don\'t have the access.'
return render_template('agentSearchPurchase.html', error1=agent_id_error)
# validate customer email
cursor = conn.cursor()
query = "SELECT * FROM customer WHERE email = %s"
cursor.execute(query, (customer_email,))
cus_data = cursor.fetchone()
cursor.close()
if not (cus_data):
email_error = 'Your customer is not registered.'
return render_template('agentSearchPurchase.html', error2=email_error)
cursor = conn.cursor()
query = "SELECT * \
FROM flight \
WHERE airline_name = %s AND flight_num = %s AND num_tickets_left > 0"
cursor.execute(query, (airline, flight_num))
flight_data = cursor.fetchall()
cursor.close()
if (flight_data): # has ticket
cursor = conn.cursor()
# calculate the new ticket id
cursor = conn.cursor()
query_id = "SELECT ticket_id \
FROM ticket \
ORDER BY ticket_id DESC \
LIMIT 1"
cursor.execute(query_id)
ticket_id_data = cursor.fetchone()
new_ticket_id = int(ticket_id_data[0]) + 1
# first insert into ticket
ins1 = "INSERT INTO ticket VALUES (%s, %s, %s)"
cursor.execute(ins1, (new_ticket_id, airline, flight_num))
# then insert into purchases
ins2 = "INSERT INTO purchases VALUES (%s, %s, %s, CURDATE())"
cursor.execute(ins2, (new_ticket_id, customer_email, booking_agent_id))
conn.commit()
cursor.close()
message = 'Ticket bought successfully!'
return render_template('agentSearchPurchase.html', message=message, email=email, emailName=email.split('@')[0])
else:
error = 'There\'re no ticket left...'
return render_template('agentSearchPurchase.html', error2=error, email=email, emailName=email.split('@')[0])
else:
session.clear()
return render_template('404.html')
#####################################################################
# AIRLINE STAFF #
# all operations from the airline_staff side #
#####################################################################
@app.route('/stafflogin')
def stafflogin():
return render_template('stafflogin.html')
# define route for register
@app.route('/staffregister')
def staffregister():
return render_template('staffregister.html')
# authenticate the login
@app.route('/staffloginAuth', methods=['GET', 'POST'])
def staffloginAuth():
if "username" in request.form and 'password' in request.form:
username = request.form['username']
db_username = check_apostrophe(username)
password = request.form['password']
cursor = conn.cursor()
query = "SELECT * FROM airline_staff WHERE username = %s and password = md5(%s)"
cursor.execute(query, (db_username, password))
data = cursor.fetchone()
query1 = "SELECT username, airline_name, airplane_id, flight_num, departure_airport, arrival_airport, departure_time, arrival_time FROM flight NATURAL JOIN airline_staff WHERE username = %s AND status = 'upcoming' AND DATEDIFF(CURDATE(), DATE(departure_time)) < 30 "
cursor.execute(query1, (db_username,))
data1 = cursor.fetchall()
cursor.close()
if (data):
session['username'] = username
return render_template('staffhome.html', username=username, posts=data1)
else:
error = 'Error: Invalid username or password!'
return render_template('stafflogin.html', error=error)
else:
session.clear()
return render_template('404.html')
# authenticate the register
@app.route('/staffregisterAuth', methods=['GET', 'POST'])
def staffregisterAuth():
if "username" in request.form and \
"password" in request.form and \
"first_name" in request.form and \
"last_name" in request.form and \
"date_of_birth" in request.form and \
"airline_name" in request.form and \
"isAdmin" in request.form and \
"isOperator" in request.form:
username = request.form['username']
db_username = check_apostrophe(username)
password = request.form['password']
first_name = request.form['first_name']
db_first_name = check_apostrophe(first_name)
last_name = request.form['last_name']
db_last_name = check_apostrophe(last_name)
date_of_birth = request.form['date_of_birth']
airline_name = check_apostrophe(request.form['airline_name'])
isAdmin = request.form['isAdmin']
isOperator = request.form['isOperator']
cursor = conn.cursor()
query = "SELECT * FROM airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data = cursor.fetchone()
if(data):
error = "This user already exists!"
return render_template('staffregister.html', error = error)
query = "SELECT airline_name FROM airline WHERE airline_name = %s"
cursor.execute(query, (airline_name,))
data = cursor.fetchone()
error = None
if (data): # airline exists
ins = "INSERT INTO airline_staff VALUES(%s, md5(%s), %s, %s, %s, %s, %s, %s)"
cursor.execute(ins, (db_username, password, db_first_name, db_last_name, date_of_birth, airline_name, isAdmin, isOperator))
conn.commit()
query1 = "SELECT username, airline_name, airplane_id, flight_num, departure_airport, arrival_airport, departure_time, arrival_time FROM flight NATURAL JOIN airline_staff WHERE username = %s AND status = 'upcoming' AND DATEDIFF(DATE(departure_time), CURDATE()) < 30 "
cursor.execute(query1, (db_username,))
data1 = cursor.fetchall()
cursor.close()
flash("Welcome to FlyEasy!")
session['username'] = username
return render_template('staffhome.html', username=username, posts = data1)
else:
error = "This airline doesn't exist!"
cursor.close()
return render_template('staffregister.html', error=error)
else:
session.clear()
return render_template('404.html')
@app.route('/staffhome')
def staffhome():
if session.get('username'):
username = session['username']
db_username = check_apostrophe(username)
cursor = conn.cursor()
query = "SELECT username, airline_name, airplane_id, flight_num, departure_airport, arrival_airport, departure_time, arrival_time \
FROM flight NATURAL JOIN airline_staff \
WHERE username = %s AND status = 'upcoming' AND DATEDIFF(CURDATE(), DATE(departure_time)) < 30 "
cursor.execute(query, (db_username,))
data1 = cursor.fetchall()
cursor.close()
return render_template('staffhome.html', username=username, posts=data1)
else:
session.clear()
return render_template('404.html')
@app.route('/staffflight')
def staffflight():
if session.get('username'):
username = session['username']
return render_template('staffflight.html', username=username)
else:
session.clear()
return render_template('404.html')
# only the operator can search and change the status of the flight
@app.route('/staffSearchFlight', methods=['GET', 'POST'])
def staffSearchFlight():
if session.get('username'):
departure_city = check_apostrophe(request.form['departure_city'])
departure_airport = check_apostrophe(request.form['departure_airport'])
arrival_city = check_apostrophe(request.form['arrival_city'])
arrival_airport = check_apostrophe(request.form['arrival_airport'])
departure_date = request.form['departure_date']
arrival_date = request.form['arrival_date']
username = session['username']
db_username = check_apostrophe(username)
cursor = conn.cursor()
query = "SELECT airline_name, airplane_id, flight_num, D.airport_city, departure_airport, A.airport_city, arrival_airport, departure_time, arrival_time, status, price \
FROM airport AS D, flight NATURAL JOIN airline_staff, airport AS A \
WHERE D.airport_city = if (%s = '',D.airport_city, %s) AND \
D.airport_name = departure_airport AND \
departure_airport = if (%s = '', departure_airport, %s) AND \
A.airport_city = if (%s = '', A.airport_city, %s) AND \
A.airport_name = arrival_airport AND \
arrival_airport = if (%s = '', arrival_airport, %s) AND \
date(departure_time) = if (%s = '', date(departure_time), %s) AND \
date(arrival_time) = if (%s = '', date(arrival_time), %s) AND \
username = %s \
ORDER BY airline_name, flight_num"
cursor.execute(query, (departure_city, departure_city,departure_airport,departure_airport, arrival_city, arrival_city, arrival_airport, arrival_airport, departure_date, departure_date, arrival_date,arrival_date, db_username))
data = cursor.fetchall()
# validate staff permission
query = "SELECT isOperator FROM airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
access = cursor.fetchone()
cursor.close()
if access[0] == '0':
error = "Sorry! You don\'t have the access."
return render_template('staffflight.html', username=username, error1=error)
else:
if (data): # has data
return render_template('staffflight.html', username=username, upcoming_flights=data)
else:
error = 'Sorry, we can\'t find any flight!'
return render_template('staffflight.html', username=username, error1=error)
else:
session.clear()
return render_template('404.html')
@app.route('/edit_status', methods=['GET', 'POST'])
def edit_status():
if session.get('username'):
username = session['username']
status = request.form['edit_status']
flight_num = request.form['flight_num']
cursor = conn.cursor()
upd = "UPDATE flight SET status = %s WHERE flight_num = %s"
cursor.execute(upd, (status, flight_num))
conn.commit()
cursor.close()
message = 'Status changed successfully!'
return render_template('staffflight.html', username=username, message = message)
else:
session.clear()
return render_template('404.html')
@app.route('/staffaddinfo')
def staffaddinfo():
if session.get('username'):
username = session['username']
db_username = check_apostrophe(username)
cursor = conn.cursor()
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data = cursor.fetchall()
cursor.close()
return render_template('staffaddinfo.html', username=username, airplane = data)
else:
session.clear()
return render_template('404.html')
@app.route('/add_flight', methods=['GET', 'POST'])
def add_flight():
if session.get('username'):
username = session['username']
db_username = check_apostrophe(username)
flight_num = request.form['flight_num']
departure_airport = check_apostrophe(request.form['departure_airport'])
departure_date = request.form['departure_date']
departure_time = request.form['departure_time']
arrival_airport = check_apostrophe(request.form['arrival_airport'])
arrival_date = request.form['arrival_date']
arrival_time = request.form['arrival_time']
price = request.form['price']
number = request.form['number']
status = request.form['status']
airplane_id = request.form['airplane_id']
# validate staff permission
cursor = conn.cursor()
query = "SELECT isAdmin FROM airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
access = cursor.fetchone()
cursor.close()
if access[0] == '0':
error = "Sorry! You don\'t have the access."
return render_template('staffaddinfo.html', username=username, error1=error)
cursor = conn.cursor()
airline = "SELECT airline_name FROM airline_staff WHERE username = %s"
cursor.execute(airline, (db_username,))
airline_name = cursor.fetchone()
airline_name = airline_name[0]
# check departure airport
query = "SELECT airport_name FROM airport WHERE airport_name = %s"
cursor.execute(query, (departure_airport,))
data = cursor.fetchall()
error1 = None
if not (data):
error1 = "Departure airport doesn't exist."
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data1 = cursor.fetchall()
return render_template('staffaddinfo.html', error1 = error1, username=username, airplane = data1)
# check arrival airport
query = "SELECT airport_name FROM airport WHERE airport_name = %s"
cursor.execute(query, (arrival_airport,))
data = cursor.fetchall()
error1 = None
if not (data):
error1 = "Arrival airport doesn't exist."
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data1 = cursor.fetchall()
return render_template('staffaddinfo.html', error1 = error1, username=username, airplane = data1)
# check airplane type
query = "SELECT airplane_id FROM airplane WHERE airline_name = %s AND airplane_id = %s"
cursor.execute(query, (airline_name, airplane_id))
data = cursor.fetchall()
error1 = None
if not (data):
error1 = "Airplane doesn't exist."
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data1 = cursor.fetchall()
return render_template('staffaddinfo.html', error1 = error1, username=username, airplane = data1)
# check number of seats
num = "SELECT seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s AND airplane_id = %s"
cursor.execute(num, (db_username, airplane_id))
num = cursor.fetchone()
numerror = None
if int(number) > int(num[0]):
numerror = "Not enough seats."
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"
cursor.execute(query, (db_username,))
data1 = cursor.fetchall()
return render_template('staffaddinfo.html', error1 = numerror, username=username, airplane = data1)
# check flight number
num = "SELECT airline_name, flight_num FROM flight WHERE airline_name = %s AND flight_num = %s"
cursor.execute(num, (airline_name, flight_num))
data = cursor.fetchone()
error1 = None
if (data):
error1 = "Flight already exists."
query = "SELECT airplane_id, seats FROM airplane NATURAL JOIN airline_staff WHERE username = %s"