File size: 3,748 Bytes
26053ca
 
 
 
 
 
 
 
 
 
 
 
564a229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26053ca
564a229
 
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
from fastapi import APIRouter,File,Form,UploadFile
import sqlite3
from passlib.context import CryptContext
from os import makedirs,chmod
from os.path import exists
from PIL import Image
from io import BytesIO
UserAddRouter=APIRouter(prefix="/Users")


@UserAddRouter.post("/AddUser")
def adduser(AdminEmail:str=Form(...),AdminPassword:str =Form(...),Email:str=Form(...),Name:str=Form(...),Job:str=Form(...),Phonenumber:str=Form(...),Place:str=Form(...),BDay:str=Form(...),Password:str=Form(...),Images:UploadFile=File(...)):
    try:
        pwd_context=CryptContext(schemes=["bcrypt"],deprecated="auto")
        AdminPassword="$2b$12$33H14GFBvtomzLrEaM6Tqu7Jr621eHlXL9TVtdPU6YCljxc4eug2a"
        if  AdminEmail !="[email protected]":
            if not pwd_context.verify(  AdminPassword,AdminPassword):
                return {"Status":False,"Message":" Admin Email or Password is not correct "}
        pwd_context=CryptContext(schemes=["bcrypt"],deprecated="auto")
        UserId=0
        connect=sqlite3.connect("DataBase/DataBase.bd")
        cursor=connect.execute(f'''
                        SELECT UserId FROM Users where Email='{ Email}'
                        ''')
        Data=cursor.fetchall()
        if len(Data)==1:
            if (Data[0]!=None):
                return  {"Status":False,"Message":"Email Is Alread Exists"}
        cursor=connect.execute('''
                        SELECT MAX(UserId) FROM Users
                        ''')
        Data=cursor.fetchall()
        
        if len(Data)!=0:
            if Data[0][0]==None:
                UserId=0
            else:
                UserId=Data[0][0]+1
        
        HashedPassword=pwd_context.hash( Password)
                
        connect.execute(f'''
                        INSERT INTO Users (UserId ,Email ,UserName ,Job, Phonenumber ,Place,BDay ,Password ) VALUES ({UserId},'{ Email}','{ Name}','{ Job}', '{ Phonenumber}' ,'{ Place}','{ BDay}','{HashedPassword}')
                        ''')
        connect.commit()
        connect.close() 
        if (not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Train")):
            makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Train")
            chmod(f"./FaceRecognition/ExtactedFaces/{UserId}/Train",0o777)
        if (not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/Negative")):
            makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/Negative")
            chmod(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/Negative",0o777)
        if (not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Test")):
            makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Test")
            chmod(f"./FaceRecognition/ExtactedFaces/{UserId}/Test",0o777)
        if (not exists(f"./FaceRecognition/FaceModel/{UserId}")):
            makedirs(f"./FaceRecognition/FaceModel/{UserId}")
            chmod(f"./FaceRecognition/FaceModel/{UserId}",0o777)
        if (not exists(f"./static/{UserId}")):
            makedirs(f"./static/{UserId}")
            chmod(f"./static/{UserId}",0o777)
            Image.open(BytesIO(Images.file.read())).save(f"./static/{UserId}/user.png","PNG")
            chmod(f"./static/{UserId}/user.png",0o777)
        if (not exists(f"./IndoorLocalization/Data/{UserId}")):
            makedirs(f"./IndoorLocalization/Data/{UserId}")
            chmod(f"./IndoorLocalization/Data/{UserId}",0o777)
        if (not exists(f"./IndoorLocalization/IndoorModels/{UserId}")):
            makedirs(f"./IndoorLocalization/IndoorModels/{UserId}")
            chmod(f"./IndoorLocalization/IndoorModels/{UserId}",0o777)
        return {"Status":True,"Message":"User Is Added Correctly"}

    except Exception as e :
        return {"Status":False,"Message":e}