Skip to content

Commit

Permalink
feat: Display usage of T4C repositories in projects
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzWeber0 committed Oct 10, 2024
1 parent 08e9331 commit 0b7479f
Show file tree
Hide file tree
Showing 46 changed files with 477 additions and 261 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ The Capella Collaboration Manager consists of a couple of components:
External software can also be linked. These parts can be installed separately:

- Optional: A Git server (used for read-only sessions and Git backups)
- Optional: A Team4Capella server
- Optional: A Team for Capella server
- Optional: A pure::variants server

## Contributing
Expand Down
9 changes: 9 additions & 0 deletions backend/capellacollab/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ProjectType(enum.Enum):


class Project(core_pydantic.BaseModel):
id: int
name: str
slug: str
description: str | None = None
Expand Down Expand Up @@ -86,6 +87,14 @@ def transform_users(cls, data: t.Any):
return data


class SimpleProject(core_pydantic.BaseModel):
id: int
name: str
slug: str
visibility: Visibility
type: ProjectType


class PatchProject(core_pydantic.BaseModel):
name: str | None = None
description: str | None = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Backup(core_pydantic.BaseModel):
id: int
k8s_cronjob_id: str | None = None

t4c_model: t4c_models.SimpleT4CModel
t4c_model: t4c_models.SimpleT4CModelWithRepository
git_model: git_models.GitModel
run_nightly: bool
include_commit_history: bool
Expand Down
15 changes: 12 additions & 3 deletions backend/capellacollab/projects/toolmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

from capellacollab.core import database
from capellacollab.core import pydantic as core_pydantic
from capellacollab.projects import models as projects_models

# Importing the modules here does not work as it produces a pydantic error for the CapellaModel
from capellacollab.projects.toolmodels.modelsources.git.models import GitModel
from capellacollab.projects.toolmodels.modelsources.t4c.models import T4CModel
from capellacollab.projects.toolmodels.modelsources.t4c.models import (
SimpleT4CModelWithRepository,
)
from capellacollab.tools import models as tools_models

from .restrictions import models as restrictions_models
Expand Down Expand Up @@ -132,6 +135,12 @@ class ToolModel(core_pydantic.BaseModel):
version: tools_models.ToolVersion | None = None
nature: tools_models.ToolNature | None = None
git_models: list[GitModel] | None = None
t4c_models: list[T4CModel] | None = None

t4c_models: list[SimpleT4CModelWithRepository] | None = None
restrictions: restrictions_models.ToolModelRestrictions | None = None


class SimpleToolModel(core_pydantic.BaseModel):
id: int
slug: str
name: str
project: projects_models.SimpleProject
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import typing as t

import pydantic
import sqlalchemy as sa
from sqlalchemy import orm

Expand Down Expand Up @@ -37,7 +36,7 @@ class DatabaseT4CModel(database.Base):
sa.ForeignKey("t4c_repositories.id"), init=False
)
repository: orm.Mapped[DatabaseT4CRepository] = orm.relationship(
back_populates="models"
back_populates="integrations"
)

model_id: orm.Mapped[int] = orm.mapped_column(
Expand All @@ -60,28 +59,7 @@ class PatchT4CModel(core_pydantic.BaseModel):
t4c_repository_id: int | None = None


class SimpleT4CModel(core_pydantic.BaseModel):
project_name: str
repository_name: str
instance_name: str

@pydantic.model_validator(mode="before")
@classmethod
def transform_database_t4c_model(cls, data: t.Any) -> t.Any:
if isinstance(data, DatabaseT4CModel):
return SimpleT4CModel(
project_name=data.name,
repository_name=data.repository.name,
instance_name=data.repository.instance.name,
)
return data


class T4CModel(core_pydantic.BaseModel):
class SimpleT4CModelWithRepository(core_pydantic.BaseModel):
id: int
name: str
repository: repositories_models.T4CRepository


class T4CRepositoryWithModels(repositories_models.T4CRepository):
models: list[T4CModel]
repository: repositories_models.SimpleT4CRepository
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from capellacollab.core import pydantic as core_pydantic
from capellacollab.projects.toolmodels import models as toolmodels_models


class SimpleT4CModelWithToolModel(core_pydantic.BaseModel):
id: int
name: str
model: toolmodels_models.SimpleToolModel
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

@router.get(
"",
response_model=list[models.T4CModel],
response_model=list[models.SimpleT4CModelWithRepository],
)
def list_t4c_models(
model: toolmodels_models.DatabaseToolModel = fastapi.Depends(
Expand All @@ -53,7 +53,7 @@ def list_t4c_models(

@router.get(
"/{t4c_model_id}",
response_model=models.T4CModel,
response_model=models.SimpleT4CModelWithRepository,
responses=responses.api_exceptions(
[
exceptions.T4CIntegrationNotFoundError(-1),
Expand All @@ -71,7 +71,7 @@ def get_t4c_model(

@router.post(
"",
response_model=models.T4CModel,
response_model=models.SimpleT4CModelWithRepository,
dependencies=[
fastapi.Depends(
auth_injectables.RoleVerification(
Expand Down Expand Up @@ -108,7 +108,7 @@ def create_t4c_model(

@router.patch(
"/{t4c_model_id}",
response_model=models.T4CModel,
response_model=models.SimpleT4CModelWithRepository,
dependencies=[
fastapi.Depends(
auth_injectables.RoleVerification(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from capellacollab.core import pydantic as core_pydantic
from capellacollab.tools import models as tools_models


class SimpleT4CInstance(core_pydantic.BaseModel):
id: int
name: str
version: tools_models.SimpleToolVersion
is_archived: bool
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _get_user_write_t4c_repositories(
) -> abc.Sequence[models.DatabaseT4CRepository]:
stmt = (
sa.select(models.DatabaseT4CRepository)
.join(models.DatabaseT4CRepository.models)
.join(models.DatabaseT4CRepository.integrations)
.join(t4c_models.DatabaseT4CModel.model)
.join(toolmodels_models.DatabaseToolModel.version)
.where(
Expand All @@ -114,7 +114,7 @@ def _get_admin_t4c_repositories(
) -> abc.Sequence[models.DatabaseT4CRepository]:
stmt = (
sa.select(models.DatabaseT4CRepository)
.join(models.DatabaseT4CRepository.models)
.join(models.DatabaseT4CRepository.integrations)
.join(t4c_models.DatabaseT4CModel.model)
.join(toolmodels_models.DatabaseToolModel.version)
.where(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from capellacollab.settings.modelsources.t4c.instance import (
models as t4c_models,
)
from capellacollab.settings.modelsources.t4c.instance import (
models2 as t4c_models2,
)

if t.TYPE_CHECKING:
from capellacollab.projects.toolmodels.modelsources.t4c.models import (
Expand Down Expand Up @@ -45,7 +48,7 @@ class DatabaseT4CRepository(database.Base):
back_populates="repositories"
)

models: orm.Mapped[list[DatabaseT4CModel]] = orm.relationship(
integrations: orm.Mapped[list[DatabaseT4CModel]] = orm.relationship(
back_populates="repository",
cascade="all, delete",
default_factory=list,
Expand All @@ -72,5 +75,6 @@ class T4CRepository(CreateT4CRepository):
status: T4CRepositoryStatus | None = None


class T4CInstanceWithRepositories(t4c_models.T4CInstance):
repositories: list[T4CRepository]
class SimpleT4CRepository(CreateT4CRepository):
id: int
instance: t4c_models2.SimpleT4CInstance
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from capellacollab.core import pydantic as core_pydantic
from capellacollab.projects.toolmodels.modelsources.t4c import (
models2 as t4c_models2,
)
from capellacollab.settings.modelsources.t4c.instance import (
models2 as settings_t4c_models2,
)

from . import models


class SimpleT4CRepositoryWithIntegrations(core_pydantic.BaseModel):
id: int
name: str
status: models.T4CRepositoryStatus | None = None
instance: settings_t4c_models2.SimpleT4CInstance
integrations: list[t4c_models2.SimpleT4CModelWithToolModel]
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

from .. import injectables as settings_t4c_injectables
from .. import models as settings_t4c_models
from . import crud, exceptions, injectables, interface, models
from . import crud, exceptions, injectables, interface, models, models2

router = fastapi.APIRouter()
log = logging.getLogger(__name__)

T4CRepositoriesResponseModel: t.TypeAlias = core_models.PayloadResponseModel[
list[models.T4CRepository]
list[models2.SimpleT4CRepositoryWithIntegrations]
]


Expand All @@ -31,10 +31,10 @@ def list_t4c_repositories(
settings_t4c_injectables.get_existing_instance
),
) -> T4CRepositoriesResponseModel:
repositories = models.T4CInstanceWithRepositories.model_validate(
instance
).repositories

repositories = [
models2.SimpleT4CRepositoryWithIntegrations.model_validate(repository)
for repository in instance.repositories
]
try:
server_repositories = interface.list_repositories(instance)
except requests.RequestException:
Expand Down Expand Up @@ -184,10 +184,10 @@ def recreate_t4c_repository(

def sync_db_with_server_repositories(
db: orm.Session,
db_repos: list[models.T4CRepository],
db_repos: list[models2.SimpleT4CRepositoryWithIntegrations],
server_repos: dict,
instance: settings_t4c_models.DatabaseT4CInstance,
) -> list[models.T4CRepository]:
) -> list[models2.SimpleT4CRepositoryWithIntegrations]:
"""
Synchronize the repository list in the database with the repository list from a server.
Expand Down Expand Up @@ -231,7 +231,7 @@ def sync_db_with_server_repositories(

server_not_db_repo_names = server_repos_names - db_repos_names
for repo_name in server_not_db_repo_names:
repo = models.T4CRepository.model_validate(
repo = models2.SimpleT4CRepositoryWithIntegrations.model_validate(
crud.create_t4c_repository(
db=db, repo_name=repo_name, instance=instance
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from capellacollab.core import exceptions as core_exceptions
from capellacollab.core import models as core_models
from capellacollab.core import pydantic as core_pydantic
from capellacollab.settings.modelsources.t4c.instance import (
models2 as t4c_instance_models2,
)

from . import interface

Expand Down Expand Up @@ -56,18 +59,12 @@ class SimpleLicenseServer(T4CLicenseServerBase):
id: int


class SimpleT4CInstace(core_pydantic.BaseModel):
# Class has to be in this module to avoid circular imports
id: int
name: str


class T4CLicenseServer(T4CLicenseServerBase):
id: int
license_server_version: str | None = None
usage: interface.T4CLicenseServerUsage | None = None
warnings: list[core_models.Message] = []
instances: list[SimpleT4CInstace] = []
instances: list[t4c_instance_models2.SimpleT4CInstance] = []

@pydantic.model_validator(mode="after")
def add_from_api(self) -> t.Any:
Expand Down
Loading

0 comments on commit 0b7479f

Please sign in to comment.