From 3e40bd3315708056ee12df99153a43054152bbf7 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Thu, 12 Dec 2024 17:54:45 +0100 Subject: [PATCH 01/29] feat: custom ro-to views --- ...oto_pertinence_alter_roto_feared_events.py | 26 +++ backend/ebios_rm/models.py | 24 ++- backend/ebios_rm/serializers.py | 2 +- backend/ebios_rm/views.py | 4 - frontend/messages/en.json | 4 +- .../src/lib/components/Forms/ModelForm.svelte | 2 +- .../Forms/ModelForm/RoToForm.svelte | 185 +++++++++++------- frontend/src/lib/utils/crud.ts | 21 +- frontend/src/lib/utils/schemas.ts | 3 +- .../ebios-rm/[id=uuid]/+page.svelte | 6 +- .../ro-to/[id=uuid]/+page.server.ts | 22 +++ .../(internal)/ro-to/[id=uuid]/+page.svelte | 163 +++++++++++++++ .../ro-to/[id=uuid]/edit/+page.server.ts | 65 ++++++ .../ro-to/[id=uuid]/edit/+page.svelte | 17 ++ 14 files changed, 440 insertions(+), 104 deletions(-) create mode 100644 backend/ebios_rm/migrations/0003_remove_roto_pertinence_alter_roto_feared_events.py create mode 100644 frontend/src/routes/(app)/(internal)/ro-to/[id=uuid]/+page.server.ts create mode 100644 frontend/src/routes/(app)/(internal)/ro-to/[id=uuid]/+page.svelte create mode 100644 frontend/src/routes/(app)/(internal)/ro-to/[id=uuid]/edit/+page.server.ts create mode 100644 frontend/src/routes/(app)/(internal)/ro-to/[id=uuid]/edit/+page.svelte diff --git a/backend/ebios_rm/migrations/0003_remove_roto_pertinence_alter_roto_feared_events.py b/backend/ebios_rm/migrations/0003_remove_roto_pertinence_alter_roto_feared_events.py new file mode 100644 index 000000000..a41a04989 --- /dev/null +++ b/backend/ebios_rm/migrations/0003_remove_roto_pertinence_alter_roto_feared_events.py @@ -0,0 +1,26 @@ +# Generated by Django 5.1.4 on 2024-12-12 16:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("ebios_rm", "0002_alter_roto_target_objective"), + ] + + operations = [ + migrations.RemoveField( + model_name="roto", + name="pertinence", + ), + migrations.AlterField( + model_name="roto", + name="feared_events", + field=models.ManyToManyField( + blank=True, + related_name="ro_to_couples", + to="ebios_rm.fearedevent", + verbose_name="Feared events", + ), + ), + ] diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index 7b8853494..eccada823 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -202,7 +202,10 @@ class Pertinence(models.IntegerChoices): on_delete=models.CASCADE, ) feared_events = models.ManyToManyField( - FearedEvent, verbose_name=_("Feared events"), related_name="ro_to_couples" + FearedEvent, + verbose_name=_("Feared events"), + related_name="ro_to_couples", + blank=True, ) risk_origin = models.CharField( @@ -219,11 +222,6 @@ class Pertinence(models.IntegerChoices): choices=Resources.choices, default=Resources.UNDEFINED, ) - pertinence = models.PositiveSmallIntegerField( - verbose_name=_("Pertinence"), - choices=Pertinence.choices, - default=Pertinence.UNDEFINED, - ) activity = models.PositiveSmallIntegerField( verbose_name=_("Activity"), default=0, validators=[MaxValueValidator(4)] ) @@ -242,6 +240,20 @@ def save(self, *args, **kwargs): self.folder = self.ebios_rm_study.folder super().save(*args, **kwargs) + @property + def get_pertinence(self): + PERTINENCE_MATRIX = [ + [1, 1, 2, 2], + [1, 2, 3, 3], + [2, 3, 3, 4], + [2, 3, 4, 4], + ] + if self.motivation == 0 or self.resources == 0: + return self.Pertinence(self.Pertinence.UNDEFINED).label + return self.Pertinence( + PERTINENCE_MATRIX[self.motivation - 1][self.resources - 1] + ).label + class Stakeholder(AbstractBaseModel, FolderMixin): class Category(models.TextChoices): diff --git a/backend/ebios_rm/serializers.py b/backend/ebios_rm/serializers.py index 2d81ac5c9..3eed45bf0 100644 --- a/backend/ebios_rm/serializers.py +++ b/backend/ebios_rm/serializers.py @@ -94,9 +94,9 @@ class RoToReadSerializer(BaseModelSerializer): folder = FieldsRelatedField() feared_events = FieldsRelatedField(["folder", "id"], many=True) - pertinence = serializers.CharField(source="get_pertinence_display") motivation = serializers.CharField(source="get_motivation_display") resources = serializers.CharField(source="get_resources_display") + pertinence = serializers.CharField(source="get_pertinence") class Meta: model = RoTo diff --git a/backend/ebios_rm/views.py b/backend/ebios_rm/views.py index bf5ec10b4..8955411e0 100644 --- a/backend/ebios_rm/views.py +++ b/backend/ebios_rm/views.py @@ -107,10 +107,6 @@ def motivation(self, request): def resources(self, request): return Response(dict(RoTo.Resources.choices)) - @action(detail=False, name="Get pertinence choices") - def pertinence(self, request): - return Response(dict(RoTo.Pertinence.choices)) - class StakeholderViewSet(BaseModelViewSet): model = Stakeholder diff --git a/frontend/messages/en.json b/frontend/messages/en.json index a58960796..13c465eaa 100644 --- a/frontend/messages/en.json +++ b/frontend/messages/en.json @@ -926,6 +926,7 @@ "ebiosRmMatrixHelpText": "Risk matrix used as a reference for the study. Defaults to `urn:intuitem:risk:library:risk-matrix-4x4-ebios-rm`", "activityOne": "Activity 1", "activityTwo": "Activity 2", + "activityThree": "Activity 3", "ebiosRmStudy": "Ebios RM study", "qualifications": "Qualifications", "impacts": "Impacts", @@ -979,5 +980,6 @@ "attackPath": "Attack path", "attackPaths": "Attack paths", "currentCriticality": "Current criticality", - "residualCriticality": "Residual criticality" + "residualCriticality": "Residual criticality", + "notSelected": "Not selected" } diff --git a/frontend/src/lib/components/Forms/ModelForm.svelte b/frontend/src/lib/components/Forms/ModelForm.svelte index c4b150cde..feada4836 100644 --- a/frontend/src/lib/components/Forms/ModelForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm.svelte @@ -266,7 +266,7 @@ {:else if URLModel === 'feared-events'} {:else if URLModel === 'ro-to'} - + {:else if URLModel === 'stakeholders'} {:else if URLModel === 'attack-paths'} diff --git a/frontend/src/lib/components/Forms/ModelForm/RoToForm.svelte b/frontend/src/lib/components/Forms/ModelForm/RoToForm.svelte index 683e6accd..a78db0a56 100644 --- a/frontend/src/lib/components/Forms/ModelForm/RoToForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm/RoToForm.svelte @@ -1,7 +1,6 @@