Skip to content

Commit

Permalink
Reordering files and re-using functions
Browse files Browse the repository at this point in the history
  • Loading branch information
giosava94 committed Apr 23, 2024
1 parent 153e1ce commit ba0a3f8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fed_mng/api/v1/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Empty file added fed_mng/crud/__init__.py
Empty file.
1 change: 1 addition & 0 deletions fed_mng/api/utils.py → fed_mng/crud/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 7 additions & 11 deletions fed_mng/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit ba0a3f8

Please sign in to comment.