23 lines
839 B
Python
Raw Normal View History

2023-10-13 21:44:30 +02:00
from ..models import users
from ..dependencies import database
from passlib.context import CryptContext
2023-10-14 23:04:19 +02:00
from pydantic import EmailStr
2023-10-13 21:44:30 +02:00
2023-10-22 18:58:35 +02:00
def add(username="", password="", roles="User", status=1, email="test@toto.com"):
2023-10-13 22:18:41 +02:00
user_repository = users.UserRepository(database=database.database)
2023-10-13 21:44:30 +02:00
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
2023-10-13 22:18:41 +02:00
result = user_repository.find_one_by({'username': username})
change = "added"
2023-10-22 18:58:35 +02:00
user = users.User(username=username, password=pwd_context.hash(password), status=status, email=email)
2023-10-13 22:18:41 +02:00
if result is not None:
result.password=pwd_context.hash(password)
result.roles=roles
2023-10-22 18:58:35 +02:00
result.status=status
2023-10-14 23:04:19 +02:00
result.email=email
2023-10-13 22:18:41 +02:00
user = result
change = "updated"
2023-10-13 21:44:30 +02:00
user_repository.save(user)
2023-10-13 22:18:41 +02:00
print("{0} {1}".format(username, change))