-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
97 lines (82 loc) · 3.38 KB
/
index.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
from flask import Flask ,render_template,request,flash,jsonify
from werkzeug.security import generate_password_hash,check_password_hash
import datetime
import json
import secrets
import jwt
#initialize the app
app=Flask(__name__)
#basic app config
app.config['SECRET_KEY']=secrets.token_hex(16)
#booked
bookings = {
'A': {'3': '[email protected]', '4': '[email protected]', '6': '[email protected]', '7': '[email protected]'},
'B': {'1': '[email protected]', '2': '[email protected]', '3': '[email protected]', '5': '[email protected]', '6': '[email protected]', '7': '[email protected]', '8': '[email protected]'},
'C': {'7': '[email protected]', '8': '[email protected]'},
'D': {'4': '[email protected]'},
'E': {'2': '[email protected]', '3': '[email protected]', '7': '[email protected]', '8': '[email protected]'}
}
#index page
@app.route('/')
def index():
return "Welcome to Booking System"
#to register and dump information onto .txt file
@app.route('/register', methods=["POST"])
def register():
data = request.get_json()
first_name = data['first_name']
last_name = data['last_name']
date_of_birth = data['date_of_birth']
email = data['email']
password = data['password']
user = {'first_name': first_name, 'last_name': last_name, 'date_of_birth': date_of_birth, 'email': email, 'password': password}
with open('users.txt', 'a') as f:
f.write(json.dumps(user) + '\n')
return 'User registered successfully'
@app.route('/login', methods=["POST","GET"])
def login():
data = request.get_json()
email = data['email']
password = data['password']
with open('users.txt') as f:
for line in f:
user = json.loads(line)
if user['email'] == email and user['password'] == password:
payload = {
'email': email,
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1)
}
token = jwt.encode(payload, 'secret', algorithm='HS256')
return token
return 'Invalid email or password', 401
#create a booking system
@app.route('/suggest-booking', methods=["GET","POST"])
def suggest_booking():
num_seats = int(request.args.get('seat'))
authorization = request.headers.get('Authorization')
if authorization != 'Bearer valid_token':
return 'Invalid token', 401
available_seats = []
current_row = None
for row, seat_bookings in bookings.items():
available_in_row = []
for seat, email in seat_bookings.items():
if len(available_in_row) == num_seats:
available_seats.append(available_in_row)
available_in_row = []
elif len(available_in_row) > 0 and seat_bookings[seat] == '1':
# If more than 1 seat is requested and a walking path is found, start a new seat group
available_seats.append(available_in_row)
available_in_row = []
elif current_row is not None and current_row != row:
# If more than 1 seat is requested and a new row is found, start a new seat group
available_seats.append(available_in_row)
available_in_row = []
else:
available_in_row.append(row + seat)
current_row = row
if available_in_row:
available_seats.append(available_in_row)
return json.dumps(available_seats)
if __name__ == "__main__":
app.run(debug=True)