From c5360f70ebbe9816c2130d028bb5932365c3ae3f Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Thu, 5 Dec 2024 17:21:01 +0100 Subject: [PATCH] Add gravity choices endpoint for feared events --- backend/ebios_rm/models.py | 4 ++++ backend/ebios_rm/views.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index 0d03410e22..a2d050858f 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -141,6 +141,10 @@ def save(self, *args, **kwargs): def risk_matrix(self): return self.ebios_rm_study.risk_matrix + @property + def parsed_matrix(self): + return self.risk_matrix.parse_json_translated() + class RoTo(AbstractBaseModel, FolderMixin): class RiskOrigin(models.TextChoices): diff --git a/backend/ebios_rm/views.py b/backend/ebios_rm/views.py index 20a4424cfc..711472c409 100644 --- a/backend/ebios_rm/views.py +++ b/backend/ebios_rm/views.py @@ -45,6 +45,20 @@ def risk_matrix(self, request, pk=None): feared_event = self.get_object() return Response(RiskMatrixReadSerializer(feared_event.risk_matrix).data) + @method_decorator(cache_page(60 * LONG_CACHE_TTL)) + @action(detail=True, name="Get impact choices") + def gravity(self, request, pk): + feared_event: FearedEvent = self.get_object() + undefined = dict([(-1, "--")]) + _choices = dict( + zip( + list(range(0, 64)), + [x["name"] for x in feared_event.parsed_matrix["impact"]], + ) + ) + choices = undefined | _choices + return Response(choices) + class RoToViewSet(BaseModelViewSet): model = RoTo