from ..models import users from ..dependencies import database from passlib.context import CryptContext def add(username="", password="", roles="User", disabled=False, confirmed=True): 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, disabled=disabled, confirmed=confirmed) if result is not None: result.password=pwd_context.hash(password) result.roles=roles result.disabled=disabled result.confirmed=confirmed user = result change = "updated" user_repository.save(user) print("{0} {1}".format(username, change))