Skip to content

Commit

Permalink
Fix choices for feared event gravity on create
Browse files Browse the repository at this point in the history
  • Loading branch information
nas-tabchiche committed Dec 6, 2024
1 parent c597f00 commit 414e106
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
4 changes: 4 additions & 0 deletions backend/ebios_rm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions backend/ebios_rm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ export const load: PageServerLoad = async ({ params, fetch }) => {

const selectOptions: Record<string, any> = {};

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;
Expand Down

0 comments on commit 414e106

Please sign in to comment.