-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
204 lines (171 loc) · 7.12 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
from flask import *
import pyrebase
from datetime import *
import pytz
from flask_qrcode import QRcode
from dotenv import load_dotenv
from os import getenv
load_dotenv()
app = Flask(__name__, static_url_path='/static')
QRcode(app)
firebaseConfig = {
'apiKey': getenv('apiKey'),
'authDomain': getenv('authDomain'),
'databaseURL': getenv('databaseURL'),
'projectId': getenv('projectId'),
'storageBucket': getenv('storageBucket'),
'messagingSenderId': getenv('messagingSenderId'),
'appId': getenv('appId'),
'measurementId': getenv('measurementId')
}
firebase = pyrebase.initialize_app(firebaseConfig)
db = firebase.database()
tz = pytz.timezone(getenv('timezone'))
def sortby_deptTime(data, cmd):
x = []
for d in data:
data[d]['key'] = d
data[d]['rowNum'] = int(data[d]['rowNum'])
data[d]['colNum'] = int(data[d]['colNum'])
if not cmd(data[d]):
continue
if (x == []):
x.append(data[d])
continue
tmp = True
for i in x:
iT = datetime(int(i['year']), int(i['month']), int(i['day']),
int(i['hour']), int(i['minute']), 0, 0, tzinfo=tz)
dT = datetime(int(data[d]['year']), int(data[d]['month']), int(data[d]['day']),
int(data[d]['hour']), int(data[d]['minute']), 0, 0, tzinfo=tz)
if (iT >= dT):
x.insert(x.index(i), data[d])
tmp = False
break
if tmp == True:
x.append(data[d])
return x
def later_than_now(x):
now = datetime.now(tz)
xT = datetime(int(x['year']), int(x['month']), int(x['day']),
int(x['hour']), int(x['minute']), 0, 0, tzinfo=tz)
return (xT >= now)
@app.route('/', methods=['GET'])
def root():
data = db.child("Flights").get()
if data.val() == None:
return render_template('selflight.html', flights=[])
data = sortby_deptTime(data.val(), lambda x: later_than_now(x))
return render_template('selflight.html', flights=data)
@app.route('/filter/<fmethod>', methods=['GET'])
def filterpg(fmethod):
data = db.child("Flights").get()
if data is None:
return render_template('index.html', flights=data)
if fmethod == 'today':
data = sortby_deptTime(data.val(), lambda x: (int(x['year']) == datetime.now(tz).year) and (
int(x['month']) == datetime.now(tz).month) and (int(x['day']) == datetime.now(tz).day))
elif fmethod == 'departed':
data = sortby_deptTime(data.val(), lambda x: x['status'] == 'Departed')
elif fmethod == 'ticketing':
data = sortby_deptTime(
data.val(), lambda x: x['status'] == 'Ticketing' and later_than_now(x))
elif fmethod == 'boarding':
data = sortby_deptTime(
data.val(), lambda x: x['status'] == 'Boarding')
elif fmethod == 'all':
data = sortby_deptTime(data.val(), lambda x: True)
return render_template('selflight.html', flights=data)
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
return render_template('addflight.html', now=datetime.now(tz))
elif request.method == 'POST':
flight = request.form.to_dict()
flight['datetime'] = datetime(int(flight['year']), int(flight['month']), int(flight['day']),
int(flight['hour']), int(flight['minute']), 0, 0, tzinfo=tz).strftime("%Y/%m/%d %H:%M:%S")
flight['status'] = 'Ticketing'
flight['Seats'] = {}
for i in range(int(flight['colNum'])):
flight['Seats'][i] = {}
for j in range(int(flight['rowNum'])):
flight['Seats'][i][j] = {'status': 'Available'}
db.child("Flights").push(flight)
return redirect('/')
else:
return "ERROR"
@app.route('/manage/<id>', methods=['GET'])
def manage(id):
data = db.child("Flights").child(id).get().val()
data['rowNum'] = int(data['rowNum'])
data['colNum'] = int(data['colNum'])
data['key'] = id
return render_template('manage.html', flight=data)
@app.route('/manage/<id>/delete', methods=['GET'])
def delflight(id):
db.child("Flights").child(id).remove()
return redirect('/')
@app.route('/manage/<id>/cstatus', methods=['POST'])
def cstatus(id):
data = request.get_json(force=True)
db.child("Flights").child(id).child(
'status').set(data['status'])
return redirect('/manage/'+id)
@app.route('/manage/<id>/seat/<col>/<row>', methods=['GET'])
def manageseat(id, col, row):
col = int(col)
row = int(row)
data = db.child("Flights").child(id).get().val()
data['key'] = id
if (data['Seats'][col][row]['status']) == 'Available':
return render_template('book.html', flight=data, col=col, row=row, edit=False)
elif (data['Seats'][col][row]['status']) == 'Booked':
return render_template('checkin.html', flight=data, col=col, row=row, checked=False)
elif (data['Seats'][col][row]['status']) == 'Checked_In':
return render_template('checkin.html', flight=data, col=col, row=row, checked=True)
else:
return "ERROR"
@app.route('/manage/<id>/seat/<col>/<row>/book', methods=['POST'])
def bookseat(id, col, row):
col = int(col)
row = int(row)
try:
if request.form['paid'] == 'on':
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('firstN').set(request.form['firstN'])
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('lastN').set(request.form['lastN'])
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('nationality').set(request.form['nationality'])
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('passportN').set(request.form['passportN'])
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('status').set('Booked')
return redirect('/manage/'+str(id)+'/seat/'+str(col)+"/"+str(row))
except:
return "DID NOT PAY"
@app.route('/manage/<id>/seat/<col>/<row>/edit', methods=['GET'])
def editseat(id, col, row):
col = int(col)
row = int(row)
data = db.child("Flights").child(id).get().val()
data['key'] = id
return render_template('book.html', flight=data, col=col, row=row, edit=True)
@app.route('/manage/<id>/seat/<col>/<row>/cancel', methods=['GET'])
def cancelseat(id, col, row):
col = int(col)
row = int(row)
db.child("Flights").child(id).child('Seats').child(col).child(row).set({})
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('status').set('Available')
return redirect('/manage/'+id)
@app.route('/manage/<id>/seat/<col>/<row>/check_in', methods=['GET'])
def checkin(id, col, row):
db.child("Flights").child(id).child('Seats').child(
col).child(row).child('status').set('Checked_In')
return redirect('/manage/'+id)
@app.route('/scan')
def scan():
return send_from_directory('templates', 'scan.html')
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=443, ssl_context='adhoc')