-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
671 lines (547 loc) · 17.1 KB
/
manage.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
'''Blueprint contains all endpoints for add/edit/remove for all
vehicles/groups/users/locations'''
from flask import Blueprint, Markup, jsonify, render_template, request
from flask_login import login_required
from flask_mysqldb import MySQL
from werkzeug.security import generate_password_hash
from .auth import require_role
bp = Blueprint('manage', __name__, url_prefix='/manage')
mysql = MySQL()
@bp.route('/users')
@login_required
@require_role('manager')
def users():
headers = [
{'text': 'Manage'},
{'text': 'Name'},
{'text': 'Wizard ID'},
{'text': 'Email'},
{'text': 'Role'},
{'text': 'Default Location'}
]
cur = mysql.connect.cursor()
cur.execute(
'''SELECT u.user_id, u.wizard_id, u.first_name, u.last_name,
u.email_address, l.name, u.active
FROM users u
LEFT JOIN locations l
ON l.location_id = u.default_location'''
)
all_users = cur.fetchall()
rows = []
for user in all_users:
if user['active'] == 1:
cur.execute(
'''SELECT g.name
FROM users_in_groups ug
LEFT JOIN user_groups g
ON g.group_id = ug.group_id
WHERE ug.user_id = %s''',
(user['user_id'],)
)
groups = []
for group in cur.fetchall():
groups.append(group['name'])
manage = {'text': Markup(
'<a href="#" edit>'
+ '<i class="fas fa-pencil-alt"></i> Edit</a>'
+ ' | '
+ '<a href="#" remove>'
+ '<i class="fas fa-trash-alt"></i> Remove</a>'
)}
else:
groups = ['Inactive']
manage = {
'text': Markup('<a href="#" activate>'
+ '<i class="fas fa-check"></i> Activate</a>')
}
rows.append({
'data': [
{
'name': 'id',
'value': user['user_id']
},
{
'name': 'first_name',
'value': user['first_name']
},
{
'name': 'last_name',
'value': user['last_name']
},
],
'cells': [
manage,
{'text': user['first_name'] + ' ' + user['last_name']},
{'text': user['wizard_id']},
{'text': user['email_address']},
{'text': ', '.join(groups)},
{'text': user['name']}
]
})
table = {
'id': 'users',
'headers': headers,
'rows': rows
}
return render_template(
'manage.html',
title='Manage Users',
table=table,
form='user'
)
@bp.route('/remove_user', methods=('POST',))
@login_required
@require_role('manager')
def remove_user():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE users SET active = 0 WHERE user_id = %s''',
(request.form.get('user_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/activate_user', methods=('POST',))
@login_required
@require_role('manager')
def activate_user():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE users SET active = 1 WHERE user_id = %s''',
(request.form.get('user_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/edit_user', methods=('POST',))
@login_required
@require_role('manager')
def edit_user():
cur = mysql.connect.cursor()
wizard_id = request.form.get('wizard_id')
if wizard_id.strip() == '':
wizard_id = None
password = request.form.get('password', None)
if password is not None and password != '':
confirm = request.form.get('confirm_password')
if password == confirm:
new_password = request.form.get('new_password', None)
confirm_password = request.form.get('confirm_password', None)
if new_password == confirm_password:
cur.execute(
'''UPDATE users
SET password_hash = %s
WHERE user_id = %s''',
(
generate_password_hash(new_password),
request.form.get('user_id')
)
)
cur.execute(
'''UPDATE users SET
wizard_id = %s,
first_name = %s,
last_name = %s,
email_address = %s,
default_location = %s
WHERE user_id = %s''',
(
wizard_id,
request.form.get('first_name'),
request.form.get('last_name'),
request.form.get('email'),
request.form.get('location'),
request.form.get('user_id')
)
)
cur.execute(
'''UPDATE users_in_groups
SET group_id = %s
WHERE user_id = %s''',
(
request.form.get('role'),
request.form.get('user_id')
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/vehicle_groups')
@login_required
@require_role('manager')
def vehicle_groups():
headers = [
{'text': 'Manage'},
{'text': 'Group Name'},
{'text': 'Budget Name'},
{'text': 'Description'},
{'text': 'Active'},
{'text': 'Colour'}
]
cur = mysql.connect.cursor()
cur.execute(
'''SELECT group_id, group_name, budget_name, description,
HEX(booking_color) AS 'booking_color', active
FROM vehicle_groups'''
)
all_groups = cur.fetchall()
rows = []
for group in all_groups:
group['booking_color'] = group['booking_color'].rjust(6, '0')
colors = Markup(
'<span style="background-color: #'
+ group['booking_color']
+ '; width: 2em; display: block;"> </span>'
)
if group['active'] == 1:
active = 'Yes'
manage = {'text': Markup(
'<a href="#" edit>'
+ '<i class="fas fa-pencil-alt"></i> Edit</a>'
+ ' | '
+ '<a href="#" remove>'
+ '<i class="fas fa-trash-alt"></i> Remove</a>'
)}
else:
active = 'No'
manage = {
'text': Markup('<a href="#" activate>'
+ '<i class="fas fa-check"></i> Activate</a>')
}
rows.append({
'data': [
{
'name': 'id',
'value': group['group_id']
},
{
'name': 'booking_color',
'value': group['booking_color']
}
],
'cells': [
manage,
{'text': group['group_name']},
{'text': group['budget_name']},
{'text': group['description']},
{'text': active},
{'text': colors}
]
})
table = {
'id': 'groups',
'headers': headers,
'rows': rows
}
return render_template(
'manage.html',
title='Manage Groups',
table=table,
form='group'
)
@bp.route('/edit_group', methods=('POST',))
@login_required
@require_role('manager')
def edit_group():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicle_groups SET
group_name = %s,
budget_name = %s,
description = %s,
booking_color = %s
WHERE group_id = %s''',
(
request.form.get('name'),
request.form.get('budget_name'),
request.form.get('description'),
int(request.form.get('booking_color').strip('#'), 16),
request.form.get('group_id')
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/add_group', methods=('POST',))
@login_required
@require_role('manager')
def add_group():
cur = mysql.connect.cursor()
cur.execute(
'''INSERT INTO vehicle_groups (
group_name, budget_name, description, booking_color
) VALUES (%s, %s, %s, %s)''',
(
request.form.get('name'),
request.form.get('budget_name'),
request.form.get('description'),
int(request.form.get('booking_color').strip('#'), 16)
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/remove_group', methods=('POST',))
@login_required
@require_role('manager')
def remove_group():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicle_groups SET active = 0 WHERE group_id = %s''',
(request.form.get('group_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/activate_group', methods=('POST',))
@login_required
@require_role('manager')
def activate_group():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicle_groups SET active = 1 WHERE group_id = %s''',
(request.form.get('group_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/locations')
@login_required
@require_role('manager')
def locations():
headers = [
{'text': 'Manage'},
{'text': 'Name'},
{'text': 'Active'}
]
cur = mysql.connect.cursor()
cur.execute(
'''SELECT location_id, name, active
FROM locations'''
)
all_locations = cur.fetchall()
rows = []
for location in all_locations:
if location['active'] == 1:
active = 'Yes'
manage = {'text': Markup(
'<a href="#" edit>'
+ '<i class="fas fa-pencil-alt"></i> Edit</a>'
+ ' | '
+ '<a href="#" remove>'
+ '<i class="fas fa-trash-alt"></i> Remove</a>'
)}
else:
active = 'No'
manage = {
'text': Markup('<a href="#" activate>'
+ '<i class="fas fa-check"></i> Activate</a>')
}
rows.append({
'data': [
{
'name': 'id',
'value': location['location_id']
}
],
'cells': [
manage,
{'text': location['name']},
{'text': active}
]
})
table = {
'id': 'locations',
'headers': headers,
'rows': rows
}
return render_template(
'manage.html',
title='Manage Locations',
table=table,
form='location'
)
@bp.route('/edit_location', methods=('POST',))
@login_required
@require_role('manager')
def edit_location():
cur = mysql.connect.cursor()
cur.execute(
'UPDATE locations SET name = %s WHERE location_id = %s',
(
request.form.get('name'),
request.form.get('location_id')
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/add_location', methods=('POST',))
@login_required
@require_role('manager')
def add_location():
cur = mysql.connect.cursor()
cur.execute(
'INSERT INTO locations (name) VALUES (%s)',
(request.form.get('name'),)
)
mysql.connect.commit()
return jsonify({'success': True})
@bp.route('/remove_location', methods=('POST',))
@login_required
@require_role('manager')
def remove_location():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE locations SET active = 0 WHERE location_id = %s''',
(request.form.get('location_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/activate_location', methods=('POST',))
@login_required
@require_role('manager')
def activate_location():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE locations SET active = 1 WHERE location_id = %s''',
(request.form.get('location_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/vehicles')
@login_required
@require_role('manager')
def vehicles():
headers = [
{'text': 'Manage'},
{'text': 'Registration'},
{'text': 'Location'},
{'text': 'Active'},
{'text': 'Group'}
]
cur = mysql.connect.cursor()
cur.execute(
'''SELECT v.vehicle_id, v.registration, v.active, l.name,
vig.group_id, vg.group_name
FROM vehicles v
LEFT JOIN locations l
ON l.location_id = v.location_id
LEFT JOIN vehicles_in_groups vig
ON vig.vehicle_id = v.vehicle_id
LEFT JOIN vehicle_groups vg
ON vig.group_id = vg.group_id'''
)
all_vehicles = cur.fetchall()
rows = []
for vehicle in all_vehicles:
if vehicle['active'] == 1:
active = 'Yes'
manage = {'text': Markup(
'<a href="#" edit>'
+ '<i class="fas fa-pencil-alt"></i> Edit</a>'
+ ' | '
+ '<a href="#" remove>'
+ '<i class="fas fa-trash-alt"></i> Remove</a>'
)}
else:
active = 'No'
manage = {
'text': Markup('<a href="#" activate>'
+ '<i class="fas fa-check"></i> Activate</a>')
}
rows.append({
'data': [
{
'name': 'id',
'value': vehicle['vehicle_id']
},
{
'name': 'group_id',
'value': vehicle['group_id']
}
],
'cells': [
manage,
{'text': vehicle['registration']},
{'text': vehicle['name']},
{'text': active},
{'text': vehicle['group_name']}
]
})
table = {
'id': 'vehicles',
'headers': headers,
'rows': rows
}
return render_template(
'manage.html',
title='Manage Vehicles',
table=table,
form='vehicle'
)
@bp.route('/edit_vehicle', methods=('POST',))
@login_required
@require_role('manager')
def edit_vehicle():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicles SET
registration = %s,
location_id = %s
WHERE vehicle_id = %s''',
(
request.form.get('registration'),
request.form.get('location'),
request.form.get('vehicle_id')
)
)
cur.execute(
'''UPDATE vehicles_in_groups SET
group_id = %s
WHERE vehicle_id = %s''',
(
request.form.get('group'),
request.form.get('vehicle_id')
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/add_vehicle', methods=('POST',))
@login_required
@require_role('manager')
def add_vehicle():
cur = mysql.connect.cursor()
cur.execute(
'INSERT INTO vehicles (registration, location_id) VALUES (%s, %s)',
(
request.form.get('registration'),
request.form.get('location')
)
)
cur.execute(
'''INSERT INTO vehicles_in_groups (
vehicle_id, group_id
) VALUES (%s, %s)''',
(
cur.lastrowid,
request.form.get('group')
)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/remove_vehicle', methods=('POST',))
@login_required
@require_role('manager')
def remove_vehicle():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicles SET active = 0 WHERE vehicle_id = %s''',
(request.form.get('vehicle_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})
@bp.route('/activate_vehicle', methods=('POST',))
@login_required
@require_role('manager')
def activate_vehicle():
cur = mysql.connect.cursor()
cur.execute(
'''UPDATE vehicles SET active = 1 WHERE vehicle_id = %s''',
(request.form.get('vehicle_id'),)
)
mysql.connection.commit()
return jsonify({'success': True})