-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
372 lines (275 loc) · 11 KB
/
database.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
import os
from bson import ObjectId
from flask import Blueprint, request, render_template, flash, redirect, url_for, make_response
from flask_login import login_required
from gridfs import GridFS
from pymongo import MongoClient
from authentication import role_required, load_user
from classes import Role, User, MenuItem, Order, Status, ServiceRequest
from qr import generate_qr_code
db_routes = Blueprint('db_routes', __name__, template_folder='templates')
# MongoDB configuration
client = MongoClient('mongodb://localhost:27017/r3s')
db = client['r3s']
users_collection = db['users']
menu_collection = db['menu']
orders_collection = db['orders']
requests_collection = db['requests']
fs = GridFS(db) # For storing images
@db_routes.route('/get-image/<image_id>')
def get_image(image_id):
image = fs.get(ObjectId(image_id))
response = make_response(image.read())
response.headers['Content-Type'] = 'image/jpeg'
return response
# Menu items
@db_routes.route('/add-item', methods=['GET', 'POST'])
@login_required
@role_required(Role.ADMIN)
def add_item():
if request.method == 'POST':
name = request.form['name']
description = request.form['description']
price = request.form['price']
item = MenuItem(name=name, description=description, price=price)
image = request.files['image']
if image and image.filename != '':
image_id = fs.put(image, filename=image.filename)
item.image_id = image_id
item_dict = item.to_dict()
result = menu_collection.insert_one(item_dict)
if result.acknowledged:
flash('Nowe danie zostało dodane do bazy.', 'success')
else:
flash('Nie udało się dodać nowego dania do bazy.', 'error')
return redirect(url_for('menu'))
else:
return render_template('add_item_form.html')
@db_routes.route('/modify-item/<item_id>', methods=['GET', 'POST'])
@login_required
@role_required(Role.ADMIN)
def modify_item(item_id):
item_id = ObjectId(item_id)
item_data = menu_collection.find_one({'_id': item_id})
menu_item = MenuItem.from_dict(item_data)
if request.method == 'POST':
menu_item.name = request.form['name']
menu_item.description = request.form['description']
menu_item.price = request.form['price']
# Update image if selected in form
image = request.files['image-input']
print(image)
if image and image.filename != '':
if menu_item.image_id is not None:
old_image_id = ObjectId(item_data.get('image_id'))
fs.delete(old_image_id)
fs.put(image, filename=image.filename, _id=old_image_id)
else:
image_id = fs.put(image, filename=image.filename)
menu_item.image_id = image_id
menu_item_dict = menu_item.to_dict()
menu_item_dict.pop('_id') # Remove id for update
menu_collection.update_one({'_id': item_id}, {
'$set': menu_item_dict
})
flash('Danie zostało zmodyfikowane.', 'success')
return redirect(url_for('menu'))
return render_template('modify_item_form.html', item=menu_item)
@db_routes.route('/delete-item-image/<item_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def delete_item_image(item_id):
item_id = ObjectId(item_id)
menu_item_dict = menu_collection.find_one({'_id': item_id})
menu_item = MenuItem.from_dict(menu_item_dict)
fs.delete(menu_item.image_id)
menu_collection.update_one({'_id': item_id}, {
'$unset': {'image_id': 1}
})
return redirect(url_for('menu'))
@db_routes.route('/delete-item/<item_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def delete_item(item_id):
item_id = ObjectId(item_id)
item = menu_collection.find_one({'_id': item_id})
if item:
image_id = item.get('image_id')
result = menu_collection.delete_one({'_id': item_id})
if result.deleted_count > 0:
fs.delete(image_id)
flash('Danie zostało usunięte.', 'success')
else:
flash('Nie udało się usunąć dania.', 'error')
else:
flash('Nie znaleziono dania.', 'error')
return redirect(url_for('menu'))
# Tables
@db_routes.route('/add-table', methods=['GET', 'POST'])
@login_required
@role_required(Role.ADMIN)
def add_table():
if request.method == 'POST':
name = request.form['name']
table = User(name=name, role=Role.USER)
table_dict = table.to_dict()
result = users_collection.insert_one(table_dict)
if result.acknowledged:
inserted_id = result.inserted_id
qr_code_image = generate_qr_code(inserted_id)
qr_code_image_id = fs.put(qr_code_image, filename=inserted_id)
users_collection.update_one(
{'_id': inserted_id},
{'$set': {'qr_code_image_id': qr_code_image_id}}
)
flash('Stolik został dodany.', 'success')
else:
flash('Nie udało się dodać stolika.', 'error')
return redirect(url_for('table_manager'))
else:
return render_template('add_table_form.html')
@db_routes.route('/modify-table/<table_id>', methods=['GET', 'POST'])
@login_required
@role_required(Role.ADMIN)
def modify_table(table_id):
table_data = users_collection.find_one({'_id': ObjectId(table_id)})
if request.method == 'POST':
name = request.form['name']
users_collection.update_one({'_id': ObjectId(table_id)}, {
'$set': {
'name': name
}
})
flash('Stół został zmodyfikowany.', 'success')
return redirect(url_for('table_manager'))
return render_template('modify_table_form.html', table=table_data)
@db_routes.route('/delete-table/<table_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def delete_table(table_id):
table_id = ObjectId(table_id)
table = users_collection.find_one({'_id': table_id})
if table:
qr_code_image_id = table.get('qr_code_image_id')
result = users_collection.delete_one({'_id': table_id})
if result.deleted_count > 0:
fs.delete(qr_code_image_id)
flash('Stół został usunięty.', 'success')
else:
flash('Nie udało się usunąć stołu.', 'error')
else:
flash('Nie znaleziono stołu.', 'error')
return redirect(url_for('table_manager'))
# Orders
@db_routes.route('/change-order-status/<order_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def change_order_status(order_id):
order_dict = orders_collection.find_one({'_id': ObjectId(order_id)})
order = Order.from_dict(order_dict)
order.status = Status(request.form['status'])
order_dict = order.to_dict()
orders_collection.update_one({'_id': ObjectId(order_id)}, {
'$set': order_dict
})
flash('Status zamówienia został zmodyfikowany.', 'success')
return redirect(url_for('orders_manager'))
@db_routes.route('/delete-order/<order_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def delete_order(order_id):
order_id = ObjectId(order_id)
order = orders_collection.find_one({'_id': order_id})
if order:
result = orders_collection.delete_one({'_id': order_id})
if result.deleted_count > 0:
flash('Zamówienie zostało usunięte.', 'success')
else:
flash('Nie udało się usunąć zamówienia.', 'error')
else:
flash('Nie znaleziono zamówienia.', 'error')
return redirect(url_for('orders_manager'))
# Service requests
@db_routes.route('/change-service-request-status/<service_request_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def change_service_request_status(service_request_id):
service_request_dict = requests_collection.find_one({'_id': ObjectId(service_request_id)})
service_request = ServiceRequest.from_dict(service_request_dict)
service_request.status = Status(request.form['status'])
service_request_dict = service_request.to_dict()
requests_collection.update_one({'_id': ObjectId(service_request_id)}, {
'$set': service_request_dict
})
flash('Status prośby został zmodyfikowany.', 'success')
return redirect(url_for('service_requests_manager'))
@db_routes.route('/delete-service-request/<service_request_id>', methods=['POST'])
@login_required
@role_required(Role.ADMIN)
def delete_service_request(service_request_id):
service_request_id = ObjectId(service_request_id)
service_request = requests_collection.find_one({'_id': service_request_id})
if service_request:
result = requests_collection.delete_one({'_id': service_request_id})
if result.deleted_count > 0:
flash('Prośba została usunięta.', 'success')
else:
flash('Nie udało się usunąć prośby.', 'error')
else:
flash('Nie znaleziono prośby.', 'error')
return redirect(url_for('service_requests_manager'))
# Helper methods
def get_menu():
menu_dict = menu_collection.find()
menu = []
for menu_item_dict in menu_dict:
menu_item = MenuItem.from_dict(menu_item_dict)
menu.append(menu_item)
return menu
def get_menu_item(item_id):
menu_item_dict = menu_collection.find_one({'_id': ObjectId(item_id)})
menu_item = MenuItem.from_dict(menu_item_dict)
return menu_item
def get_user(user_id):
return load_user(user_id)
def get_tables():
tables_dict = users_collection.find({"role": {"$ne": "admin"}})
tables = []
for table_dict in tables_dict:
tables.append(User.from_dict(table_dict))
return tables
def get_orders(include_finished=False):
orders_dict = orders_collection.find()
orders = []
for order_dict in orders_dict:
order = Order.from_dict(order_dict)
if not include_finished and order.status == Status.FINISHED:
continue
for order_item in order.order_items:
order.price_sum = order.price_sum + order_item.menu_item.price
order.price_sum = round(order.price_sum, 2)
orders.append(order)
return orders
def get_service_requests(include_finished=False):
service_requests_dict = requests_collection.find()
service_requests = []
for service_request_dict in service_requests_dict:
service_request = ServiceRequest.from_dict(service_request_dict)
if not include_finished and service_request.status == Status.FINISHED:
continue
service_requests.append(service_request)
return service_requests
def insert_order(order):
order_dict = order.to_dict()
result = orders_collection.insert_one(order_dict)
if result.acknowledged:
return True
else:
return False
def insert_service_request(service_request):
service_request_dict = service_request.to_dict()
result = requests_collection.insert_one(service_request_dict)
if result.acknowledged:
flash('Prośba została wysłana do obsługi.', 'success')
else:
flash('Nie udało się wysłać prośby do obsługi.', 'error')