From 414e1067fabcdc6a68cf98705ffad9d199a35138 Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Fri, 6 Dec 2024 12:29:37 +0100 Subject: [PATCH] Fix choices for feared event gravity on create --- backend/ebios_rm/models.py | 4 +++ backend/ebios_rm/views.py | 14 +++++++++ frontend/src/lib/utils/schemas.ts | 2 +- .../feared-events/+page.server.ts | 29 ++++++++----------- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index a2d050858..6617f0d98 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -101,6 +101,10 @@ class Meta: verbose_name_plural = _("Ebios RM Studies") ordering = ["created_at"] + @property + def parsed_matrix(self): + return self.risk_matrix.parse_json_translated() + class FearedEvent(NameDescriptionMixin, FolderMixin): ebios_rm_study = models.ForeignKey( diff --git a/backend/ebios_rm/views.py b/backend/ebios_rm/views.py index 47cfac6b2..af0360603 100644 --- a/backend/ebios_rm/views.py +++ b/backend/ebios_rm/views.py @@ -32,6 +32,20 @@ class EbiosRMStudyViewSet(BaseModelViewSet): def status(self, request): return Response(dict(EbiosRMStudy.Status.choices)) + @method_decorator(cache_page(60 * LONG_CACHE_TTL)) + @action(detail=True, name="Get gravity choices") + def gravity(self, request, pk): + study: EbiosRMStudy = self.get_object() + undefined = dict([(-1, "--")]) + _choices = dict( + zip( + list(range(0, 64)), + [x["name"] for x in study.parsed_matrix["impact"]], + ) + ) + choices = undefined | _choices + return Response(choices) + class FearedEventViewSet(BaseModelViewSet): model = FearedEvent diff --git a/frontend/src/lib/utils/schemas.ts b/frontend/src/lib/utils/schemas.ts index 16e2e136c..413d7c45c 100644 --- a/frontend/src/lib/utils/schemas.ts +++ b/frontend/src/lib/utils/schemas.ts @@ -405,7 +405,7 @@ export const ebiosRMSchema = z.object({ export const fearedEventsSchema = z.object({ ...NameDescriptionMixin, ref_id: z.string().optional(), - gravity: z.number().optional(), + gravity: z.number().optional().default(-1), is_selected: z.boolean().optional(), justification: z.string().optional(), ebios_rm_study: z.string(), diff --git a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/workshop-one/feared-events/+page.server.ts b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/workshop-one/feared-events/+page.server.ts index 12d667e74..852754409 100644 --- a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/workshop-one/feared-events/+page.server.ts +++ b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/workshop-one/feared-events/+page.server.ts @@ -29,23 +29,18 @@ export const load: PageServerLoad = async ({ params, fetch }) => { const selectOptions: Record = {}; - if (model.selectFields) { - for (const selectField of model.selectFields) { - const url = `${BASE_API_URL}/${model.endpointUrl ?? URLModel}/${ - selectField.detail ? params.id + '/' : '' - }${selectField.field}/`; - const response = await fetch(url); - if (response.ok) { - selectOptions[selectField.field] = await response.json().then((data) => - Object.entries(data).map(([key, value]) => ({ - label: value, - value: selectField.valueType === 'number' ? parseInt(key) : key - })) - ); - } else { - console.error(`Failed to fetch data for ${selectField.field}: ${response.statusText}`); - } - } + const gravityChoicesEndpoint = `${BASE_API_URL}/ebios-rm/studies/${params.id}/gravity/`; + const gravityChoicesResponse = await fetch(gravityChoicesEndpoint); + + if (gravityChoicesResponse.ok) { + selectOptions['gravity'] = await gravityChoicesResponse.json().then((data) => + Object.entries(data).map(([key, value]) => ({ + label: value, + value: parseInt(key) + })) + ); + } else { + console.error(`Failed to fetch data for gravity: ${gravityChoicesResponse.statusText}`); } model['selectOptions'] = selectOptions;