User.create() methods

This commit is contained in:
Jörn-Michael Miehe 2022-03-29 00:01:28 +00:00
parent 865e712ea5
commit 186ac0eab3
3 changed files with 24 additions and 7 deletions

View file

@ -90,25 +90,42 @@ class User(UserBase, table=True):
)
@classmethod
def create(cls, **kwargs) -> User | None:
def create(
cls,
*,
user: UserCreate,
) -> User | None:
"""
Create a new user in the database.
"""
try:
with Connection.session as db:
user = cls.from_orm(UserCreate(**kwargs))
new_user = cls.from_orm(user)
db.add(user)
db.add(new_user)
db.commit()
db.refresh(user)
db.refresh(new_user)
return user
return new_user
except IntegrityError:
# user already existed
return None
@classmethod
def create_kwargs(
cls,
**kwargs
) -> User | None:
"""
Create a new user in the database. Keywords version.
"""
return cls.create(
user=UserCreate(**kwargs),
)
@classmethod
def get(cls, name: str) -> User | None:
"""

View file

@ -63,7 +63,7 @@ async def create_initial_admin(
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
# create an administrative user
new_user = User.create(**admin_user.dict())
new_user = User.create(admin_user)
new_user.set_capabilities([UserCapabilityType.admin])
new_user.update()

View file

@ -87,7 +87,7 @@ async def add_user(
"""
# actually create the new user
new_user = User.create(**user.dict())
new_user = User.create(user=user)
# fail if creation was unsuccessful
if new_user is None: