-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
621 lines (421 loc) · 16.4 KB
/
server.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
from flask import Flask ,Response,request,render_template,redirect,session, url_for,jsonify
import pymongo
import json
import os
from user import User
app = Flask(__name__)
app.static_folder = 'static'
from bson.objectid import ObjectId
from cars import Cars
from werkzeug.utils import secure_filename
from datetime import datetime
from pymongo import MongoClient
##################################### MOHAMED FATEHI #########################################
## CONNECTION TO DB
try :
mongo = pymongo.MongoClient(host="localhost", port =27017,serverSelectionTimeoutMS =1000 )
db = mongo.location_voitures #use the company db in mongo
mongo.server_info()
print("connect to db")
except :
print("ERROR - Cannot connect to db")
#ROOT TO LOGIN
@app.route("/login")
def blade() :
return render_template("login.html")
#AUTHENTIFICATION
@app.route("/auth", methods=["POST"])
def auth():
email = request.form["email"]
password = request.form["password"]
session['email'] = email
user = User.authenticate(email, password)
if user is not None:
# Authentication successful, redirect to get_cars route
return redirect(url_for('get_cars'))
return render_template("login.html", error_message="Invalid credentials")
#GET ALL THE CARS BASED ON THE ETAT
@app.route('/cars', methods=['GET'])
def get_cars():
email = session.get('email')
car_list = Cars.get()
user = User.get(email)
return render_template('category.html', cars=car_list,user=user)
@app.route('/logout')
def logout():
session.clear()
return render_template('login.html')
@app.route('/addManager' ,methods=['POST','GET'])
def addManager():
if request.method == 'POST':
client_data = {
'password': request.form['password'],
'nom': request.form['nom'],
'prenom': request.form['prenom'],
'email': request.form['email'],
'tel': request.form['telephone'],
'ville': request.form['ville'],
'role' : "manager"
}
db.utilisateur.insert_one(client_data)
session['success'] = True
return redirect('/manager')
#return render_template('ManagerList.html',success_message="Manager bien saisie!")
return render_template('addManager.html')
@app.route('/addAdmin' ,methods=['POST','GET'])
def addAdmin():
if request.method == 'POST':
client_data = {
'password': request.form['password'],
'nom': request.form['nom'],
'prenom': request.form['prenom'],
'email': request.form['email'],
'tel': request.form['telephone'],
'ville': request.form['ville'],
'role' : "admin"
}
db.utilisateur.insert_one(client_data)
session['success'] = True
return render_template('addAdmin.html',success_message="Admin bien saisie!")
return render_template('addAdmin.html')
@app.route('/manager')
def list_managers():
role = "manager"
managers = db.utilisateur.find({'role': role})
return render_template('ManagerList.html', clients=managers)
@app.route('/deleteManager', methods=['POST'])
def delete_manager():
manager_id = ObjectId(request.form.get('idClient'))
print(manager_id)
print(f"Deleting client with ID: {manager_id}")
result = db.utilisateur.delete_one({'_id': manager_id})
if result.deleted_count > 0:
return redirect(url_for('list_managers'))
else:
return 'Failed to delete the client or client not found.'
@app.route('/admin')
def list_admins():
role = "admin"
managers = db.utilisateur.find({'role': role})
return render_template('AdminList.html', clients=managers)
@app.route('/deleteAdmin', methods=['POST'])
def delete_admin():
manager_id = ObjectId(request.form.get('idClient'))
print(manager_id)
print(f"Deleting client with ID: {manager_id}")
result = db.utilisateur.delete_one({'_id': manager_id})
if result.deleted_count > 0:
return redirect(url_for('list_admins'))
else:
return 'Failed to delete the client or client not found.'
@app.route('/modify_manager', methods=['POST'])
def modify_manager():
print(request.form) # Debugging line
id= ObjectId(request.form['CIN'])
nom = request.form['nom']
prenom = request.form['prenom']
email = request.form['email']
telephone = request.form['telephone']
ville= request.form['ville']
# Find the client with the matching CIN
query = {'_id': id}
client = db.utilisateur.find_one(query)
if client:
# Update the client data
update = {
'$set': {
'nom': nom,
'prenom': prenom,
'email': email,
'tel': telephone,
'ville': ville
}
}
db.utilisateur.update_one(query, update)
return redirect(url_for('list_managers'))
else:
return 'Manager not found'
@app.route('/modify_admin', methods=['POST'])
def modify_admin():
print(request.form) # Debugging line
id= ObjectId(request.form['CIN'])
nom = request.form['nom']
prenom = request.form['prenom']
email = request.form['email']
telephone = request.form['telephone']
ville= request.form['ville']
# Find the client with the matching CIN
query = {'_id': id}
client = db.utilisateur.find_one(query)
if client:
# Update the client data
update = {
'$set': {
'nom': nom,
'prenom': prenom,
'email': email,
'tel': telephone,
'ville': ville
}
}
db.utilisateur.update_one(query, update)
return redirect(url_for('list_admins'))
else:
return 'Admin not found'
##############################""ANAS#######################
# Connect to MongoDB
try:
mongo = pymongo.MongoClient(
host='localhost',
port=27017,
serverSelectionTimeoutMS = 1000
)
db = mongo.location_voitures
cars_collection = db['voiture']
mongo.server_info() # trigger exception if cannot connect to db
print("Connected to db")
except:
print("ERROR - Cannot connect to db")
@app.route('/manage_cars')
def manage_cars():
cars = cars_collection.find()
return render_template('managecars.html', cars=cars)
UPLOAD_FOLDER = 'static/images/cars/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/add_car', methods=['GET', 'POST'])
def add_car():
if request.method == 'POST':
make = request.form['make']
model = request.form['model']
year = int(request.form['year'])
price = int(request.form['price'])
matricule = request.form['matricule']
image = request.files['image']
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
image.save(image_path)
car_data = {
'marque': make,
'modele': model,
'annee': year,
'prix': price,
'matricule': matricule,
'image': filename
}
cars_collection.insert_one(car_data)
return redirect('/manage_cars')
return render_template('addcar.html')
def allowed_file(filename):
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/update_car', methods=['POST'])
def update_car():
car_id = request.form.get('carId')
make = request.form['make']
model = request.form['model']
year = int(request.form['year'])
price = int(request.form['price'])
matricule = request.form['matricule']
filter_criteria = {'_id': ObjectId(car_id)}
update_data = {'$set': {'marque': make, 'modele': model, 'annee': year, 'prix': price, 'matricule': matricule}}
try:
cars_collection.update_one(filter_criteria, update_data)
return redirect('/manage_cars')
except Exception as e:
print(f"Error updating document: {e}")
@app.route('/modify_car')
def modify_car():
car_id = request.args.get('carId')
car = cars_collection.find_one({'_id': ObjectId(car_id)})
if car:
return render_template('modifycar.html', car=car)
else:
return "Car not found"
@app.route('/delete_car', methods=['POST'])
def delete_car():
car_id = request.form.get('carId')
try:
cars_collection.delete_one({'_id': ObjectId(car_id)})
return redirect('/manage_cars')
except Exception as e:
print(f"Error deleting document: {e}")
############################ AHMED ##################################
app.secret_key = 'HereWegoAgain'
client = MongoClient('mongodb://localhost:27017')
db = client['location_voitures']
collection = db['client']
@app.route('/AddNewClient')
def hello():
return render_template('AddNewClient.html')
@app.route('/add_client', methods=['POST'])
def add_client():
client_data = {
'cin': request.form['CIN'],
'nom': request.form['nom'],
'prenom': request.form['prenom'],
'email': request.form['email'],
'tel': request.form['telephone'],
'adresse': request.form['adresse']
}
collection.insert_one(client_data)
session['success'] = True
return redirect(url_for('list_clients'))
@app.route('/clients')
def list_clients():
clients = collection.find()
return render_template('ClientList.html', clients=clients, client=None)
@app.route('/deleteClient', methods=['POST'])
def delete_client():
client_id = request.form.get('idClient')
print(f"Deleting client with ID: {client_id}")
result = collection.delete_one({'cin': client_id})
if result.deleted_count > 0:
return redirect(url_for('list_clients'))
else:
return 'Failed to delete the client or client not found.'
@app.route('/add_new_client')
def add_new_client():
success = session.pop('success', False)
if success:
return """
<script>
Swal.fire({
title: 'Success!',
text: 'Client data inserted successfully.',
icon: 'success',
confirmButtonText: 'OK'
});
</script>
"""
return render_template('AddNewClient.html')
@app.route('/modify_client', methods=['POST'])
def modify_client():
print(request.form) # Debugging line
cin = request.form['CIN']
nom = request.form['nom']
prenom = request.form['prenom']
email = request.form['email']
telephone = request.form['telephone']
adresse = request.form['adresse']
# Find the client with the matching CIN
query = {'cin': cin}
client = collection.find_one(query)
if client:
# Update the client data
update = {
'$set': {
'nom': nom,
'prenom': prenom,
'email': email,
'tel': telephone,
'adresse': adresse
}
}
collection.update_one(query, update)
return redirect(url_for('list_clients'))
else:
return 'Client not found'
############################### MOUAD ###################################
reservation_collection = db['reservation']
@app.route("/location_car/<voiture_id>", methods=['GET', 'POST'])
def location_car(voiture_id):
if request.method == 'POST':
client_id = ObjectId(request.form.get('client'))
voiture_id = ObjectId(voiture_id)
date_debut_str = request.form.get('date_debut')
date_fin_str = request.form.get('date_fin')
voitures = db.voiture.find_one({"_id": ObjectId(voiture_id)})
date_debut = datetime.strptime(date_debut_str, "%Y-%m-%d")
date_fin = datetime.strptime(date_fin_str, "%Y-%m-%d")
# Calculate the difference in days
diff_days = (date_fin - date_debut).days
# Calculate the price based on the difference in days
price = diff_days * voitures.get('prix')
print(price)
#prix_reservation = int(request.form.get('prix_reservation'))
statut = request.form.get('statut', 'en_attente') # Set the default value to "en_attente"
# Convert date_debut and date_fin to datetime objects
date_debut = datetime.strptime(date_debut_str, '%Y-%m-%d')
date_fin = datetime.strptime(date_fin_str, '%Y-%m-%d')
print("====================================")
print(client_id)
print(voiture_id)
print(date_debut)
print(date_fin)
print(statut)
print("====================================")
# Store the client, car, and statut details in the reservation collection
reservation = {
'client_id': client_id,
'voiture_id': voiture_id,
'date_debut': date_debut,
'date_fin': date_fin,
'prix_reservation': price,
'statut': statut
}
reservation_collection.insert_one(reservation)
clients = db['client'].find()
# Redirect or render a success page
return redirect('/cars')
#return render_template('location_car.html',success_message="Voiture bien reserver!",clients=clients, voiture_id=voiture_id,voiture=voitures)
# Fetch client and car data to populate the select options
clients = db['client'].find()
voitures = db.voiture.find_one({"_id": ObjectId(voiture_id)})
return render_template("location_car.html", clients=clients, voiture_id=voiture_id,voiture=voitures)
# Route to display all reservations and handle accept/refuse functionality
@app.route('/gestion_reservations', methods=['GET', 'POST'])
def gestion_reservations():
if request.method == 'POST':
reservation_id = request.form.get('reservation_id') # Get the reservation ID from the submitted form
action = request.form.get('action') # Get the action (accept/refuse) from the submitted form
print(reservation_id, action)
# Update the reservation status based on the action
reservation_collection.update_one({'_id': ObjectId(reservation_id)}, {'$set': {'statut': action}})
# return 'Reservation updated successfully!'
# Fetch all reservations from the collection
reservations = reservation_collection.find({"statut": "en_attente"})
return render_template('gestion_reservations.html', reservations=reservations)
# Route to edit a reservation
@app.route('/edit_reservation/<reservation_id>', methods=['GET', 'POST'])
def edit_reservation(reservation_id):
# Retrieve the reservation object using reservation_id (example implementation)
reservation = db.reservations.find_one({'_id': ObjectId(reservation_id)})
print(reservation_id, reservation.get('client_id'))
return render_template('edit_reservation.html', reservation=reservation)
# Route to dashboard
@app.route('/dashboardTest')
def dashboardtest():
# Fetch all reservations from the collection
reservations = list(reservation_collection.find())
client_collection = db['client']
# Fetch all clients from the collection
clients = list(client_collection.find())
# Extract the status counts from reservations
status_counts = {}
for reservation in reservations:
status = reservation.get('statut')
if status:
status_counts[status] = status_counts.get(status, 0) + 1
# Extract the address counts from clients
address_counts = {}
for client in clients:
address = client.get('adresse')
if address:
address_counts[address] = address_counts.get(address, 0) + 1
# Prepare the data for the chart
labels = list(status_counts.keys())
data = list(status_counts.values())
# Prepare the data for the chart
labels1 = list(address_counts.keys())
data1 = list(address_counts.values())
return render_template('dashboard_test.html',
labels=json.dumps(labels),
data=json.dumps(data),
labels1=json.dumps(labels1),
data1=json.dumps(data1)
)
####################################
# if the script is the main programme, else if its imported from another module not __main__
if __name__ == "__main__" :
app.run(port =80, debug =True )