-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
315 lines (258 loc) · 10.3 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
from dotenv import load_dotenv
from flask import Flask, request, jsonify, session, render_template, flash, redirect
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson.objectid import ObjectId
from datetime import datetime, timedelta
import bcrypt
import os
import stripePayment
from flask_session import Session
# constants
MAX_CYCLES = 2
load_dotenv('.env')
app = Flask(__name__)
app.secret_key = 'some_secret_key'
# MongoDB setup
uri = os.environ.get('MONGODB_URI')
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Successfully connected to MongoDB!")
except Exception as e:
print(e)
db = client['laundromat']
users = db['users']
machines = db['machines']
bookings = db['bookings']
# Flask session setup
app.config['SESSION_TYPE'] = 'mongodb'
app.config['SESSION_MONGODB'] = client
app.config['SESSION_MONGODB_DB'] = 'laundromat'
app.config['SESSION_MONGODB_COLLECTION'] = 'sessions'
Session(app)
# Helper Functions
def check_availability(machine_id, start_time, end_time):
""" Check if the machine is available between the given times """
conflicts = bookings.find_one({
'machineId': ObjectId(machine_id),
'start': {'$lt': end_time},
'end': {'$gt': start_time}
})
return conflicts is None
# API Endpoints
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@app.route('/machines', methods=['GET'])
def machines_html():
return render_template('machines.html')
@app.route('/bookings_history', methods=['GET'])
def bookings_history_html():
return render_template('bookings_history.html')
@app.route('/bookings/<machine_type>/<machine_id>/<machine_name>', methods=['GET'])
def bookings_html(machine_type, machine_id, machine_name):
return render_template('bookings.html')
@app.route('/bookingConfirmation/<booking_id>', methods=['GET'])
def booking_confirmation_html(booking_id):
return render_template('booking_confirmation.html')
@app.route('/payment', methods=['GET'])
def payment_html():
return render_template('payment.html')
@app.route('/payment', methods=['POST'])
def start_payment():
# call stripe function if selected payment method is card
url = stripePayment.create_checkout_session()
return jsonify({'redirectUrl': str(url)}), 200
@app.route('/cancel')
def paymentCancel():
return render_template('cancel.html')
@app.route('/#', methods=['GET', 'POST'])
def #():
if request.method == 'GET':
return render_template('login.html')
data = request.json
# Check if the email is already in use
existing_user = users.find_one({'email': data['email']})
if existing_user:
return jsonify({'message': 'Email already in use'}), 409
# check if the password is at least 8 characters long and contains at least one number and one symbol
if len(data['password']) < 8:
return jsonify({'message': 'Password must be at least 8 characters long'}), 400
if not any(char.isdigit() for char in data['password']):
return jsonify({'message': 'Password must contain at least one number'}), 400
if not any(char in '!@#$%^&*()_+-=[]{}|;:,.<>?/~`' for char in data['password']):
return jsonify({'message': 'Password must contain at least one symbol'}), 400
hashed_password = bcrypt.hashpw(
data['password'], bcrypt.gensalt())
user_id = users.insert_one({
'name': data['name'],
'email': data['email'],
'password': hashed_password,
'role': 'user' if not 'admin' in data['name'] else 'admin'
}).inserted_id
session['user_id'] = str(user_id)
return jsonify({'user_id': str(user_id)}), 200
@app.route('/#', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
data = request.json
print(data)
try:
user = users.find_one({'email': data['email']})
if user and bcrypt.hashpw(data['password'], user['password']):
session['user_id'] = str(user['_id'])
return jsonify({'message': 'Login successful', 'user_id': str(user['_id']), 'user_name': user['name'], 'user_email': user['email']}), 200
except Exception as e:
print(e)
return jsonify({'message': 'Unauthorized'}), 401
@app.route('/logout', methods=['GET'])
def logout():
session.pop('user_id', None)
flash('Logged out successfuly')
return redirect('/')
@app.route('/machineBookings', methods=['POST'])
def machine_bookings():
data = request.json
machine_id = data['machineId']
bookings_this_week = bookings.find({
'machineId': ObjectId(machine_id),
'start': {'$gte': datetime.now() - timedelta(days=datetime.now().weekday() + 1),
'$lt': datetime.now() + timedelta(days=7 - datetime.now().weekday())}
})
result = []
for booking in bookings_this_week:
booking_data = {
'booking_id': str(booking['_id']),
'userId': str(booking['userId']),
'start': booking['start'].strftime('%Y-%m-%d %H:%M'),
'end': booking['end'].strftime('%Y-%m-%d %H:%M'),
'title': booking['title']
}
result.append(booking_data)
return jsonify(result)
@app.route('/machineInfo', methods=['POST'])
def machine_info():
data = request.json
machine_id = data['machineId']
machine = machines.find_one({'_id': ObjectId(machine_id)})
machine_data = {
'id': str(machine['_id']),
'status': machine['status'],
'type': machine['type'],
'name': machine['name']
}
return jsonify(machine_data)
@app.route('/setMachineStatus', methods=['POST'])
def set_machine_status():
data = request.json
machine_id = data['machineId']
status = data['status']
machines.update_one({'_id': ObjectId(machine_id)}, {
'$set': {'status': status}})
return jsonify({'message': 'Status updated'}), 200
@app.route('/getMachines', methods=['GET'])
def get_machines():
all_machines = machines.find({})
result = {'washers': [], 'dryers': []}
for machine in all_machines:
machine_data = {
'id': str(machine['_id']),
'status': machine['status'],
'type': machine['type'],
'name': machine['name'] # added
}
if machine['type'] == 'washer':
result['washers'].append(machine_data)
elif machine['type'] == 'dryer':
result['dryers'].append(machine_data)
return jsonify(result)
@app.route('/getBookings', methods=['GET'])
def get_bookings():
user_id = session.get('user_id')
if not user_id:
return jsonify({'message': 'Unauthorized'}), 401
all_bookings = bookings.find({'userId': ObjectId(user_id)})
result = []
for booking in all_bookings:
booking_data = {
'booking_id': str(booking['_id']),
'machineId': str(booking['machineId']),
'machineName': machines.find_one({'_id': booking['machineId']})['name'],
'cycles': booking['cycles'],
'machineType': machines.find_one({'_id': booking['machineId']})['type'],
'start': booking['start'].strftime('%Y-%m-%d %H:%M'),
'end': booking['end'].strftime('%Y-%m-%d %H:%M'),
'title': booking['title'],
'status': booking['status'],
'paymentMethod': booking['paymentMethod']
}
result.append(booking_data)
# sort latest to oldest
result.sort(key=lambda x: x['start'], reverse=True)
return jsonify(result)
@app.route('/getBooking/<booking_id>', methods=['GET'])
def get_booking(booking_id):
if not ObjectId.is_valid(booking_id):
return jsonify({'message': 'Invalid booking id'}), 400
user_id = session.get('user_id')
if not user_id:
return jsonify({'message': 'Unauthorized'}), 401
booking = bookings.find_one(
{'_id': ObjectId(booking_id), 'userId': ObjectId(user_id)})
if not booking:
return jsonify({'message': 'Booking not found'}), 404
booking_data = {
'machineName': machines.find_one({'_id': booking['machineId']})['name'],
'cycles': booking['cycles'],
'machineType': machines.find_one({'_id': booking['machineId']})['type'],
'start': booking['start'].strftime('%Y-%m-%d %H:%M'),
'end': booking['end'].strftime('%Y-%m-%d %H:%M'),
'status': booking['status'],
'paymentMethod': booking['paymentMethod']
}
return jsonify(booking_data)
@app.route('/userData', methods=['GET'])
def get_user_data():
user_id = session.get('user_id')
if not user_id:
return jsonify({'message': 'Unauthorized'}), 401
user = users.find_one({'_id': ObjectId(user_id)})
if not user:
return jsonify({'message': 'User not found'}), 404
return jsonify({'name': user['name'], 'email': user['email']})
@app.route('/bookMachine', methods=['POST'])
def book_machine():
data = request.json
user_id = session.get('user_id')
if not user_id:
flash('Please login to book a machine')
return jsonify({'message': 'Unauthorized'}), 401
start_time = datetime.strptime(data['start'][:19], '%Y-%m-%dT%H:%M:%S')
end_time = datetime.strptime(data['end'][:19], '%Y-%m-%dT%H:%M:%S')
if start_time < datetime.now():
flash('Cannot book in the past')
return jsonify({'message': 'Cannot book in the past'}), 400
if end_time > start_time + timedelta(minutes=30 * MAX_CYCLES):
flash('Cannot book more than 2 cycles')
return jsonify({'message': 'Cannot book more than 2 cycles'}), 400
if check_availability(data['machineId'], start_time, end_time):
booking_id = bookings.insert_one({
'machineId': ObjectId(data['machineId']),
'userId': ObjectId(user_id),
'start': start_time,
'end': end_time,
'title': data['title'],
'cycles': data['cycles'],
'status': data['status'],
'paymentMethod': data['paymentMethod']
}).inserted_id
return jsonify({'booking_id': str(booking_id)}), 200
return jsonify({'message': 'Machine not available'}), 409
if __name__ == '__main__':
# app.run(debug=True, host='0.0.0.0', port=8080)
from waitress import serve
serve(app, host="0.0.0.0", port=8080, threads=100)