forked from itucsdb1813/itucsdb1813
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1157 lines (1070 loc) · 46.8 KB
/
server.py
File metadata and controls
1157 lines (1070 loc) · 46.8 KB
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, redirect, url_for, request, session, flash, render_template
import datetime
import psycopg2 as dbapi2
import mailsender
from werkzeug.utils import secure_filename
from forms import *
import decimal
from dbinit import INIT_STATEMENTS
from base64 import b64encode
app = Flask(__name__)
app.debug = True
app.secret_key = "pnBM(@?&#p]l~eI%L&$@#9f)T^uK7U"
WTF_CSRF_SECRET_KEY = "N4<*$83/[[{)ZO&X2yL_qN68+{;;Xo"
dsn = """user='ddzwibxvysqwgx' password='9e0edae8756536ffdba78314ebde69e2d019e58a2c05dfbad508b5eb657ac9e7'
host='ec2-54-247-101-205.eu-west-1.compute.amazonaws.com' port=5432 dbname='d8o6dthnk5anke'"""
#Sercan
@app.route('/')
def index():
refreshUserData()
_Datetime = datetime.datetime.now()
date = _Datetime.strftime("%Y-%m-%d")
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT city FROM cities
ORDER BY city
"""
cursor.execute(statement)
cities = cursor.fetchall()
statement = """SELECT postid, fullname, content, date, title, img.data, img.filename FROM posts
INNER JOIN person ON poster = username
LEFT OUTER JOIN uploads as img ON posts.image = img.id
ORDER BY postid DESC
LIMIT 6
"""
cursor.execute(statement)
posts = cursor.fetchall()
statement = """SELECT DISTINCT t.rate, t.flight_id, c.city, c2.city FROM tickets AS t
INNER JOIN flights AS f ON f.flight_id = t.flight_id
INNER JOIN airports AS a1 ON a1.airport_id = f.departure_id
INNER JOIN airports AS a2 ON a2.airport_id = f.destination_id
INNER JOIN cities AS c ON c.city_id = a1.city_id
INNER JOIN cities AS c2 ON c2.city_id = a2.city_id
WHERE t.rate < 1
ORDER BY t.rate ASC
LIMIT 10
"""
cursor.execute(statement)
discount = cursor.fetchall()
images = {}
for post in posts:
images[post[0]] = b64encode(post[5]).decode('utf-8')
return RenderTemplate('index.html', cities=cities, date=date, discount=discount, homeActive='active', posts=posts, images=images)
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
@app.route("/searchList", methods = ['GET', 'POST'])
def searchList():
departure = request.form['from']
destination = request.form['to']
departure_time = request.form['date']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name, c2.city, f.departure_time, f.arrival_time FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
WHERE c.city = %s AND c2.city = %s AND f.departure_time::text LIKE %s"""
departure_time += '%'
cursor.execute(statement, (departure, destination, departure_time))
rows = cursor.fetchall()
return RenderTemplate('flights.html', flights=rows, flightsActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
@app.route("/flights")
def flights():
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name, c2.city, f.departure_time, f.arrival_time FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
"""
cursor.execute(statement)
rows = cursor.fetchall()
return RenderTemplate('flights.html', flights=rows, flightsActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
@app.route("/roundFlight", methods = ['GET', 'POST'])
def roundFlight():
departure = request.form['from']
destination = request.form['to']
departure_time = request.form['date']
departure_time2 = request.form['date2']
destination2 = request.form['from']
departure2 = request.form['to']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name, c2.city, f.departure_time, f.arrival_time FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
WHERE c.city = %s AND c2.city = %s AND f.departure_time::text LIKE %s"""
departure_time += '%'
cursor.execute(statement, (departure, destination, departure_time))
rows = cursor.fetchall()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name, c2.city, f.departure_time, f.arrival_time FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
WHERE c.city = %s AND c2.city = %s AND f.departure_time::text LIKE %s"""
departure_time2 += '%'
cursor.execute(statement, (departure2, destination2, departure_time2))
rows2 = cursor.fetchall()
return RenderTemplate('roundFlight.html', flights=rows, flightss=rows2, flightsActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
@app.route("/addPlane", methods = ['GET', 'POST'])
def addPlane():
refreshUserData()
if ifAdmin():
if request.method == 'POST':
planeid = request.form['planeId']
planemodel = request.form['planeModel']
bsncap = request.form['bsnCap']
ecocap = request.form['ecoCap']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ INSERT INTO planes (plane_id, plane_model, bsn_capacity, eco_capacity)
VALUES (%s, %s,%s,%s)
"""
cursor.execute(statement, (planeid, planemodel, bsncap, ecocap))
connection.commit()
flash('You have succesfully added a plane.')
return RenderTemplate('addPlane.html', adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata2!"
finally:
connection.close()
else:
return RenderTemplate('addPlane.html')
else:
return redirect(url_for('errorpage', message='Not Authorized!'))
@app.route("/add_city", methods = ['GET', 'POST'])
def add_city():
refreshUserData()
if ifAdmin():
if request.method == 'POST':
city_id = request.form['city_id']
city = request.form['city']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ INSERT INTO cities (city_id, city)
VALUES (%s, %s)
"""
cursor.execute(statement, (city_id, city))
connection.commit()
flash('You have succesfully added a city.')
return RenderTemplate('addCity.html', adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata2!"
finally:
connection.close()
else:
return RenderTemplate('addCity.html')
else:
return redirect(url_for('errorpage', message='Not Authorized!'))
@app.route("/add_airport", methods = ['GET', 'POST'])
def add_airport():
refreshUserData()
if ifAdmin():
if request.method == 'POST':
airport_id = request.form['airport_id']
airport_name = request.form['airport_name']
city_id = request.form['city_id']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ INSERT INTO airports (airport_id, airport_name, city_id)
VALUES (%s, %s, %s)
"""
cursor.execute(statement, (airport_id, airport_name, city_id))
connection.commit()
flash('You have succesfully added a airport.')
return RenderTemplate('addAirport.html', adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata2!"
finally:
connection.close()
else:
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT * FROM cities
"""
cursor.execute(statement)
rows = cursor.fetchall()
return RenderTemplate('addAirport.html', cities=rows, adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata2!"
finally:
connection.close()
else:
return redirect(url_for('errorpage', message='Not Authorized!'))
@app.route("/discount", methods = ['GET', 'POST'])
def discount():
if ifAdmin():
refreshUserData()
if request.method == 'GET':
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name, c2.city, f.departure_time, f.arrival_time FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
"""
cursor.execute(statement)
rows = cursor.fetchall()
return RenderTemplate('discount.html', flights=rows, flightsActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else :
try:
_id = request.form['id']
_discount = request.form['discount_rate']
rate = 1 - float(_discount)/100
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ UPDATE tickets SET rate = %s
WHERE flight_id = %s
"""
cursor.execute(statement, (rate, _id))
connection.commit()
statement = """ UPDATE tickets SET price = base_price*rate
WHERE flight_id = %s
""" % _id
cursor.execute(statement)
connection.commit()
flash('Discount has been set')
return RenderTemplate('discount.html', adminActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'Not Authorized!'))
#Enes
@app.route("/login", methods = ['POST'])
def login():
_Username = request.form['username']
_Password = request.form['password']
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT * FROM users WHERE username = '%s'
""" % _Username
cursor.execute(statement)
if cursor.rowcount > 0:
password = cursor.fetchone()[1]
if password == _Password:
session['online'] = 1
session['Username'] = _Username
statement = """SELECT * FROM person WHERE username = '%s'
""" % _Username
cursor.execute(statement)
row = cursor.fetchone()
session['Fullname'] = row[1]
session['Email'] = row[2]
session['Role'] = row[3]
session['Balance'] = str(row[4])
if ifAdmin():
return redirect(url_for('adminpage'))
else:
return redirect(url_for('userpage'))
return redirect(url_for('errorpage', message = 'Wrong username or password!'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
#Enes
@app.route("/register", methods = ['POST'])
def register():
form = formRegister()
_Username = form.username.data
_Password = form.password.data
_Fullname = form.name.data
_Email = form.email.data
if form.validate_on_submit():
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT * FROM users WHERE username = '%s'
""" % _Username
cursor.execute(statement)
if cursor.rowcount > 0:
return redirect(url_for('errorpage', message = 'This username already exists!'))
statement = """INSERT INTO users (username, password) VALUES (%s, %s)
"""
cursor.execute(statement, (_Username, _Password))
statement = """INSERT INTO person (username, fullname, emailaddress, userrole) VALUES (%s, %s, %s, %s)
"""
cursor.execute(statement, (_Username, _Fullname, _Email, 'P'))
connection.commit()
session['online'] = 1
session['Username'] = _Username
session['Fullname'] = _Fullname
session['Email'] = _Email
session['Balance'] = 0
flash('You have been succesfully registered.')
return redirect(url_for('userpage'))
for error in form.errors:
flash(form.errors[error][0])
return redirect(url_for('index'))
#Enes
@app.route("/userpage")
def userpage():
if 'online' in session:
refreshUserData()
return RenderTemplate('userpage.html', profileActive='active')
else:
return redirect(url_for('errorpage', message = 'You need to log in first!'))
#Enes
@app.route("/logout")
def logout():
if session['online'] == 1:
session.clear()
return redirect(url_for('index'))
else:
return redirect(url_for('errorpage', message = 'You need to log in first!'))
#Enes
@app.route("/buycoins", methods = ['GET', 'POST'])
def buycoins():
if request.method == 'GET':
return RenderTemplate('buycoins.html', coinActive='active')
elif request.method == 'POST':
amount = request.form['amount']
refreshUserData()
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """INSERT INTO payments(username, amount) VALUES(%s, %s)
"""
cursor.execute(statement, (session['Username'], amount))
connection.commit()
flash('Payment request has been sent to admins.')
return redirect(url_for('userpage'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
#Enes
@app.route("/edituser", methods = ['GET', 'POST'])
def edituser():
if refreshUserData():
form = formEditUser()
if request.method == 'GET':
return RenderTemplate('edituser.html', form = form, fullname=session['Fullname'], email=session['Email'])
else:
if form.validate_on_submit():
newFullname = form.fullname.data
newEmail = form.email.data
newPassword = form.password.data
try:
if newFullname == '':
newFullname = session['Fullname']
if newEmail == '':
newEmail = session['Email']
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """
UPDATE person SET fullname = %s, emailaddress = %s WHERE username = %s
"""
cursor.execute(statement, (newFullname, newEmail, session['Username']))
if newPassword != '':
statement = """
UPDATE users SET password = %s WHERE username = %s
"""
cursor.execute(statement, (newPassword, session['Username']))
connection.commit()
flash('You have successfully updated your profile')
return redirect(url_for('userpage'))
except dbapi2.DatabaseError as e:
connection.rollback()
return redirect(url_for('error_page', str(e)))
finally:
connection.close()
#Enes
@app.route("/forgotpassword", methods = ['GET', 'POST'])
def forgotpassword():
form = formForgotPass()
refreshUserData()
if 'Username' in session:
return redirect(url_for('errorpage'), message = 'Not allowed!')
if request.method == 'GET':
return RenderTemplate('forgotpassword.html', form=form)
else:
if form.validate_on_submit():
username = form.username.data
refreshUserData()
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT p.emailaddress, u.password FROM person AS p
INNER JOIN users AS u ON u.username = p.username
WHERE p.username = '%s'
""" % username
cursor.execute(statement)
row = cursor.fetchone()
if row:
mailsender.sendMail(row[1], row[0])
flash('Your password has been sent to your email address. Please check your inbox.')
return RenderTemplate('forgotpassword.html', form=form)
else:
flash('This username is not registered to our website!')
return RenderTemplate('forgotpassword.html', form=form)
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
for error in form.errors:
flash(form.errors[error][0])
return RenderTemplate('forgotpassword.html', form=form)
#Enes
def RenderTemplate(template, **context):
context['registerForm'] = formRegister()
return render_template(template, **context)
@app.route('/errorpage/<message>')
def errorpage(message):
return RenderTemplate('errorpage.html', message = message)
#Enes
@app.route('/about')
def about():
return RenderTemplate('about.html', aboutActive='active')
#Enes
@app.route('/news')
def news():
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT p.postid, f.fullname, p.content, p.date, p.time, f.userrole, p.title, img.filename, img.data FROM posts as p
LEFT OUTER JOIN person as f ON f.username = p.poster
LEFT OUTER JOIN uploads as img ON p.image = img.id
ORDER BY p.postid DESC
"""
cursor.execute(statement)
posts = cursor.fetchall()
images = {}
for post in posts:
images[post[0]] = b64encode(post[8]).decode('utf-8')
return RenderTemplate('news.html', posts = posts, newsActive='active', images = images)
#Enes
def ifAdmin():
if 'Username' in session:
_Refreshed = refreshUserData()
if _Refreshed:
if session['Role'] == 'A':
return True
else:
return _Refreshed
return False
#Enes
def refreshUserData():
if 'Username' in session:
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT * FROM person WHERE username = '%s'
""" % session['Username']
cursor.execute(statement)
row = cursor.fetchone()
session['Fullname'] = row[1]
session['Email'] = row[2]
session['Role'] = row[3]
session['Balance'] = str(row[4])
return True
except dbapi2.DatabaseError as e:
connection.rollback()
return e
finally:
connection.close()
else:
return False
#Enes
@app.route("/adm_sendpost", methods = ['GET', 'POST'])
def adm_sendpost():
refreshUserData()
if ifAdmin():
form = formSendPost()
if request.method == 'POST':
if form.validate_on_submit():
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
image = form.image.data
filename = secure_filename(image.filename)
statement = """INSERT INTO uploads (filename, data) VALUES (%s, %s)
"""
cursor.execute(statement, (filename, dbapi2.Binary(image.read())))
connection.commit()
statement = """SELECT MAX(id) FROM uploads
"""
cursor.execute(statement, (filename, dbapi2.Binary(image.read())))
id = cursor.fetchone()
_Datetime = datetime.datetime.now()
poster = session['Username']
content = form.content.data
title = form.title.data
date = _Datetime.strftime("%d/%m/%Y")
time = _Datetime.strftime("%H:%M")
statement = """INSERT INTO posts (poster, content, date, time, title, image) VALUES (%s, %s, TO_DATE(%s, 'DD/MM/YYYY'), %s, %s, %s)
"""
cursor.execute(statement, (poster, content, date, time, title, id))
connection.commit()
flash('You have succesfully posted an entry.')
return redirect(url_for('news'))
for key in form.errors:
flash(form.errors[key][0])
return RenderTemplate('adm_sendpost.html', adminActive='active', form=form)
else:
return redirect(url_for('errorpage', message = 'Not Authorized!'))
#Enes
@app.route("/adm_pymreqs", methods = ['GET', 'POST'])
def adm_pymreqs():
refreshUserData()
if ifAdmin():
if request.method == 'GET':
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT paymentid, username, amount FROM payments WHERE approved = '0'
ORDER BY paymentid ASC
"""
cursor.execute(statement)
payments = cursor.fetchall()
return RenderTemplate('adm_pymreqs.html', payments = payments, adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata!"
finally:
connection.close()
return RenderTemplate('adm_pymreqs.html', adminActive='active')
elif request.method == 'POST':
try:
for key in request.form.keys():
pymId = key[3:]
if request.form[key]:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """UPDATE payments SET approved = %s, approved_by = %s WHERE paymentid = %s
"""
cursor.execute(statement, ('1', session['Username'], pymId))
##statement = """SELECT amount FROM payments WHERE paymentid = %s
##"""
##cursor.execute(statement, (pymId))
##amount = cursor.fetchone()
statement = """UPDATE person
SET balance = t1.balance + t2.amount
FROM person as t1
INNER JOIN payments as t2 ON t2.username = t1.username
WHERE t2.paymentid = %s
AND person.username = t2.username;
""" % pymId
cursor.execute(statement)
connection.commit()
flash('Approved requests')
return redirect(url_for('adm_pymreqs'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
#Enes
@app.route("/deleteuser/<username>", methods = ['POST'])
def deleteuser(username):
if ifAdmin():
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """DELETE FROM users WHERE username = '%s'
""" % username
cursor.execute(statement)
statement = """DELETE FROM person WHERE username = '%s'
""" % username
cursor.execute(statement)
connection.commit()
flash('You have succesfully deleted a user.')
return RenderTemplate('adm_users.html', adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata!"
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'Not Authorized!'))
#Enes
@app.route("/adminpage")
def adminpage():
if ifAdmin():
return RenderTemplate('adminpage.html', adminActive='active')
else:
return redirect(url_for('errorpage', message = 'You are not authorized!'))
#Enes
@app.route("/adm_users")
def adm_users():
if ifAdmin():
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
username = session['Username']
statement = """SELECT * FROM person WHERE username <> '%s'
""" % username
cursor.execute(statement)
rows = cursor.fetchall()
return RenderTemplate('adm_users.html', userlist = rows, adminActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return e
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'You are not authorized!'))
#Enes
@app.route("/adm_updateuser/<username>")
def updateuser(username):
if ifAdmin():
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT * FROM person WHERE username = '%s'
""" % username
cursor.execute(statement)
row = cursor.fetchone()
return RenderTemplate('adm_updateuser.html', user = row, adminActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return e
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'You are not authorized!'))
#Enes
@app.route("/adm_users/<username>", methods = ['POST'])
def adm_updateuser(username):
if ifAdmin():
try:
form_dict = request.form
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """UPDATE person SET
"""
set_text = ""
if 'fname_cb' in form_dict and form_dict['fname_cb']:
set_text += ("fullname = '%s'" % form_dict['fullname']) + ','
if 'mail_cb' in form_dict and form_dict['mail_cb']:
set_text += ("emailaddress = '%s'" % form_dict['mail']) + ','
if 'role_cb' in form_dict and form_dict['role_cb']:
set_text += ("userrole = '%s'" % form_dict['role']) + ','
if 'balance_cb' in form_dict and form_dict['balance_cb']:
set_text += ("balance = '%s'" % form_dict['balance']) + ','
if len(set_text) > 0:
set_text = set_text[:-1]
statement += set_text
statement += " WHERE username = '%s'" % username
else:
return redirect(url_for('adm_users'))
cursor.execute(statement)
connection.commit()
flash('You have succesfully updated a user.')
return redirect(url_for('adm_users'))
except dbapi2.DatabaseError as e:
connection.rollback()
return e
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'You are not authorized!'))
#Sercan
@app.route("/adm_updateflight", methods = ['GET', 'POST'])
def adm_updateflight():
refreshUserData()
if ifAdmin():
if request.method == 'GET' :
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ SELECT airport_name, city, airport_id FROM airports AS a
INNER JOIN cities AS c ON a.city_id = c.city_id
ORDER BY city
"""
cursor.execute(statement)
rows = cursor.fetchall()
statement = """ SELECT plane_id, plane_model, bsn_capacity, eco_capacity FROM planes AS p
ORDER BY plane_id
"""
cursor.execute(statement)
plane = cursor.fetchall()
return RenderTemplate('adm_updateflight.html', cities=rows, planes=plane, adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata1!"
finally:
connection.close()
else :
try:
_from = request.form['from']
_to = request.form['to']
_on = request.form['on']
_arr_date = request.form['arr_date']
_dep_date = request.form['dep_date']
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ INSERT INTO flights (destination_id, plane_id, departure_time, arrival_time, departure_id)
VALUES (%s, %s,%s,%s,%s)
"""
cursor.execute(statement, (_to, _on, _dep_date, _arr_date, _from))
connection.commit()
statement = """ SELECT MAX(flight_id) FROM flights
"""
cursor.execute(statement)
flight = cursor.fetchone()
create_tickets(flight, 100)
return RenderTemplate('adm_updateflight.html', adminActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata2!"
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'Not Authorized!'))
#Sercan
@app.route("/adm_deleteflight", methods = ['GET', 'POST'])
def adm_deleteflight():
if ifAdmin():
refreshUserData()
if request.method == 'GET':
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT f.flight_id,a.airport_name, c.city, a2.airport_name,
c2.city, f.departure_time, f.arrival_time
FROM flights AS f
INNER JOIN airports AS a ON f.departure_id = a.airport_id
INNER JOIN airports AS a2 ON f.destination_id = a2.airport_id
INNER JOIN planes AS p ON f.plane_id = p.plane_id
INNER JOIN cities AS c ON a.city_id = c.city_id
INNER JOIN cities AS c2 ON a2.city_id = c2.city_id
"""
cursor.execute(statement)
rows = cursor.fetchall()
return RenderTemplate('adm_deleteflight.html', flights=rows, flightsActive='active')
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else:
try:
_id = request.form['id']
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """ DELETE FROM tickets
WHERE flight_id = %s
""" % _id
cursor.execute(statement)
connection.commit()
statement = """ DELETE FROM flights
WHERE flight_id = %s
""" % _id
cursor.execute(statement)
connection.commit()
flash('You have successfully deleted a flight.')
return redirect(url_for('flights'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else:
return redirect(url_for('errorpage', message = 'Not Authorized!'))
#Enes
@app.route("/adm_fabrika_ayarlari")
def adm_fabrika_ayarlari():
if ifAdmin():
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
DROP_STATEMENTS = [ """DROP TABLE tickets"""
, """DROP TABLE flights"""
, """DROP TABLE airports"""
, """DROP TABLE cities"""
, """DROP TABLE planes"""
, """DROP TABLE posts"""
, """DROP TABLE payments"""
, """DROP TABLE person"""
, """DROP TABLE uploads"""
, """DROP TABLE users""" ]
for statement in DROP_STATEMENTS:
cursor.execute(statement)
connection.commit()
for statement in INIT_STATEMENTS:
cursor.execute(statement)
connection.commit()
return redirect(url_for('adminpage'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else:
return redirect(url_for('errorpage', message='Not Authorized!'))
#Said
@app.route("/buy_ticket/<int:flight_id>", methods = ['GET', 'POST'])
def buy_ticket(flight_id):
if 'Username' in session:
refreshUserData()
if request.method == 'GET':
try:
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT class, COUNT(*) FROM tickets WHERE flight_id = '%s' AND username IS NULL
GROUP BY class
""" % flight_id
cursor.execute(statement)
rows = cursor.fetchall()
emptyseatsforeco = 0
emptyseatsforbsn = 0
for row in rows:
if row[0] == 'E':
emptyseatsforeco = row[1]
elif row[0] == 'B':
emptyseatsforbsn = row[1]
statement = """SELECT class, MIN(price) FROM tickets WHERE flight_id = '%s' AND username IS NULL
GROUP BY class
""" % flight_id
cursor.execute(statement)
rows = cursor.fetchall()
priceforeco = 0
priceforbsn = 0
for row in rows:
if row[0] == 'E':
priceforeco = row[1]
elif row[0] == 'B':
priceforbsn = row[1]
return RenderTemplate('buy_ticket.html', flightid = flight_id, balance = session['Balance'], emptyseatsforeco = emptyseatsforeco, emptyseatsforbsn =emptyseatsforbsn, priceforeco = priceforeco, priceforbsn = priceforbsn, flightsActive='active')
except dbapi2.DatabaseError:
connection.rollback()
return "Hata!"
finally:
connection.close()
elif request.method == 'POST':
try:
classtype = request.form['class']
session['Username']
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
statement = """SELECT ticket_id, price from tickets
WHERE ticket_id = (SELECT MIN(ticket_id)
FROM tickets WHERE flight_id = %s AND username IS NULL AND class = %s)
AND flight_id = %s
"""
cursor.execute(statement, (flight_id, classtype, flight_id))
row = cursor.fetchone()
ticketid = row[0]
ticketprice = row[1]
if ticketprice <= decimal.Decimal(session['Balance']):
statement = """UPDATE tickets
SET username = %s
WHERE ticket_id = %s
AND flight_id = %s
"""
cursor.execute(statement, (session['Username'], ticketid, flight_id))
statement = """UPDATE person
SET balance = balance-%s
WHERE username = %s
"""
cursor.execute(statement,(ticketprice, session['Username']))
connection.commit()
refreshUserData()
flash('Ticket has been bought successfully. Your new balance is %s SCoins' % session['Balance'])
return redirect(url_for('index'))
else:
flash('Your balance is not enough!')
return redirect(url_for('buycoins'))
except dbapi2.DatabaseError as e:
connection.rollback()
return str(e)
finally:
connection.close()
else: