-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
731 lines (645 loc) · 35.9 KB
/
main.py
File metadata and controls
731 lines (645 loc) · 35.9 KB
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import os
import hashlib
from qrme import *
from database import *
from PIL import Image
from flask_cors import CORS
from datetime import datetime
from flask_cors.decorator import cross_origin
from flask import Flask, render_template, request, redirect, jsonify, send_file
db = DataBase(path=os.path.join(os.getcwd(), "db"), filename="b52.db")
db.create_table(name="users",
labels={"id": DBType.TEXT,
"user_name": DBType.TEXT,
"first_name": DBType.TEXT,
"second_name": DBType.TEXT,
"patronymic": DBType.TEXT,
"company_id": DBType.TEXT,
"tasks": DBType.TEXT,
"categories": DBType.TEXT,
"phone": DBType.TEXT,
"email": DBType.TEXT,
"password": DBType.TEXT},
primary_key="id")
db.create_table(name="company",
labels={"id": DBType.TEXT,
"admin": DBType.TEXT,
"company_name": DBType.TEXT,
"employees": DBType.TEXT,
"locations": DBType.TEXT,
"tasks": DBType.TEXT,
"categories": DBType.TEXT,
"licenses": DBType.INTEGER},
primary_key="id")
db.create_table(name="task",
labels={"id": DBType.TEXT,
"description": DBType.TEXT,
"location": DBType.TEXT,
"status": DBType.TEXT,
"category": DBType.TEXT,
"executor": DBType.TEXT,
"create_at": DBType.TEXT},
primary_key="id")
db.create_table(name="location",
labels={"id": DBType.TEXT,
"name": DBType.TEXT,
"floor": DBType.INTEGER,
"room": DBType.TEXT,
"url": DBType.TEXT},
primary_key="id")
db.create_table(name="category",
labels={"id": DBType.TEXT,
"name": DBType.TEXT})
app = Flask(__name__)
CORS(app)
tokens = {}
hostname = "http://127.0.0.1:5000/"
# =========================================================USER=========================================================
@app.route('/api/login', methods=['POST'])
@cross_origin()
def user_login() -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if get_hash(my_string=api_json['email']) not in db.get_table("users").get_all_UIDs():
return jsonify(message="There is no such user!"), 401
user = db.get_table("users").get_row(key=get_hash(my_string=api_json['email']))
if user["password"] != api_json['password']:
return jsonify(message="Password is incorrect!"), 401
token = user["id"] + get_hash(str(datetime.now()))
tokens[token] = user["id"]
return jsonify({"user": get_user(user_id=user["id"]).get_json(),
"token": token})
@app.route('/api/registerUser', methods=['POST'])
@cross_origin()
def register_user() -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
user_id = get_hash(api_json['email'])
company_id = get_hash(api_json['company_name'])
if user_id in db.get_table("users").get_all_UIDs():
return jsonify(message='User already exist!'), 401
if "@" not in api_json['email']:
return jsonify(message='The email must contain the "@" symbol!'), 401
if company_id not in db.get_table("company").get_all_UIDs():
db_add_company(company_id=company_id,
admin=user_id,
company_name=api_json['company_name'],
employees=user_id,
locations="",
tasks="",
categories="",
licenses=20)
else:
employees = str(db.get_table("company").get_from_cell(key=company_id, column_name="employees")).split(",")
db.get_table("company").set_to_cell(key=company_id, column_name="employees",
new_value=",".join(list(filter(None, employees + [user_id]))))
db_add_user(user_id=user_id,
user_name=api_json['first_name'],
first_name=api_json['first_name'],
second_name=api_json['second_name'],
patronymic=api_json['patronymic'],
company_id=company_id,
tasks="",
categories="",
phone=api_json['phone'],
email=api_json['email'],
password=api_json['password'])
add_location(company_id=company_id,
name="Example of name",
floor="1",
room="1")
token = user_id + get_hash(str(datetime.now()))
tokens[token] = user_id
return jsonify({"user": get_user(user_id=user_id).get_json(),
"token": token})
@app.route('/api/user/<string:user_id>', methods=['GET'])
@cross_origin()
def get_user(user_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if user_id not in db.get_table("users").get_all_UIDs():
return jsonify(message='User not founded!'), 401
user = db.get_table("users").get_row(user_id)
company = db.get_table("company").get_row(user['company_id'])
return jsonify({"id": user["id"],
"username": user["user_name"],
"first_name": user["first_name"],
"second_name": user["second_name"],
"patronymic": user["patronymic"],
"phone": user["phone"],
"categories": [get_company_category(company['id'], category_id).get_json()
for category_id in list(filter(None, user['categories'].split(",")))],
"user_role": "ADMIN" if user["id"] == company["admin"] else "EMPLOYEE",
"company": {"id": user['company_id'],
"name": company["company_name"],
"licenses": company["licenses"]}})
@app.route('/api/user/<string:user_id>/changePassword', methods=['POST'])
@cross_origin()
def change_password(user_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if user_id not in db.get_table("users").get_all_UIDs():
return jsonify(message='User not founded!'), 401
if api_json['password'] != db.get_table("users").get_row(key=user_id)["password"]:
return jsonify({"success": False})
db.get_table("users").set_to_cell(key=user_id,
column_name="password",
new_value=request.args.get('new_password'))
return jsonify(success=True)
def db_add_user(user_id: str, user_name: str, first_name: str, second_name: str, patronymic: str,
company_id: str, tasks: str, categories: str, phone: str, email: str, password: str) -> None:
db.get_table("users").add_row(row=[user_id, user_name, first_name, second_name, patronymic,
company_id, tasks, categories, phone, email, password])
def get_user_short_name(user_id: str) -> str:
user = db.get_table("users").get_row(key=user_id)
return f"{user['second_name']} {user['first_name'][0].upper()}.{user['patronymic'][0].upper()}."
# =========================================================COMPANY======================================================
@app.route('/api/company/<string:company_id>', methods=['GET'])
@cross_origin()
def get_company_info(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
company = db.get_table("company").get_row(key=company_id)
return jsonify({"id": company["id"],
"name": company["company_name"],
"licenses": company["licenses"]})
@app.route('/api/company/<string:company_id>', methods=['PATCH'])
@cross_origin()
def change_company_info(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
db.get_table("company").set_to_cell(key=company_id, column_name="company_name", new_value=api_json['name'])
db.get_table("company").set_to_cell(key=company_id, column_name="licenses", new_value=api_json['licenses'])
return jsonify(success=True)
@app.route('/api/company/<string:company_id>/employees', methods=['GET'])
@cross_origin()
def get_company_employees(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
items = []
for employ in db.get_table("company").get_row(key=company_id)['employees'].split(","):
try:
user = db.get_table("users").get_row(key=employ)
items.append({"id": user["id"], "full_name": get_user_short_name(user["id"])})
except:
pass
return jsonify({"items": items})
@app.route('/api/company/<string:company_id>/registerEmployee', methods=['POST'])
@cross_origin()
def company_register_employee(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if get_hash(api_json['email']) in db.get_table("users").get_all_UIDs():
return jsonify(message='This user already exist!'), 401
company = db.get_table("company").get_row(key=company_id)
if get_hash(api_json['email']) in company['employees'].split(","):
return jsonify(message="This user already exist"), 401
db_add_user(user_id=get_hash(api_json['email']),
user_name=api_json['first_name'],
first_name=api_json['first_name'],
second_name=api_json['second_name'],
patronymic=api_json['patronymic'],
company_id=company_id,
tasks="",
categories=",".join(list(filter(None, api_json['categories']))),
phone="8 (000) 000-00-00",
email=api_json['email'],
password=get_hash(api_json['first_name']))
db.get_table("company").set_to_cell(key=company_id,
column_name="employees",
new_value=",".join(list(filter(None, company['employees'].split(",") +
[get_hash(api_json['email'])]))))
return jsonify({"id": get_hash(api_json['email']),
"email": api_json['email'],
"first_name": api_json['first_name'],
"second_name": api_json['second_name'],
"patronymic": api_json['patronymic'],
"categories": [get_company_category(company_id, category_id).get_json()
for category_id in list(filter(None, api_json['categories']))],
"password": get_hash(api_json['first_name'])})
@app.route('/api/company/<string:company_id>/employee/<string:user_id>', methods=['PATCH'])
@cross_origin()
def company_chenge_employee(company_id: str, user_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if user_id not in db.get_table("users").get_all_UIDs():
return jsonify(message='User not founded!'), 401
for field in ["first_name", "second_name", "patronymic", "phone"]:
db.get_table("users").set_to_cell(key=user_id, column_name=field, new_value=api_json[field])
db.get_table("users").set_to_cell(key=user_id, column_name="categories",
new_value=",".join(list(filter(None, api_json['categories']))))
return jsonify(success=True)
@app.route('/api/company/<string:company_id>/employee/<string:user_id>', methods=['DELETE'])
@cross_origin()
def company_delete_employee(company_id: str, user_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if user_id not in db.get_table("users").get_all_UIDs():
return jsonify(message='User not founded!'), 401
employees = db.get_table("company").get_row(key=company_id)['employees'].split(",")
if user_id not in employees:
return jsonify(message='User not founded in this company!'), 401
employees.remove(user_id)
db.get_table("company").set_to_cell(key=company_id,
column_name="employees",
new_value=",".join(list(filter(None, employees))))
db.get_table("users").delete_row(key=user_id)
return jsonify(success=True)
def db_add_company(company_id: str, admin: str, company_name: str,
employees: str, locations: str, tasks: str, categories: str, licenses: int) -> None:
db.get_table("company").add_row(row=[company_id, admin, company_name, employees,
locations, tasks, categories, licenses])
# ========================================================TASKS=========================================================
@app.route('/api/company/<string:company_id>/task', methods=['POST'])
@cross_origin()
def create_company_task(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
task_id = f"{company_id}{get_hash(str(datetime.now()))}"
db_add_task(task_id=task_id,
description=api_json['description'],
location=api_json['location'],
status="OPEN",
category=api_json['category'],
executor="",
create_at=datetime.now().strftime("%Y-%m-%d"))
tasks = db.get_table("company").get_from_cell(key=company_id, column_name='tasks').split(",")
db.get_table("company").set_to_cell(key=company_id,
column_name='tasks',
new_value=",".join(list(filter(None, tasks + [task_id]))))
return get_company_task(company_id, task_id)
@app.route('/api/company/<string:company_id>/task/<string:task_id>', methods=['GET'])
@cross_origin()
def get_company_task(company_id: str, task_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("task").get_all_UIDs():
return jsonify(message='Task not founded!'), 401
if task_id not in db.get_table("company").get_row(key=company_id)['tasks'].split(","):
return jsonify(message='Task not founded in this company!'), 401
task = db.get_table("task").get_row(key=task_id)
return jsonify({"id": task['id'],
"description": task['description'],
"location": {"id": task['location'],
"name": db.get_table("location").get_row(key=task['location'])['name']},
"status": task['status'],
"category": {"id": task["category"],
"name": db.get_table('category').get_from_cell(task['category'], "name")}
if task['category'] != "" else None,
"executor": {"id": task["executor"],
"full_name": get_user_short_name(task["executor"])}
if task["executor"] != "" else None,
"create_at": task['create_at']})
@app.route('/api/company/<string:company_id>/task/<string:task_id>', methods=['PATCH'])
@cross_origin()
def set_company_task(company_id: str, task_id: str) -> jsonify:
api_json = request.get_json()
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("task").get_all_UIDs():
return jsonify(message='Task not founded!'), 401
db.get_table("task").set_to_cell(key=task_id, column_name="description", new_value=api_json['description'])
db.get_table("task").set_to_cell(key=task_id, column_name="status", new_value=api_json['status'])
db.get_table("task").set_to_cell(key=task_id, column_name="create_at", new_value=api_json['create_at'])
db.get_table("task").set_to_cell(key=task_id,
column_name="location",
new_value=api_json["location"]['id'] if api_json["location"] is not None else "")
db.get_table("task").set_to_cell(key=task_id,
column_name="category",
new_value=api_json["category"]['id'] if api_json["category"] is not None else "")
db.get_table("task").set_to_cell(key=task_id,
column_name="executor",
new_value=api_json["executor"]['id'] if api_json["executor"] is not None else "")
return get_company_task(company_id=company_id, task_id=task_id).get_json()
@app.route('/api/company/<string:company_id>/task/<string:task_id>/addExecutor', methods=['POST'])
@cross_origin()
def company_task_add_executor(company_id: str, task_id: str) -> jsonify:
api_json = request.get_json()
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("task").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("company").get_row(key=company_id)['tasks'].split(","):
return jsonify(message='Task not founded in this company!'), 401
if task_id in db.get_table("users").get_row(key=api_json['user_id'])['tasks'].split(","):
return jsonify(message='The user has already been assigned responsibility for this task!'), 401
executors = db.get_table("task").get_from_cell(key=task_id, column_name="executor").split(",")
db.get_table("task").set_to_cell(key=task_id,
column_name="executor",
new_value=",".join(list(filter(None, executors + [api_json['user_id']]))))
user_tasks = db.get_table("users").get_from_cell(key=api_json['user_id'],
column_name="tasks").split(",")
db.get_table("users").set_to_cell(key=api_json['user_id'],
column_name="tasks",
new_value=",".join(list(filter(None, user_tasks + [task_id]))))
return get_company_task(company_id=company_id, task_id=task_id)
@app.route('/api/company/<string:company_id>/task/<string:task_id>/removeExecutor', methods=['POST'])
@cross_origin()
def company_task_remove_executor(company_id: str, task_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("task").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if task_id not in db.get_table("company").get_row(key=company_id)['tasks'].split(","):
return jsonify(message='Task not founded in this company!'), 401
if task_id not in db.get_table("users").get_row(key=api_json['user_id'])['tasks'].split(","):
return jsonify(message='The user was not assigned responsibility for this task!'), 401
executors: list = db.get_table("task").get_from_cell(key=task_id, column_name="executor").split(",")
user_tasks = db.get_table("users").get_from_cell(key=api_json['user_id'], column_name="tasks").split(",")
executors.remove(api_json['user_id'])
user_tasks.remove(task_id)
db.get_table("task").set_to_cell(key=task_id,
column_name="executor",
new_value=",".join(list(filter(None, executors))))
db.get_table("users").set_to_cell(key=api_json['user_id'],
column_name="tasks",
new_value=",".join(list(filter(None, user_tasks))))
return get_company_task(company_id=company_id, task_id=task_id).get_json()
@app.route('/api/company/<string:company_id>/tasks', methods=['GET'])
@cross_origin()
def get_company_tasks(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
company_tasks = []
for task_id in list(filter(None, db.get_table("company").get_from_cell(company_id, "tasks").split(','))):
try:
this_task = get_company_task(company_id, task_id).get_json()
if 'message' not in this_task:
company_tasks.append(this_task)
except:
pass
return jsonify({"items": company_tasks})
@app.route('/api/company/<string:company_id>/tasks/<string:user_id>', methods=['GET'])
@cross_origin()
def get_company_user_tasks(company_id: str, user_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if user_id not in db.get_table("users").get_all_UIDs():
return jsonify(message='User not founded!'), 401
user_tasks = []
for task_id in list(filter(None, db.get_table("users").get_from_cell(user_id, "tasks").split(','))):
try:
this_task = get_company_task(company_id, task_id).get_json()
if 'message' not in this_task:
user_tasks.append(this_task)
except:
pass
return jsonify({"items": user_tasks})
@app.route('/api/company/<string:company_id>/tasks/free', methods=['GET'])
@cross_origin()
def get_company_free_tasks(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
company_free_tasks = []
for task_id in list(filter(None, db.get_table("company").get_from_cell(company_id, "tasks").split(','))):
try:
this_task = get_company_task(company_id, task_id).get_json()
if 'message' not in this_task:
if this_task['executor'] == "" or this_task['executor'] is None or \
this_task['executor']["id"] == "" or this_task['executor']["id"] is None:
company_free_tasks.append(this_task)
except:
pass
return jsonify({"items": company_free_tasks})
def db_add_task(task_id: str, description: str, location: str,
status: str, category: str, executor: str, create_at: str) -> None:
db.get_table("task").add_row(row=[task_id, description, location, status, category, executor, create_at])
# ======================================================LOCATION========================================================
@app.route('/api/company/<string:company_id>/location', methods=['POST'])
@cross_origin()
def add_company_location(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
location_id = add_location(company_id, api_json['name'], api_json['floor'], api_json['room'])
return get_company_location(company_id=company_id, location_id=location_id).get_json()
@app.route('/api/company/<string:company_id>/location/<string:location_id>', methods=['GET'])
@cross_origin()
def get_company_location(company_id: str, location_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if location_id not in db.get_table("company").get_row(key=company_id)['locations'].split(","):
return jsonify(message='Location not founded!'), 401
location = db.get_table("location").get_row(key=location_id)
return jsonify({"id": location['id'],
"name": location['name'],
"floor": location['floor'],
"room": location['room'],
"url": location['url']})
@app.route('/api/company/<string:company_id>/location/<string:location_id>', methods=['PATCH'])
@cross_origin()
def change_company_location(company_id: str, location_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if location_id not in db.get_table("company").get_row(key=company_id)['locations'].split(","):
return jsonify(message='Location not founded!'), 401
for field in ["name", "floor", "room"]:
db.get_table("location").set_to_cell(key=location_id, column_name=field, new_value=api_json[field])
return jsonify(success=True)
@app.route('/api/company/<string:company_id>/locations', methods=['GET'])
@cross_origin()
def get_company_locations(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
locations = []
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
for location_id in db.get_table("company").get_row(key=company_id)['locations'].split(","):
try:
location = db.get_table("location").get_row(key=location_id)
if 'message' not in location:
locations.append({"id": location['id'],
"name": location['name'],
"floor": location['floor'],
"room": location['room'],
"url": location['url']})
except:
pass
return jsonify(locations)
@app.route('/api/company/<string:company_id>/locations/grouped', methods=['GET'])
@cross_origin()
def get_company_grouped_locations(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
locations = {}
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
for location_id in list(filter(None, db.get_table("company").get_row(company_id)['locations'].split(","))):
try:
location = db.get_table("location").get_row(key=location_id)
if 'message' not in location:
if location['floor'] not in locations:
locations[location['floor']] = {"floor": location['floor'], "locations": []}
locations[location['floor']]["locations"].append({"id": location['id'],
"name": location['name'],
"floor": location['floor'],
"room": location['room'],
"url": location['url']})
except:
pass
return jsonify([locations[floor] for floor in sorted(locations.keys())])
def add_location(company_id: str, name: str, floor: str, room: str):
company = db.get_table("company").get_row(key=company_id)
location_id = company_id + get_hash(name) + get_hash(str(datetime.now()))
if location_id in str(company['locations']).split(","):
return jsonify(message="Locations already exist!"), 401
create_qrcode(data=f"{hostname}#/company/{company_id}/location/{location_id}/createTask",
path_to_folder=os.path.join(os.getcwd(), "qrcodes"),
filename=f"{location_id}.png")
db_add_location(location_id=location_id, name=name, floor=floor, room=room, url=f"/api/get_image/{location_id}.png")
db.get_table('company').set_to_cell(key=company_id,
column_name="locations",
new_value=",".join(list(filter(None, company['locations'].split(",") +
[location_id]))))
return location_id
def db_add_location(location_id: str, name: str, floor: str, room: str, url: str) -> None:
db.get_table("location").add_row(row=[location_id, name, floor, room, url])
# =======================================================CATEGORY=======================================================
@app.route('/api/company/<string:company_id>/category', methods=['POST'])
@cross_origin()
def set_company_category(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if f"{company_id}{get_hash(api_json['name'])}" in db.get_table("category").get_all_UIDs():
return jsonify(message='This category is already exist!'), 401
category_id = f"{company_id}{get_hash(api_json['name'])}"
db_add_category(category_id=category_id, name=api_json['name'])
company_categories = db.get_table("company").get_from_cell(key=company_id, column_name="categories").split(",")
db.get_table("company").set_to_cell(key=company_id,
column_name="categories",
new_value=",".join(list(filter(None, company_categories + [category_id]))))
return jsonify({"id": category_id,
"name": api_json['name']})
@app.route('/api/company/<string:company_id>/category/<string:category_id>', methods=['GET'])
@cross_origin()
def get_company_category(company_id: str, category_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if category_id not in db.get_table("category").get_all_UIDs():
return jsonify(message='Category not founded!'), 401
return jsonify({"id": category_id,
"name": db.get_table("category").get_from_cell(key=category_id, column_name='name')})
@app.route('/api/company/<string:company_id>/category/<string:category_id>', methods=['PATCH'])
@cross_origin()
def change_company_category(company_id: str, category_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
api_json = request.get_json()
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if category_id not in db.get_table("category").get_all_UIDs():
return jsonify(message='Category not founded!'), 401
db.get_table("category").set_to_cell(key=category_id, column_name='name', new_value=api_json['name'])
return jsonify(success=True)
@app.route('/api/company/<string:company_id>/category/<string:category_id>', methods=['DELETE'])
@cross_origin()
def delete_company_category(company_id: str, category_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
if category_id not in db.get_table("category").get_all_UIDs():
return jsonify(message='Category not founded!'), 401
db.get_table("category").delete_row(key=category_id)
try:
categories = list(filter(None, db.get_table("company").get_from_cell(company_id, "categories").split(",")))
if category_id in categories:
categories.remove(category_id)
db.get_table("company").set_to_cell(key=company_id, column_name="categories",
new_value=",".join(list(filter(None, categories))))
except:
pass
employees = db.get_table("company").get_from_cell(key=company_id, column_name="employees").split(",")
for user_id in list(filter(None, employees)):
user_categories = db.get_table("users").get_from_cell(key=user_id, column_name="categories").split(",")
if category_id in user_categories:
user_categories.remove(category_id)
db.get_table("users").set_to_cell(key=user_id,
column_name="categories",
new_value=",".join(list(filter(None, user_categories))))
for tasks_id in db.get_table("company").get_from_cell(key=company_id, column_name="tasks").split(","):
db.get_table("task").set_to_cell(key=tasks_id, column_name="category", new_value="")
return jsonify(success=True)
@app.route('/api/company/<string:company_id>/categories', methods=['GET'])
@cross_origin()
def get_company_categories(company_id: str) -> jsonify:
if not check_api_key():
return jsonify(message="Invalid API-KEY"), 401
if company_id not in db.get_table("company").get_all_UIDs():
return jsonify(message='Company not founded!'), 401
categories = []
for category_id in list(filter(None, db.get_table("company").get_from_cell(company_id, "categories").split(","))):
try:
category = get_company_category(company_id=company_id, category_id=category_id).get_json()
if 'message' not in category:
categories.append(category)
except:
pass
return jsonify({"items": categories})
@app.route('/api/get_image/<string:image>.png', methods=['GET'])
@cross_origin()
def get_image(image: str) -> send_file:
if f"{image}.png" not in os.listdir(os.path.join(os.getcwd(), "qrcodes")):
return jsonify(message='Image not founded!'), 401
return send_file(os.path.join(os.getcwd(), "qrcodes", f"{image}.png"), mimetype='image/png')
def db_add_category(category_id: str, name: str) -> None:
db.get_table("category").add_row(row=[category_id, name])
def get_hash(my_string: str) -> str:
"""
Этот метод на вход получает строку (в нашем часном случае - почту) и хэширует её
:param my_string: Входная строка
:return:
"""
return hashlib.md5(my_string.encode()).hexdigest()
def check_api_key() -> bool:
return request.headers.get('API-KEY') == "6kcDRDO!0B<;^MCM=bv'jyMO?(R)c/j0YIpx[>!Q*%kX;&99B^'xgQ_=}}R-5:fkme1"
if __name__ == '__main__':
app.run(debug=True)