Spaces:
Sleeping
Sleeping
| 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") | |
| 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 !="Mhammed@Admin.com": | |
| 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} |