From 186ac0eab38214aa8803265afbf34af4b6b58d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 29 Mar 2022 00:01:28 +0000 Subject: [PATCH] User.create() methods --- api/kiwi_vpn_api/db/user.py | 27 ++++++++++++++++++++++----- api/kiwi_vpn_api/routers/admin.py | 2 +- api/kiwi_vpn_api/routers/user.py | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/api/kiwi_vpn_api/db/user.py b/api/kiwi_vpn_api/db/user.py index 5e1a4bb..4b1f3fe 100644 --- a/api/kiwi_vpn_api/db/user.py +++ b/api/kiwi_vpn_api/db/user.py @@ -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: """ diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index ca0ad6e..81978b9 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -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() diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index 6beafb0..c54b51c 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -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: