Skip to content

Commit

Permalink
Merge pull request #1623 from DSD-DBS/t4c-instance-adjustments
Browse files Browse the repository at this point in the history
refactor: Add stories, use generated frontend client and Angular control flow
  • Loading branch information
MoritzWeber0 authored Jun 20, 2024
2 parents 2a6e31c + bb929d2 commit bef9037
Show file tree
Hide file tree
Showing 96 changed files with 1,117 additions and 994 deletions.
4 changes: 2 additions & 2 deletions backend/capellacollab/core/database/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def create_default_models(db: orm.Session):
capella_model = toolmodels_crud.create_model(
db=db,
project=default_project,
post_model=toolmodels_models.PostCapellaModel(
post_model=toolmodels_models.PostToolModel(
name=f"Melody Model Test {version}",
description="",
tool_id=capella_tool.id,
Expand Down Expand Up @@ -434,7 +434,7 @@ def create_coffee_machine_model(db: orm.Session):
capella_model = toolmodels_crud.create_model(
db=db,
project=coffee_machine_project,
post_model=toolmodels_models.PostCapellaModel(
post_model=toolmodels_models.PostToolModel(
name="Coffee Machine",
description="An open source model of a coffee machine",
tool_id=capella_tool.id,
Expand Down
39 changes: 38 additions & 1 deletion backend/capellacollab/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import typing as t

import fastapi
import pydantic

from capellacollab.core import pydantic as core_pydantic
Expand Down Expand Up @@ -104,7 +105,43 @@ def _translate_exceptions_to_openapi_schema(excs: list[exceptions.BaseError]):
...,
),
__base__=pydantic.RootModel,
)
),
}
for status_code, excs in grouped_by_status_code.items()
}


class SVGResponse(fastapi.responses.Response):
"""Custom error class for SVG responses.
To use the class as response class, pass the following parameters
to the fastapi route definition.
```python
response_class=fastapi.responses.Response
responses=responses.SVGResponse.responses
```
Don't use SVGResponse as response_class as this will also change the
media type for all error responses, see:
https://github.com/tiangolo/fastapi/discussions/6799
To return an SVG response in the route, use:
```python
return responses.SVGResponse(
content=b"<svg>...</svg>",
)
```
"""

media_type = "image/svg+xml"
responses: dict[int | str, dict[str, t.Any]] | None = {
200: {
"content": {
"image/svg+xml": {
"schema": {"type": "string", "format": "binary"}
}
}
}
}
2 changes: 1 addition & 1 deletion backend/capellacollab/projects/toolmodels/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_model_by_slugs(
def create_model(
db: orm.Session,
project: projects_model.DatabaseProject,
post_model: models.PostCapellaModel,
post_model: models.PostToolModel,
tool: tools_models.DatabaseTool,
version: tools_models.DatabaseVersion | None = None,
nature: tools_models.DatabaseNature | None = None,
Expand Down
8 changes: 5 additions & 3 deletions backend/capellacollab/projects/toolmodels/diagrams/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import capellacollab.projects.toolmodels.modelsources.git.injectables as git_injectables
from capellacollab.core import logging as log
from capellacollab.core import responses
from capellacollab.core.authentication import injectables as auth_injectables
from capellacollab.projects.toolmodels.diagrams import models
from capellacollab.projects.toolmodels.modelsources.git.handler import (
Expand Down Expand Up @@ -62,7 +63,9 @@ async def get_diagram_metadata(


@router.get(
"/{diagram_uuid_or_filename}", response_class=fastapi.responses.Response
"/{diagram_uuid_or_filename}",
response_class=fastapi.responses.Response,
responses=responses.SVGResponse.responses,
)
async def get_diagram(
diagram_uuid_or_filename: str,
Expand All @@ -86,7 +89,6 @@ async def get_diagram(
logger.info("Failed fetching diagram", exc_info=True)
raise exceptions.DiagramCacheNotConfiguredProperlyError()

return fastapi.responses.Response(
return responses.SVGResponse(
content=diagram,
media_type="image/svg+xml",
)
10 changes: 7 additions & 3 deletions backend/capellacollab/projects/toolmodels/modelbadge/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import capellacollab.projects.toolmodels.modelsources.git.injectables as git_injectables
from capellacollab.core import logging as log
from capellacollab.core import responses
from capellacollab.core.authentication import injectables as auth_injectables
from capellacollab.projects.toolmodels.modelsources.git.handler import handler
from capellacollab.projects.users import models as projects_users_models
Expand All @@ -28,21 +29,24 @@
)


@router.get("", response_class=fastapi.responses.Response)
@router.get(
"",
response_class=fastapi.responses.Response,
responses=responses.SVGResponse.responses,
)
async def get_model_complexity_badge(
git_handler: handler.GitHandler = fastapi.Depends(
git_injectables.get_git_handler
),
logger: logging.LoggerAdapter = fastapi.Depends(log.get_request_logger),
):
try:
return fastapi.responses.Response(
return responses.SVGResponse(
content=(
await git_handler.get_file_from_repository_or_artifacts(
"model-complexity-badge.svg", "generate-model-badge"
)
)[1],
media_type="image/svg+xml",
)
except (aiohttp.web.HTTPException, requests.exceptions.HTTPError):
logger.info("Failed fetching model complexity badge", exc_info=True)
Expand Down
6 changes: 3 additions & 3 deletions backend/capellacollab/projects/toolmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class EditingMode(enum.Enum):
GIT = "git"


class PostCapellaModel(core_pydantic.BaseModel):
class PostToolModel(core_pydantic.BaseModel):
name: str
description: str | None = None
tool_id: int


class PatchCapellaModel(core_pydantic.BaseModel):
class PatchToolModel(core_pydantic.BaseModel):
name: str | None = None
description: str | None = None
version_id: int | None = None
Expand Down Expand Up @@ -122,7 +122,7 @@ class DatabaseToolModel(database.Base):
)


class CapellaModel(core_pydantic.BaseModel):
class ToolModel(core_pydantic.BaseModel):
id: int
slug: str
name: str
Expand Down
12 changes: 6 additions & 6 deletions backend/capellacollab/projects/toolmodels/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


@router.get(
"", response_model=list[models.CapellaModel], tags=["Projects - Models"]
"", response_model=list[models.ToolModel], tags=["Projects - Models"]
)
def get_models(
project: projects_models.DatabaseProject = fastapi.Depends(
Expand All @@ -48,7 +48,7 @@ def get_models(

@router.get(
"/{model_slug}",
response_model=models.CapellaModel,
response_model=models.ToolModel,
tags=["Projects - Models"],
)
def get_model_by_slug(
Expand All @@ -61,7 +61,7 @@ def get_model_by_slug(

@router.post(
"",
response_model=models.CapellaModel,
response_model=models.ToolModel,
dependencies=[
fastapi.Depends(
auth_injectables.ProjectRoleVerification(
Expand All @@ -72,7 +72,7 @@ def get_model_by_slug(
tags=["Projects - Models"],
)
def create_new_tool_model(
new_model: models.PostCapellaModel,
new_model: models.PostToolModel,
project: projects_models.DatabaseProject = fastapi.Depends(
projects_injectables.get_existing_project
),
Expand Down Expand Up @@ -104,7 +104,7 @@ def create_new_tool_model(

@router.patch(
"/{model_slug}",
response_model=models.CapellaModel,
response_model=models.ToolModel,
dependencies=[
fastapi.Depends(
auth_injectables.ProjectRoleVerification(
Expand All @@ -115,7 +115,7 @@ def create_new_tool_model(
tags=["Projects - Models"],
)
def patch_tool_model(
body: models.PatchCapellaModel,
body: models.PatchToolModel,
project: projects_models.DatabaseProject = fastapi.Depends(
projects_injectables.get_existing_project
),
Expand Down
2 changes: 1 addition & 1 deletion backend/capellacollab/settings/modelsources/t4c/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


@router.get("", response_model=list[models.T4CInstance])
def list_t4c_settings(
def get_t4c_instances(
db: orm.Session = fastapi.Depends(database.get_db),
) -> abc.Sequence[models.DatabaseT4CInstance]:
return crud.get_t4c_instances(db)
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/projects/toolmodels/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def fixture_capella_model(
project: projects_models.DatabaseProject,
capella_tool_version: tools_models.DatabaseVersion,
) -> toolmodels_models.DatabaseToolModel:
model = toolmodels_models.PostCapellaModel(
model = toolmodels_models.PostToolModel(
name="test", description="test", tool_id=capella_tool_version.tool.id
)
return toolmodels_crud.create_model(
Expand All @@ -37,7 +37,7 @@ def fixture_jupyter_model(
project: projects_models.DatabaseProject,
jupyter_tool: tools_models.DatabaseTool,
) -> toolmodels_models.DatabaseToolModel:
jupyter_model = toolmodels_models.PostCapellaModel(
jupyter_model = toolmodels_models.PostToolModel(
name="Jupyter test",
description="",
tool_id=jupyter_tool.id,
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/projects/toolmodels/pipelines/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fixture_include_commit_history(
@pytest.fixture(name="pipeline")
def fixture_pipeline(
db: orm.Session,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
git_model: git_models.DatabaseGitModel,
t4c_model: t4c_models.T4CModel,
executor_name: str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def fetch_logs_from_loki(
def test_create_pipeline_run(
db: orm.Session,
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
):
Expand All @@ -72,7 +72,7 @@ def test_create_pipeline_run(
@pytest.mark.usefixtures("project_manager")
def test_create_pipeline_run_with_custom_environment(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
):
Expand All @@ -89,7 +89,7 @@ def test_create_pipeline_run_with_custom_environment(

def test_get_pipeline_runs(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
pipeline_run: pipeline_runs_models.DatabasePipelineRun,
Expand All @@ -105,7 +105,7 @@ def test_get_pipeline_runs(

def test_get_pipeline_run(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
pipeline_run: pipeline_runs_models.DatabasePipelineRun,
Expand All @@ -121,7 +121,7 @@ def test_get_pipeline_run(
@pytest.mark.usefixtures("patch_loki")
def test_get_events(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
pipeline_run: pipeline_runs_models.DatabasePipelineRun,
Expand All @@ -136,7 +136,7 @@ def test_get_events(
@pytest.mark.usefixtures("patch_loki")
def def_get_logs(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
pipeline: pipelines_models.DatabaseBackup,
pipeline_run: pipeline_runs_models.DatabasePipelineRun,
Expand Down
10 changes: 5 additions & 5 deletions backend/tests/projects/toolmodels/pipelines/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def fixture_mockoperator(monkeypatch: pytest.MonkeyPatch):
@pytest.mark.usefixtures("project_manager", "mockoperator", "pipeline")
def test_get_all_pipelines_of_capellamodel(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
client: testclient.TestClient,
run_nightly: bool,
include_commit_history: bool,
Expand All @@ -73,7 +73,7 @@ def test_get_all_pipelines_of_capellamodel(
)
def test_create_pipeline_of_capellamodel_git_model_does_not_exist(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
t4c_model: models_t4c_models.T4CModel,
client: testclient.TestClient,
):
Expand All @@ -99,7 +99,7 @@ def test_create_pipeline_of_capellamodel_git_model_does_not_exist(
def test_create_pipeline(
db: orm.Session,
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
t4c_model: models_t4c_models.T4CModel,
git_model: git_models.GitModel,
client: testclient.TestClient,
Expand Down Expand Up @@ -129,7 +129,7 @@ def test_create_pipeline(
)
def test_pipeline_creation_fails_if_t4c_server_not_available(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
t4c_model: models_t4c_models.T4CModel,
git_model: git_models.GitModel,
client: testclient.TestClient,
Expand Down Expand Up @@ -174,7 +174,7 @@ def mock_add_user_to_repository(
def test_delete_pipeline(
db: orm.Session,
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
capella_model: toolmodels_models.ToolModel,
pipeline: pipelines_models.DatabaseBackup,
client: testclient.TestClient,
monkeypatch: pytest.MonkeyPatch,
Expand Down
Loading

0 comments on commit bef9037

Please sign in to comment.