diff --git a/backend/capellacollab/alembic/versions/d8cf851562cd_add_missing_empty_model_restrictions.py b/backend/capellacollab/alembic/versions/d8cf851562cd_add_missing_empty_model_restrictions.py new file mode 100644 index 000000000..c341024c0 --- /dev/null +++ b/backend/capellacollab/alembic/versions/d8cf851562cd_add_missing_empty_model_restrictions.py @@ -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 + ) + ) diff --git a/backend/capellacollab/projects/toolmodels/crud.py b/backend/capellacollab/projects/toolmodels/crud.py index 1e39342e9..8032f945a 100644 --- a/backend/capellacollab/projects/toolmodels/crud.py +++ b/backend/capellacollab/projects/toolmodels/crud.py @@ -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 diff --git a/backend/capellacollab/projects/toolmodels/models.py b/backend/capellacollab/projects/toolmodels/models.py index 62437248f..0ad5badbe 100644 --- a/backend/capellacollab/projects/toolmodels/models.py +++ b/backend/capellacollab/projects/toolmodels/models.py @@ -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" ) diff --git a/frontend/src/app/projects/models/model-restrictions/model-restrictions.component.ts b/frontend/src/app/projects/models/model-restrictions/model-restrictions.component.ts index b84586163..bce5e7c5c 100644 --- a/frontend/src/app/projects/models/model-restrictions/model-restrictions.component.ts +++ b/frontend/src/app/projects/models/model-restrictions/model-restrictions.component.ts @@ -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, + }; + } } diff --git a/frontend/src/app/projects/models/model-restrictions/service/model-restrictions.service.ts b/frontend/src/app/projects/models/model-restrictions/service/model-restrictions.service.ts index 15d32dde7..cbadff5c5 100644 --- a/frontend/src/app/projects/models/model-restrictions/service/model-restrictions.service.ts +++ b/frontend/src/app/projects/models/model-restrictions/service/model-restrictions.service.ts @@ -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; };