File size: 2,405 Bytes
2647087
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4dc0b3
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
from fastapi import APIRouter,File,Form,UploadFile
import sqlite3

from os.path import exists
from os import makedirs,chmod

from PIL import Image
from io import BytesIO

UserItemAddRouter=APIRouter(prefix="/Users")
@UserItemAddRouter.post("/AddUserItem")
def adduser(Email:str=Form(...),Name:str=Form(...),Password:str=Form(...),Images:UploadFile=File(...)):
    try:
        
        State=False
        connect=sqlite3.connect("DataBase/DataBase.bd")
        cursor=connect.execute(f'''
                        SELECT UserId,Password FROM Users where Email='{ Email}'
                        ''')

        FData=cursor.fetchall()
        
        
        
        if len(FData) !=0 :
            if FData[0][0]==None:
                return  {"Status":False,"Message":"Email or Password Is Incorrect "}
            HasedPassword=FData[0][1]
            UserId=FData[0][0]
            State= Password==HasedPassword
        
        Name= Name
        if not State:
            return {"Status":False,"Message":"Email or Password is not correct"}
        
        if not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/{Name}"):
            connect.execute(f'''
                            INSERT INTO UserItems (UserId ,UserItemName  ) VALUES ({UserId},'{Name}')
                            ''')
            connect.commit()
            if not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/{Name}"):
                makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/{Name}")
                chmod(f"./FaceRecognition/ExtactedFaces/{UserId}/Train/{Name}",0o777)
            if not exists(f"./FaceRecognition/ExtactedFaces/{UserId}/Test/{Name}"):
                makedirs(f"./FaceRecognition/ExtactedFaces/{UserId}/Test/{Name}")
                chmod(f"./FaceRecognition/ExtactedFaces/{UserId}/Test/{Name}",0o777)
            if not exists(f"./static/{UserId}/{Name}"):
                makedirs(f"./static/{UserId}/{Name}")
                chmod(f"./static/{UserId}/{Name}",0o777)
                Image.open(BytesIO(Images.file.read())).save(f"./static/{UserId}/{Name}/user.png","PNG")
                chmod(f"./static/{UserId}/{Name}/user.png",0o777)
            return {"Status":True,"Message":"User Is Added Correctly"}
        else:
            return {"Status":False,"Message":"Name Is Already Exist"}
    except Exception as e:
        return {"Status":False,"Message":e}