File size: 3,036 Bytes
bf7beeb
 
 
 
 
 
 
 
 
 
 
 
 
2442c30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d308fc
2442c30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf7beeb
2442c30
 
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
from fastapi import APIRouter,File,Form,UploadFile
import sqlite3
from passlib.context import CryptContext
from os import makedirs
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")
    if (not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/Negative")):
        makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/Negative")
    if (not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Test")):
        makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Test")
    if (not exists(f"./FaceRecognition/FaceModel/{UserId}")):
        makedirs(f"./FaceRecognition/FaceModel/{UserId}")
    if (not exists(f"./static/{UserId}")):
        makedirs(f"./static/{UserId}")
        Image.open(BytesIO(Images.file.read())).save(f"./static/{UserId}/user.png","PNG")
    if (not exists(f"./IndoorLocalization/Data/{UserId}")):
        makedirs(f"./IndoorLocalization/Data/{UserId}")
    if (not exists(f"./IndoorLocalization/IndoorModels/{UserId}")):
        makedirs(f"./IndoorLocalization/IndoorModels/{UserId}")
        
    return {"Status":True,"Message":"User Is Added Correctly"}

    # except sqlite3.Error as e :
    #     return {"Status":False,"Message":e}