Skip to content

Commit

Permalink
Merge pull request #957 from DSD-DBS/fix-restriction-creation
Browse files Browse the repository at this point in the history
fix: Fix Setting Model Restrictions
MoritzWeber0 authored Sep 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 1e00fc8 + c41ddf8 commit 88a923f
Showing 5 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors
# SPDX-License-Identifier: Apache-2.0

"""Add missing empty model restrictions
Revision ID: d8cf851562cd
Revises: 4c58f4db4f54
Create Date: 2023-08-21 15:45:27.243037
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "d8cf851562cd"
down_revision = "4c58f4db4f54"
branch_labels = None
depends_on = None


def upgrade():
t_restrictions = sa.Table(
"model_restrictions", sa.MetaData(), autoload_with=op.get_bind()
)

restrictions_model_ids = (
op.get_bind()
.execute(sa.text("SELECT model_id FROM model_restrictions"))
.scalars()
.all()
)
models_ids = (
op.get_bind().execute(sa.text("SELECT id FROM models")).scalars().all()
)

model_ids_restrictions_null = set(models_ids) - set(restrictions_model_ids)

for model_id in model_ids_restrictions_null:
op.get_bind().execute(
t_restrictions.insert().values(
model_id=model_id, allow_pure_variants=False
)
)
5 changes: 4 additions & 1 deletion backend/capellacollab/projects/toolmodels/crud.py
Original file line number Diff line number Diff line change
@@ -84,6 +84,8 @@ def create_model(
nature: tools_models.DatabaseNature | None = None,
configuration: dict[str, str] | None = None,
) -> models.DatabaseCapellaModel:
restrictions = restrictions_models.DatabaseToolModelRestrictions()

model = models.DatabaseCapellaModel(
name=post_model.name,
slug=slugify.slugify(post_model.name),
@@ -92,9 +94,10 @@ def create_model(
tool=tool,
version=version,
nature=nature,
restrictions=restrictions_models.DatabaseToolModelRestrictions(),
restrictions=restrictions,
configuration=configuration,
)
db.add(restrictions)
db.add(model)
db.commit()
return model
4 changes: 1 addition & 3 deletions backend/capellacollab/projects/toolmodels/models.py
Original file line number Diff line number Diff line change
@@ -102,9 +102,7 @@ class DatabaseCapellaModel(database.Base):
back_populates="model"
)

restrictions: orm.Mapped[
DatabaseToolModelRestrictions | None
] = orm.relationship(
restrictions: orm.Mapped[DatabaseToolModelRestrictions] = orm.relationship(
back_populates="model", uselist=False, cascade="delete"
)

Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import { ToastService } from 'src/app/helpers/toast/toast.service';
import {
ModelRestrictions,
ModelRestrictionsService,
areRestrictionsEqual,
} from 'src/app/projects/models/model-restrictions/service/model-restrictions.service';
import {
Model,
@@ -64,31 +65,32 @@ export class ModelRestrictionsComponent implements OnInit {
});
}

private mapRestrictionsFormToToolModelRestrictionsObject(): ModelRestrictions {
return {
allow_pure_variants:
this.restrictionsForm.controls.pureVariants.value || false,
};
}

private patchRestrictions() {
if (
JSON.stringify(this.model?.restrictions) ===
JSON.stringify(this.mapRestrictionsFormToToolModelRestrictionsObject())
) {
if (this.model === undefined || this.projectSlug === undefined) {
return;
}

const projectSlug = this.projectSlug;
const modelSlug = this.model.slug;
const restrictions = this.mapRestrictionsFormToModelRestrictions();

if (areRestrictionsEqual(this.model.restrictions, restrictions)) {
return;
}

this.loading = true;
this.modelRestrictionService
.patchModelRestrictions(
this.projectSlug!,
this.model!.slug,
this.mapRestrictionsFormToToolModelRestrictionsObject()
)
.patchModelRestrictions(projectSlug, modelSlug, restrictions)
.subscribe(() => {
this.modelService.loadModelbySlug(this.projectSlug!, this.model!.slug!);
this.modelService.loadModelbySlug(modelSlug, projectSlug);
this.loading = false;
});
}

private mapRestrictionsFormToModelRestrictions(): ModelRestrictions {
return {
allow_pure_variants:
this.restrictionsForm.controls.pureVariants.value || false,
};
}
}
Original file line number Diff line number Diff line change
@@ -26,6 +26,13 @@ export class ModelRestrictionsService {
}
}

export function areRestrictionsEqual(
a: ModelRestrictions,
b: ModelRestrictions
): boolean {
return a.allow_pure_variants === b.allow_pure_variants;
}

export type ModelRestrictions = {
allow_pure_variants: boolean;
};

0 comments on commit 88a923f

Please sign in to comment.