Skip to content

Commit

Permalink
Merge pull request #1161 from DSD-DBS/model-order-updates
Browse files Browse the repository at this point in the history
feat: Add ability to reorder models in project overview
  • Loading branch information
MoritzWeber0 authored Dec 12, 2023
2 parents 4c0df1d + 01beee6 commit f6e8fb9
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors
# SPDX-License-Identifier: Apache-2.0

"""Add user-determined display order to models
Revision ID: 0e2028f83156
Revises: ac0e6e0f77ee
Create Date: 2023-11-12 14:47:12.295103
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "0e2028f83156"
down_revision = "ac0e6e0f77ee"
branch_labels = None
depends_on = None


def upgrade():
op.add_column(
"models", sa.Column("display_order", sa.Integer(), nullable=True)
)
5 changes: 5 additions & 0 deletions backend/capellacollab/projects/toolmodels/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def create_model(
version: tools_models.DatabaseVersion | None = None,
nature: tools_models.DatabaseNature | None = None,
configuration: dict[str, str] | None = None,
display_order: int | None = None,
) -> models.DatabaseCapellaModel:
restrictions = restrictions_models.DatabaseToolModelRestrictions()

Expand All @@ -96,6 +97,7 @@ def create_model(
nature=nature,
restrictions=restrictions,
configuration=configuration,
display_order=display_order,
)
db.add(restrictions)
db.add(model)
Expand Down Expand Up @@ -133,6 +135,7 @@ def update_model(
version: tools_models.DatabaseVersion,
nature: tools_models.DatabaseNature,
project: projects_model.DatabaseProject,
display_order: int | None,
) -> models.DatabaseCapellaModel:
model.version = version
model.nature = nature
Expand All @@ -142,6 +145,8 @@ def update_model(
if name:
model.name = name
model.slug = slugify.slugify(name)
if display_order:
model.display_order = display_order
db.commit()
return model

Expand Down
3 changes: 3 additions & 0 deletions backend/capellacollab/projects/toolmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PatchCapellaModel(pydantic.BaseModel):
version_id: int | None = None
nature_id: int | None = None
project_slug: str | None = None
display_order: int | None = None


class ToolDetails(pydantic.BaseModel):
Expand All @@ -72,6 +73,7 @@ class DatabaseCapellaModel(database.Base):
name: orm.Mapped[str] = orm.mapped_column(index=True)
slug: orm.Mapped[str]
description: orm.Mapped[str]
display_order: orm.Mapped[int | None]

configuration: orm.Mapped[dict[str, str] | None]

Expand Down Expand Up @@ -116,6 +118,7 @@ class CapellaModel(pydantic.BaseModel):
slug: str
name: str
description: str
display_order: int | None
tool: tools_models.ToolBase
version: tools_models.ToolVersionBase | None = None
nature: tools_models.ToolNatureBase | None = None
Expand Down
9 changes: 8 additions & 1 deletion backend/capellacollab/projects/toolmodels/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ def patch_tool_model(
new_project = model.project

return crud.update_model(
db, model, body.description, body.name, version, nature, new_project
db,
model,
body.description,
body.name,
version,
nature,
new_project,
body.display_order,
)


Expand Down
18 changes: 18 additions & 0 deletions backend/tests/projects/toolmodels/test_toolmodel_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@ def test_rename_toolmodel_where_name_already_exists(
assert response.status_code == 409
assert "A model with a similar name already exists" in response.text
mock_get_model_by_slugs.assert_called_once()


def test_update_toolmodel_order_successful(
capella_model: toolmodels_models.DatabaseCapellaModel,
project: projects_models.DatabaseProject,
client: testclient.TestClient,
executor_name: str,
db: orm.Session,
):
users_crud.create_user(db, executor_name, users_models.Role.ADMIN)

response = client.patch(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}",
json={"display_order": 1},
)

assert response.status_code == 200
assert "1" in response.text
135 changes: 135 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import { EditProjectMetadataComponent } from './projects/project-detail/edit-pro
import { ModelComplexityBadgeComponent } from './projects/project-detail/model-overview/model-complexity-badge/model-complexity-badge.component';
import { ModelOverviewComponent } from './projects/project-detail/model-overview/model-overview.component';
import { MoveModelComponent } from './projects/project-detail/model-overview/move-model/move-model.component';
import { ReorderModelsDialogComponent } from './projects/project-detail/model-overview/reorder-models-dialog/reorder-models-dialog.component';
import { ProjectDetailsComponent } from './projects/project-detail/project-details.component';
import { ProjectMetadataComponent } from './projects/project-detail/project-metadata/project-metadata.component';
import { AddUserToProjectDialogComponent } from './projects/project-detail/project-users/add-user-to-project/add-user-to-project.component';
Expand Down Expand Up @@ -215,6 +216,7 @@ import { UsersProfileComponent } from './users/users-profile/users-profile.compo
ProjectUserSettingsComponent,
ProjectWrapperComponent,
PureVariantsComponent,
ReorderModelsDialogComponent,
SessionComponent,
SessionIFrameComponent,
SessionOverviewComponent,
Expand Down
54 changes: 40 additions & 14 deletions frontend/src/app/projects/models/service/model.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
AsyncValidatorFn,
ValidationErrors,
} from '@angular/forms';
import { BehaviorSubject, map, Observable, take, tap } from 'rxjs';
import { BehaviorSubject, forkJoin, map, Observable, take, tap } from 'rxjs';
import slugify from 'slugify';
import { ModelRestrictions } from 'src/app/projects/models/model-restrictions/service/model-restrictions.service';
import { T4CModel } from 'src/app/projects/models/model-source/t4c/service/t4c-model.service';
Expand Down Expand Up @@ -92,24 +92,48 @@ export class ModelService {
);
}

private applyModelPatch(
projectSlug: string,
modelSlug: string,
patchData: PatchModel,
): Observable<Model> {
return this.http.patch<Model>(
`${this.base_url}${projectSlug}/models/${modelSlug}/`,
patchData,
);
}

updateModel(
projectSlug: string,
modelSlug: string,
patchModel: PatchModel,
): Observable<Model> {
return this.http
.patch<Model>(
`${this.base_url}${projectSlug}/models/${modelSlug}/`,
patchModel,
)
.pipe(
tap({
next: (model) => {
this.loadModels(projectSlug);
this._model.next(model);
},
}),
);
return this.applyModelPatch(projectSlug, modelSlug, patchModel).pipe(
tap({
next: (model) => {
this.loadModels(projectSlug);
this._model.next(model);
},
}),
);
}

updateModels(
projectSlug: string,
modelUpdates: { modelSlug: string; patchModel: PatchModel }[],
): Observable<Model[]> {
const updateObservables = modelUpdates.map(({ modelSlug, patchModel }) =>
this.applyModelPatch(projectSlug, modelSlug, patchModel),
);

return forkJoin(updateObservables).pipe(
tap({
next: (models: Model[]) => {
this.loadModels(projectSlug);
this._model.next(models[models.length - 1]);
},
}),
);
}

deleteModel(projectSlug: string, modelSlug: string): Observable<void> {
Expand Down Expand Up @@ -184,6 +208,7 @@ export type Model = {
t4c_models: T4CModel[];
git_models: GetGitModel[];
restrictions: ModelRestrictions;
display_order: number;
};

export type PatchModel = {
Expand All @@ -192,6 +217,7 @@ export type PatchModel = {
nature_id?: number;
version_id?: number;
project_slug?: string;
display_order?: number;
};

export function getPrimaryGitModel(model: Model): GetGitModel | undefined {
Expand Down
Loading

0 comments on commit f6e8fb9

Please sign in to comment.