Skip to content

Commit

Permalink
feat: Unify various API endpoints related to config
Browse files Browse the repository at this point in the history
More details in the linked issue.

Closes #1960
  • Loading branch information
zusorio committed Dec 3, 2024
1 parent 5f230dd commit 6cba80d
Show file tree
Hide file tree
Showing 29 changed files with 329 additions and 511 deletions.
16 changes: 0 additions & 16 deletions backend/capellacollab/feedback/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import fastapi
from sqlalchemy import orm

from capellacollab.config import config
from capellacollab.core import database
from capellacollab.core import logging as log
from capellacollab.core.authentication import injectables as auth_injectables
from capellacollab.settings.configuration import core as config_core
from capellacollab.settings.configuration import models as config_models
from capellacollab.users import injectables as user_injectables
from capellacollab.users import models as users_models

Expand All @@ -23,19 +20,6 @@
router = fastapi.APIRouter()


@router.get(
"/configurations/feedback",
response_model=config_models.FeedbackConfiguration,
)
def get_feedback_configuration(
db: orm.Session = fastapi.Depends(database.get_db),
):
feedback = config_core.get_global_configuration(db).feedback
if not (config.smtp and config.smtp.enabled):
util.disable_feedback(feedback)
return feedback


@router.post(
"/feedback",
status_code=204,
Expand Down
45 changes: 0 additions & 45 deletions backend/capellacollab/metadata/routes.py

This file was deleted.

23 changes: 0 additions & 23 deletions backend/capellacollab/navbar/routes.py

This file was deleted.

4 changes: 0 additions & 4 deletions backend/capellacollab/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from capellacollab.events import routes as events_router
from capellacollab.feedback import routes as feedback_routes
from capellacollab.health import routes as health_routes
from capellacollab.metadata import routes as core_metadata
from capellacollab.navbar import routes as navbar_routes
from capellacollab.notices import routes as notices_routes
from capellacollab.projects import routes as projects_routes
from capellacollab.sessions import routes as sessions_routes
Expand All @@ -30,8 +28,6 @@
responses=auth_responses.api_exceptions(include_authentication=True),
tags=["Health"],
)
router.include_router(core_metadata.router, tags=["Metadata"])
router.include_router(navbar_routes.router, tags=["Navbar"])
router.include_router(feedback_routes.router, tags=["Feedback"])
router.include_router(
sessions_routes.router,
Expand Down
26 changes: 19 additions & 7 deletions backend/capellacollab/settings/configuration/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@
from capellacollab.core import database
from capellacollab.core.authentication import injectables as auth_injectables
from capellacollab.feedback import util as feedback_util
from capellacollab.settings.configuration.unifiedconfig import (
routes as unified_config_routes,
)
from capellacollab.users import crud as users_crud
from capellacollab.users import models as users_models

from . import core, crud, models

router = fastapi.APIRouter(
dependencies=[
fastapi.Depends(
auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN
)
)
],
tags=["Configuration"],
)

router.include_router(unified_config_routes.router, tags=["Unified Config"])

schema_router = fastapi.APIRouter(dependencies=[], tags=["Configuration"])


@router.get(
f"/{models.GlobalConfiguration._name}",
response_model=models.GlobalConfiguration,
dependencies=[
fastapi.Depends(
auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN
)
)
],
)
async def get_configuration(
db: orm.Session = fastapi.Depends(database.get_db),
Expand All @@ -41,6 +46,13 @@ async def get_configuration(
@router.put(
f"/{models.GlobalConfiguration._name}",
response_model=models.GlobalConfiguration,
dependencies=[
fastapi.Depends(
auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN
)
)
],
)
async def update_configuration(
body: models.GlobalConfiguration,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from capellacollab.core import pydantic as core_pydantic
from capellacollab.settings.configuration import models as config_models


class Metadata(core_pydantic.BaseModel):
version: str
privacy_policy_url: str | None
imprint_url: str | None
provider: str | None
authentication_provider: str | None
environment: str | None

host: str | None
port: str | None
protocol: str | None


class UnifiedConfig(core_pydantic.BaseModel):
metadata: Metadata
feedback: config_models.FeedbackConfiguration
navbar: config_models.NavbarConfiguration
beta: config_models.BetaConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import fastapi
from sqlalchemy import orm

from capellacollab.core import database
from capellacollab.settings.configuration import core as config_core
from capellacollab.settings.configuration.unifiedconfig import models, util

router = fastapi.APIRouter()


@router.get(
"/unified",
)
def get_unified_config(
db: orm.Session = fastapi.Depends(database.get_db),
) -> models.UnifiedConfig:
cfg = config_core.get_global_configuration(db)

return models.UnifiedConfig(
metadata=util.get_metadata(cfg),
feedback=util.get_feedback(cfg),
beta=util.get_beta(cfg),
navbar=util.get_navbar(cfg),
)
47 changes: 47 additions & 0 deletions backend/capellacollab/settings/configuration/unifiedconfig/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import capellacollab
from capellacollab.config import config
from capellacollab.settings.configuration import models as config_models
from capellacollab.settings.configuration.unifiedconfig import models


def get_metadata(
global_config: config_models.GlobalConfiguration,
) -> models.Metadata:
return models.Metadata.model_validate(
global_config.metadata.model_dump()
| {
"version": capellacollab.__version__,
"host": config.general.host,
"port": str(config.general.port),
"protocol": config.general.scheme,
}
)


def get_feedback(
global_config: config_models.GlobalConfiguration,
) -> config_models.FeedbackConfiguration:
feedback = global_config.feedback
if not (config.smtp and config.smtp.enabled):
feedback.enabled = False
feedback.after_session = False
feedback.on_footer = False
feedback.on_session_card = False
feedback.interval.enabled = False

return feedback


def get_navbar(
global_config: config_models.GlobalConfiguration,
) -> config_models.NavbarConfiguration:
return global_config.navbar


def get_beta(
global_config: config_models.GlobalConfiguration,
) -> config_models.BetaConfiguration:
return global_config.beta
13 changes: 0 additions & 13 deletions backend/capellacollab/users/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from capellacollab.projects.users import crud as projects_users_crud
from capellacollab.sessions import routes as session_routes
from capellacollab.settings.configuration import core as config_core
from capellacollab.settings.configuration import models as config_models
from capellacollab.users import injectables as users_injectables
from capellacollab.users import models as users_models
from capellacollab.users.tokens import routes as tokens_routes
Expand All @@ -41,18 +40,6 @@ def get_current_user(
return user


@router.get(
"/beta",
response_model=config_models.BetaConfiguration,
)
def get_beta_config(db: orm.Session = fastapi.Depends(database.get_db)):
cfg = config_core.get_global_configuration(db)

return config_models.BetaConfiguration.model_validate(
cfg.beta.model_dump()
)


@router.get(
"/{user_id}",
response_model=models.User,
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/settings/test_global_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ def test_metadata_is_updated(

assert response.status_code == 200

response = client.get("/api/v1/metadata")
response = client.get("/api/v1/settings/configurations/unified")
assert response.status_code == 200
assert response.json()["environment"] == "test"
assert response.json()["metadata"]["environment"] == "test"


@pytest.mark.usefixtures("admin")
Expand All @@ -154,9 +154,9 @@ def test_navbar_is_updated(

assert response.status_code == 200

response = client.get("/api/v1/navbar")
response = client.get("/api/v1/settings/configurations/unified")
assert response.status_code == 200
assert response.json()["external_links"][0] == {
assert response.json()["navbar"]["external_links"][0] == {
"name": "Example",
"href": "https://example.com",
"role": "user",
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/test_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def test_feedback_is_updated(

assert response.status_code == 200

response = client.get("/api/v1/configurations/feedback")
response = client.get("/api/v1/settings/configurations/unified")
assert response.status_code == 200
assert response.json() == {
assert response.json()["feedback"] == {
"enabled": True,
"after_session": True,
"on_footer": True,
Expand Down Expand Up @@ -262,9 +262,9 @@ def test_activate_feedback_without_recipients(

@pytest.mark.usefixtures("user", "smtp_config_not_set", "feedback_enabled")
def test_feedback_is_disabled_without_smtp(client: testclient.TestClient):
response = client.get("/api/v1/configurations/feedback")
response = client.get("/api/v1/settings/configurations/unified")
assert response.status_code == 200
assert response.json()["enabled"] is False
assert response.json()["feedback"]["enabled"] is False


def test_feedback_metric(db: orm.Session):
Expand Down
Loading

0 comments on commit 6cba80d

Please sign in to comment.