-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
378 lines (317 loc) · 13.4 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
from flask import Flask, render_template, request, redirect, url_for, jsonify, send_file
import sqlite3
import os
import datetime
import time
from waitress import serve
app = Flask(__name__)
# Function to initialize the database
def initialize_database():
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS bookings
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, ph_desc TEXT, activity TEXT, timeslot TEXT, qr_code TEXT, status TEXT, subactivity TEXT)''')
conn.commit()
# Check if the database file exists, if not, create the database
if not os.path.exists('booking.db'):
initialize_database()
def get_next_10_minute_range():
current_time = datetime.datetime.now().time()
next_10_minutes = (current_time.minute // 10 + 1) * 10
if next_10_minutes == 60:
next_10_minutes = 0
current_hour = current_time.hour + 1
else:
current_hour = current_time.hour
next_time10 = current_time.replace(hour=current_hour, minute=next_10_minutes, second=0, microsecond=0)
next_20_minutes = (current_time.minute // 10 + 1) * 10 + 10
if next_20_minutes > 60:
next_20_minutes -= 60
elif next_20_minutes == 60:
next_20_minutes -= 60
current_hour += 1
next_time20 = current_time.replace(hour=current_hour, minute=next_20_minutes, second=0, microsecond=0)
final_time = next_time10.strftime("%H:%M") + " - " + next_time20.strftime("%H:%M")
return final_time
def get_current_10_minute_range():
current_time = datetime.datetime.now().time()
current_10_minutes = (current_time.minute // 10) * 10
current_time10 = current_time.replace(minute=current_10_minutes, second=0, microsecond=0)
# Check if adding 10 minutes would exceed 59 minutes
if current_10_minutes == 50:
current_time20 = current_time.replace(hour=current_time.hour + 1, minute=0, second=0, microsecond=0)
else:
current_time20 = current_time.replace(minute=current_10_minutes + 10, second=0, microsecond=0)
final_time = current_time10.strftime("%H:%M") + " - " + current_time20.strftime("%H:%M")
return final_time
# Routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/logo.png')
def logo():
return send_file('./templates/airk.png')
@app.route('/instructor')
def inst():
return render_template('instructor.html')
@app.route('/instructor/kampfjet')
def instkj():
return render_template('instructor_kj.html')
@app.route('/instructor/propeller')
def instpp():
return render_template('instructor_pp.html')
@app.route('/get_next_time_slot')
def get_next_time_slot():
activity = str(request.args.get('activity'))
print(activity)
next_time = get_current_10_minute_range()
print(next_time)
# Get the next available time slot for the selected activity
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT name, status, ph_desc, subactivity FROM bookings WHERE activity = ? AND timeslot = ?", (activity, next_time))
row = c.fetchone()
if row:
return jsonify({'name': row[0], 'status': row[1], 'ph_desc': row[2], 'sub': row[3]})
else:
return jsonify('None')
@app.route('/get_next_next_time_slot')
def get_next_next_time_slot():
activity = str(request.args.get('activity'))
print(activity)
next_time = get_next_10_minute_range()
print(next_time)
# Get the next available time slot for the selected activity
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT name, status, ph_desc, subactivity FROM bookings WHERE activity = ? AND timeslot = ?", (activity, next_time))
row = c.fetchone()
if row:
return jsonify({'name': row[0], 'status': row[1], 'ph_desc': row[2], 'sub': row[3]})
else:
return jsonify('None')
@app.route('/booking2', methods=['GET', 'Post'])
def booking2():
if request.method == 'POST':
name = request.args.get('name')
ph_desc = request.args.get('ph_desc')
activity = request.args.get('activity')
slot = request.args.get('slot')
qr = request.args.get('qr')
if name:
print('hi')
@app.route('/booking', methods=['GET', 'POST'])
def booking():
global booking_state
global activity_form
global timeslot_form
global subactivity_form
if booking_state == None:
booking_state = 0
if request.method == 'POST':
type = request.args.get('s')
if type == '0':
with sqlite3.connect('booking.db') as conn:
name = request.form['name']
ph_desc = request.form['ph_desc']
activity_form = request.form['activity']
subactivity_form = request.form['subactivity']
timeslot_form = request.form['timeslot']
qr_code = request.form['qr_code']
status = "Booked"
c = conn.cursor()
c.execute("INSERT INTO bookings (name, ph_desc, activity, timeslot, qr_code, status, subactivity) VALUES (?, ?, ?, ?, ?, ?, ?)",
(name, ph_desc, activity_form, timeslot_form, qr_code, status, subactivity_form))
conn.commit()
booking_state = 1
return redirect(url_for('booking'))
elif type == '1':
booking_state = 2
return redirect(url_for('booking'))
elif type == '2':
booking_state = 0
return redirect(url_for('booking'))
elif type == '3':
booking_state = 3
return redirect(url_for('booking'))
elif type == '4':
booking_state = 4
return redirect(url_for('booking'))
else:
if booking_state == 0:
return render_template('booking.html')
elif booking_state == 1:
return render_template('booking_overw.html')
elif booking_state == 2:
return render_template('booking_pay.html')
elif booking_state == 3:
return render_template('booking_pay_gt.html')
elif booking_state == 4:
return render_template('booking_pay_gt.html')
else:
return '500 Server Error'
@app.route('/booking/customer')
def booking_pd():
global booking_state
global activity_form
global timeslot_form
global subactivity_form
if booking_state == 0:
return render_template('custompayinit.html')
elif booking_state == 1:
return render_template('custompayoverw.html', activity=activity_form, slot=timeslot_form, sub=subactivity_form)
elif booking_state == 2:
return render_template('custompayprice.html')
elif booking_state == 3:
return render_template('custompayprice_gt.html')
elif booking_state == 4:
return render_template('custompayprice_do.html')
else:
return '500 Internal error'
@app.route('/get_booked_timeslots')
def get_booked_timeslots():
activity = request.args.get('activity')
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT timeslot FROM bookings WHERE activity = ? AND status IN ('Booked', 'Arrived')", (activity,))
booked_timeslots = [row[0] for row in c.fetchall()]
return jsonify(booked_timeslots)
@app.route('/get_next_timeslot')
def get_timeslots():
timeslots = {
"Kampfjet": ["17:00 - 17:10", "17:10 - 17:20", "17:20 - 17:30", "17:30 - 17:40", "17:40 - 17:50", "17:50 - 18:00", "18:00 - 18:10", "18:10 - 18:20", "18:20 - 18:30", "18:30 - 18:40", "18:40 - 18:50", "18:50 - 19:00", "19:00 - 19:10", "19:10 - 19:20", "19:20 - 19:30", "19:30 - 19:40", "19:40 - 19:50", "19:50 - 20:00"],
"Propeller": ["17:00 - 17:10", "17:10 - 17:20", "17:20 - 17:30", "17:30 - 17:40", "17:40 - 17:50", "17:50 - 18:00", "18:00 - 18:10", "18:10 - 18:20", "18:20 - 18:30", "18:30 - 18:40", "18:40 - 18:50", "18:50 - 19:00", "19:00 - 19:10", "19:10 - 19:20", "19:20 - 19:30", "19:30 - 19:40", "19:40 - 19:50", "19:50 - 20:00"]
# Add more activities and their corresponding timeslots as needed
}
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT activity, timeslot, status FROM bookings WHERE status IN ('Booked', 'Arrived')")
booked_timeslots = {activity: set() for activity in timeslots.keys()}
for row in c.fetchall():
activity = row[0]
timeslot = row[1]
booked_timeslots[activity].add(timeslot)
# Filter out booked timeslots for each activity
for activity in timeslots.keys():
timeslots[activity] = [timeslot for timeslot in timeslots[activity] if timeslot not in booked_timeslots[activity]]
return jsonify(timeslots)
@app.route('/get_bookings')
def get_bookings():
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT name, ph_desc, activity, timeslot, status, qr_code, subactivity FROM bookings ORDER BY timeslot ASC")
bookings = [{'name': row[0], 'ph_desc': row[1], 'activity': row[2], 'timeslot': row[3], 'status': row[4], 'qr_code': row[5], 'subactivity': row[6]} for row in c.fetchall()]
return jsonify(bookings)
@app.route('/remove_booking', methods=['DELETE'])
def remove_booking():
booking_name = request.args.get('name')
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("DELETE FROM bookings WHERE qr_code = ?", (booking_name,))
conn.commit()
return '', 204 # No content response for successful deletion
@app.route('/instascan.min.js')
def instascan():
return send_file('./templates/instascan.min.js')
@app.route('/qr.png')
def qrpng():
return send_file('./templates/qr.png')
@app.route('/check.png')
def checkpng():
return send_file('./templates/check.png')
@app.route('/kamap.jpg')
def kmap():
return send_file('./templates/kamap.jpg')
@app.route('/clock.png')
def clockpng():
return send_file('./templates/clock.png')
@app.route('/sw.json')
def swjson():
return send_file('./templates/sw.json')
@app.route('/logom.png')
def logm():
return send_file('./templates/logom.png')
@app.route('/cross.png')
def crosspng():
return send_file('./templates/cross.png')
@app.route('/checkin/reset')
def checkin_reset():
return redirect(url_for('checkin'))
@app.route('/manifest.json')
def manifest_pwa():
return send_file('./templates/manifest.json')
@app.route('/checkin', methods=['GET', 'POST'])
def checkin():
global ch_status
if ch_status == None:
ch_status = 0
if request.method == 'POST':
qr_code = request.form['qr_code']
current_time = datetime.datetime.now().strftime("%H:%M")
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("SELECT timeslot FROM bookings WHERE qr_code = ? AND status = 'Booked'", (qr_code,))
row = c.fetchone()
c.execute("SELECT activity FROM bookings WHERE qr_code = ? AND status = 'Booked'", (qr_code,))
act = c.fetchone()
c.execute("SELECT name FROM bookings WHERE qr_code = ? AND status = 'Booked'", (qr_code,))
nam = c.fetchone()
if nam:
name = nam[0]
if row:
timeslot = row[0]
timeslot_start = timeslot.split('-')[0].strip()
timeslot_end = timeslot.split('-')[1].strip()
current_timeslot = get_current_10_minute_range()
next_time_slot = get_next_10_minute_range()
if timeslot == current_timeslot or timeslot == next_time_slot:
c.execute("UPDATE bookings SET status = 'Arrived' WHERE qr_code = ?", (qr_code,))
conn.commit()
if act:
activity = act[0]
if activity == 'Kampfjet':
ch_status = 1
return render_template('checkin_ok_kj.html', name=name)
else:
ch_status = 1
return render_template('checkin_ok_pf.html', name=name)
else:
ch_status = 2
return render_template('checkin_wait.html', timeslot=timeslot)
else:
ch_status = 2
return render_template('checkin_error.html')
else:
return render_template('checkin.html')
@app.route('/checkin/ev3')
def checkin_ev3():
global ch_status
ch_status_tmp = ch_status
if ch_status != 0:
ch_status = 0
return str(ch_status_tmp)
@app.route('/checkin/admin')
def adminarrive():
return render_template('checkin_admin.html')
@app.route('/arrive_booking', methods=['ARRIVE'])
def arrive_booking():
booking_name = request.args.get('name')
with sqlite3.connect('booking.db') as conn:
c = conn.cursor()
c.execute("UPDATE bookings SET status = 'Arrived' WHERE qr_code = ?", (booking_name,))
conn.commit()
return '', 204 # No content response for successful deletion
@app.route('/qr.js')
def qrjs():
return send_file('./templates/html5-qrcode.min.js')
@app.route('/info')
def info():
return render_template('info.html')
if __name__ == '__main__':
booking_state = 0
ch_status = 0
#app.run(debug=True, ssl_context='adhoc', host='0.0.0.0')
serve(app, listen='*:5000')