2023-10-25 23:42:34 +02:00

23 lines
852 B
Python

from ..models import users
from ..dependencies import database
from passlib.context import CryptContext
from pydantic import EmailStr
def add(username="", password="", roles="User", status=1, email="test@toto.com"):
user_repository = users.UserRepository(database=database.database)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
result = user_repository.find_one_by({'username': username})
change = "added"
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, status=status, email=email)
if result is not None:
result.password=pwd_context.hash(password)
result.roles=roles
result.status=status
result.email=email
user = result
change = "updated"
user_repository.save(user)
print("{0} {1}".format(username, change))