diff --git a/fed_mng/api/v1/users.py b/fed_mng/api/v1/users.py index ad17b5f..270a7c8 100644 --- a/fed_mng/api/v1/users.py +++ b/fed_mng/api/v1/users.py @@ -5,8 +5,8 @@ from sqlmodel import Session from fed_mng.api.dependencies import check_user_exists -from fed_mng.api.utils import change_role, create_user, retrieve_users from fed_mng.auth import flaat, security +from fed_mng.crud.users import change_role, create_user, retrieve_users from fed_mng.db import get_session from fed_mng.models import ( Admin, diff --git a/fed_mng/crud/__init__.py b/fed_mng/crud/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fed_mng/api/utils.py b/fed_mng/crud/users.py similarity index 99% rename from fed_mng/api/utils.py rename to fed_mng/crud/users.py index c2d0238..347b39e 100644 --- a/fed_mng/api/utils.py +++ b/fed_mng/crud/users.py @@ -71,6 +71,7 @@ def filter_role( return statement.join(role) elif match_role is False: return statement.join(role, isouter=True).filter(role.id == None) # noqa: E711 + return statement def create_user(session: Session, user: UserCreate) -> User: diff --git a/fed_mng/db.py b/fed_mng/db.py index 3338802..1a6ba69 100644 --- a/fed_mng/db.py +++ b/fed_mng/db.py @@ -7,6 +7,7 @@ from fed_mng import models from fed_mng.config import get_settings +from fed_mng.crud.users import change_role, create_user settings = get_settings() connect_args = {"check_same_thread": False} @@ -19,18 +20,13 @@ def initialize() -> None: with Session(engine) as session: for email, name in zip(settings.ADMIN_EMAIL_LIST, settings.ADMIN_NAME_LIST): statement = select(models.User).filter(models.User.email == email) - user: models.User = session.exec(statement).first() + user = session.exec(statement).first() if not user: - user = models.User(name=name, email=email) - session.add(user) - session.commit() - else: - statement = select(models.Admin).filter(models.Admin.id == user.id) - admin: models.Admin = session.exec(statement).first() - if not admin: - admin = models.Admin(id=user.id) - session.add(admin) - session.commit() + user = create_user(session, models.UserCreate(name=name, email=email)) + statement = select(models.Admin).filter(models.Admin.id == user.id) + admin = session.exec(statement).first() + if not admin: + change_role(session, user, models.Admin, True) @asynccontextmanager