From 3288ac1734a900fba4572aa8cb89512cb8cb4c69 Mon Sep 17 00:00:00 2001 From: Nassim <71079376+nas-tabchiche@users.noreply.github.com> Date: Fri, 3 Jan 2025 09:26:47 +0100 Subject: [PATCH] Prevent assigning parent assets to primary assets (#1302) --- backend/core/views.py | 12 ++++++++++++ .../lib/components/Forms/ModelForm/AssetForm.svelte | 1 + 2 files changed, 13 insertions(+) diff --git a/backend/core/views.py b/backend/core/views.py index 0393f14d4..13e07a6f1 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -382,6 +382,18 @@ class AssetViewSet(BaseModelViewSet): ] search_fields = ["name", "description", "business_value"] + def _perform_write(self, serializer): + type = serializer.validated_data.get("type") + if type == Asset.Type.PRIMARY: + serializer.validated_data["parent_assets"] = [] + serializer.save() + + def perform_create(self, serializer): + return self._perform_write(serializer) + + def perform_update(self, serializer): + return self._perform_write(serializer) + @action(detail=False, name="Get type choices") def type(self, request): return Response(dict(Asset.Type.choices)) diff --git a/frontend/src/lib/components/Forms/ModelForm/AssetForm.svelte b/frontend/src/lib/components/Forms/ModelForm/AssetForm.svelte index 4e924356b..95c2bf0f8 100644 --- a/frontend/src/lib/components/Forms/ModelForm/AssetForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm/AssetForm.svelte @@ -106,6 +106,7 @@ />