Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workshop 2 #1134

Merged
merged 9 commits into from
Dec 9, 2024
17 changes: 17 additions & 0 deletions backend/ebios_rm/migrations/0002_alter_roto_target_objective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.1 on 2024-12-06 14:51

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ebios_rm", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="roto",
name="target_objective",
field=models.TextField(verbose_name="Target objective"),
),
]
4 changes: 1 addition & 3 deletions backend/ebios_rm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ class Pertinence(models.IntegerChoices):
risk_origin = models.CharField(
max_length=32, verbose_name=_("Risk origin"), choices=RiskOrigin.choices
)
target_objective = models.CharField(
max_length=200, verbose_name=_("Target objective")
)
target_objective = models.TextField(verbose_name=_("Target objective"))
motivation = models.PositiveSmallIntegerField(
verbose_name=_("Motivation"),
choices=Motivation.choices,
Expand Down
6 changes: 5 additions & 1 deletion backend/ebios_rm/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ class RoToReadSerializer(BaseModelSerializer):
str = serializers.CharField(source="__str__")
ebios_rm_study = FieldsRelatedField()
folder = FieldsRelatedField()
feared_events = FieldsRelatedField(many=True)
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")

class Meta:
model = RoTo
Expand Down
4 changes: 4 additions & 0 deletions backend/ebios_rm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def gravity(self, request, pk):
class RoToViewSet(BaseModelViewSet):
model = RoTo

filterset_fields = [
"ebios_rm_study",
]

@action(detail=False, name="Get risk origin choices", url_path="risk-origin")
def risk_origin(self, request):
return Response(dict(RoTo.RiskOrigin.choices))
Expand Down
27 changes: 25 additions & 2 deletions frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@
"exportDatabase": "Export database",
"upload": "Upload",
"add": "Add",
"undefined": "--",
"undefined": "undefined",
"production": "Production",
"design": "Design",
"development": "Development",
Expand Down Expand Up @@ -937,5 +937,28 @@
"fearedEvent": "Feared event",
"fearedEvents": "Feared events",
"isSelected": "Is selected",
"ebiosRM": "Ebios RM"
"ebiosRM": "Ebios RM",
"riskOrigin": "Risk origin",
"targetObjective": "Target objective",
"motivation": "Motivation",
"resources": "Resources",
"pertinence": "Pertinence",
"limited": "Limited",
"significant": "Significant",
"important": "Important",
"unlimited": "Unlimited",
"strong": "Strong",
"irrelevant": "Irrelevant",
"partiallyRelevant": "Partially relevant",
"fairlyRelevant": "Fairly relevant",
"highlyRelevant": "Highly relevant",
"roTo": "RO/TO",
"addRoto": "Add RO/TO couple",
"organizedCrime": "Organized crime",
"terrorist": "Terrorist",
"activist": "Activist",
"professional": "Professional",
"amateur": "Amateur",
"avenger": "Avenger",
"pathological": "Pathological"
}
3 changes: 3 additions & 0 deletions frontend/src/lib/components/Forms/ModelForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import GeneralSettingsForm from './ModelForm/GeneralSettingForm.svelte';
import EbiosRmForm from './ModelForm/EbiosRmForm.svelte';
import FearedEventForm from './ModelForm/FearedEventForm.svelte';
import RoToForm from './ModelForm/RoToForm.svelte';

import AutocompleteSelect from './AutocompleteSelect.svelte';

Expand Down Expand Up @@ -261,6 +262,8 @@
<EbiosRmForm {form} {model} {cacheLocks} {formDataCache} {context} />
{:else if URLModel === 'feared-events'}
<FearedEventForm {form} {model} {cacheLocks} {formDataCache} {initialData} />
{:else if URLModel === 'ro-to'}
<RoToForm {form} {model} {cacheLocks} {formDataCache} {initialData} />
{/if}
<div class="flex flex-row justify-between space-x-4">
{#if closeModal}
Expand Down
93 changes: 93 additions & 0 deletions frontend/src/lib/components/Forms/ModelForm/RoToForm.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<script lang="ts">
import type { SuperValidated } from 'sveltekit-superforms';
import type { ModelInfo, CacheLock } from '$lib/utils/types';
import TextField from '$lib/components/Forms/TextField.svelte';
import Checkbox from '$lib/components/Forms/Checkbox.svelte';
import AutocompleteSelect from '$lib/components/Forms/AutocompleteSelect.svelte';
import Select from '$lib/components/Forms/Select.svelte';
import * as m from '$paraglide/messages.js';
import { getOptions } from '$lib/utils/crud';
import TextArea from '../TextArea.svelte';
import NumberField from '../NumberField.svelte';

export let form: SuperValidated<any>;
export let model: ModelInfo;
export let cacheLocks: Record<string, CacheLock> = {};
export let formDataCache: Record<string, any> = {};
export let initialData: Record<string, any> = {};
</script>

<AutocompleteSelect
{form}
options={getOptions({ objects: model.foreignKeys['ebios_rm_study'] })}
field="ebios_rm_study"
cacheLock={cacheLocks['ebios_rm_study']}
bind:cachedValue={formDataCache['ebios_rm_study']}
label={m.ebiosRmStudy()}
hidden={initialData.ebios_rm_study}
/>
<AutocompleteSelect
multiple
{form}
options={getOptions({
objects: model.foreignKeys['feared_events'],
extra_fields: [['folder', 'str']],
label: 'auto'
})}
field="feared_events"
label={m.fearedEvents()}
/>
<Select
{form}
options={model.selectOptions['risk-origin']}
field="risk_origin"
label={m.riskOrigin()}
cacheLock={cacheLocks['risk_origin']}
bind:cachedValue={formDataCache['risk_origin']}
/>
<TextArea
{form}
field="target_objective"
label={m.targetObjective()}
cacheLock={cacheLocks['target_objective']}
bind:cachedValue={formDataCache['target_objective']}
/>
<Select
{form}
options={model.selectOptions['motivation']}
field="motivation"
label={m.motivation()}
cacheLock={cacheLocks['motivation']}
bind:cachedValue={formDataCache['motivation']}
/>
<Select
{form}
options={model.selectOptions['resources']}
field="resources"
label={m.resources()}
cacheLock={cacheLocks['resources']}
bind:cachedValue={formDataCache['resources']}
/>
<Select
{form}
options={model.selectOptions['pertinence']}
field="pertinence"
label={m.pertinence()}
cacheLock={cacheLocks['pertinence']}
bind:cachedValue={formDataCache['pertinence']}
/>
<NumberField
{form}
field="activity"
label={m.activity()}
cacheLock={cacheLocks['activity']}
bind:cachedValue={formDataCache['activity']}
/>
<Checkbox {form} field="is_selected" label={m.isSelected()} />
<TextArea
{form}
field="justification"
label={m.justification()}
cacheLock={cacheLocks['justification']}
bind:cachedValue={formDataCache['justification']}
/>
18 changes: 18 additions & 0 deletions frontend/src/lib/utils/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@ export const URL_MODEL_MAP: ModelMap = {
{ field: 'qualifications', urlModel: 'qualifications' }
],
selectFields: [{ field: 'gravity', valueType: 'number', detail: true }]
},
'ro-to': {
endpointUrl: 'ebios-rm/ro-to',
name: 'roto',
localName: 'roto',
localNamePlural: 'roto',
verboseName: 'Ro to',
verboseNamePlural: 'Ro to',
foreignKeyFields: [
{ field: 'ebios_rm_study', urlModel: 'ebios-rm' },
{ field: 'feared_events', urlModel: 'feared-events' }
],
selectFields: [
{ field: 'risk-origin' },
{ field: 'motivation', valueType: 'number' },
{ field: 'resources', valueType: 'number' },
{ field: 'pertinence', valueType: 'number' }
]
}
};

Expand Down
16 changes: 15 additions & 1 deletion frontend/src/lib/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ export const fearedEventsSchema = z.object({
qualifications: z.string().optional().array().optional()
});

export const roToSchema = z.object({
ebios_rm_study: z.string(),
feared_events: z.string().uuid().array(),
risk_origin: z.string(),
target_objective: z.string(),
motivation: z.number().default(0).optional(),
resources: z.number().default(0).optional(),
pertinence: z.number().default(0).optional(),
activity: z.number().min(0).max(4).optional().default(0),
is_selected: z.boolean().optional().default(false),
justification: z.string().optional()
});

const SCHEMA_MAP: Record<string, AnyZodObject> = {
folders: FolderSchema,
projects: ProjectSchema,
Expand All @@ -439,7 +452,8 @@ const SCHEMA_MAP: Record<string, AnyZodObject> = {
vulnerabilities: vulnerabilitySchema,
'filtering-labels': FilteringLabelSchema,
'ebios-rm': ebiosRMSchema,
'feared-events': fearedEventsSchema
'feared-events': fearedEventsSchema,
'ro-to': roToSchema
};

export const modelSchema = (model: string) => {
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/lib/utils/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,23 @@ export const listViewFields: ListViewFieldsConfig = {
'feared-events': {
head: ['selected', 'assets', 'fearedEvent', 'qualifications', 'gravity'],
body: ['is_selected', 'assets', 'description', 'qualifications', 'gravity']
},
'ro-to': {
head: [
'isSelected',
'riskOrigin',
'targetObjective',
'motivation',
'fearedEvents',
'pertinence'
],
body: [
'is_selected',
'risk_origin',
'target_objective',
'motivation',
'feared_events',
'pertinence'
]
}
};
3 changes: 2 additions & 1 deletion frontend/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const URL_MODEL = [
'representatives',
'vulnerabilities',
'filtering-labels',
'feared-events'
'feared-events',
'ro-to'
// 'ebios-rm',
] as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const load: PageServerLoad = async ({ params, fetch }) => {

for (const keyField of foreignKeyFields) {
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const url = `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`;
const keyModel = getModelInfo(keyField.urlModel);
const url = keyModel.endpointUrl
? `${BASE_API_URL}/${keyModel.endpointUrl}/${queryParams}`
: `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`;
const response = await fetch(url);
if (response.ok) {
foreignKeys[keyField.field] = await response.json().then((data) => data.results);
Expand All @@ -42,7 +45,9 @@ export const load: PageServerLoad = async ({ params, fetch }) => {

for (const selectField of selectFields) {
if (selectField.detail) continue;
const url = `${BASE_API_URL}/${params.model}/${selectField.field}/`;
const url = model.endpointUrl
? `${BASE_API_URL}/${model.endpointUrl}/${selectField.field}/`
: `${BASE_API_URL}/${params.model}/${selectField.field}/`;
const response = await fetch(url);
if (response.ok) {
selectOptions[selectField.field] = await response.json().then((data) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ export const load: PageServerLoad = async (event) => {
if (modelInfo.foreignKeyFields) {
await Promise.all(
modelInfo.foreignKeyFields.map(async (keyField) => {
const keyModel = getModelInfo(keyField.urlModel);
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const url = `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`;
const url = keyModel.endpointUrl
? `${BASE_API_URL}/${keyModel.endpointUrl}/${queryParams}`
: `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`;
const response = await event.fetch(url);
if (response.ok) {
foreignKeys[keyField.field] = await response.json().then((data) => data.results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,21 @@
}
],
ws2: [
{ title: safeTranslate(m.ebiosWs2_1()), status: 'to_do', href: '#' },
{ title: safeTranslate(m.ebiosWs2_2()), status: 'to_do', href: '#' },
{ title: safeTranslate(m.ebiosWs2_3()), status: 'to_do', href: '#' }
{
title: safeTranslate(m.ebiosWs2_1()),
status: 'to_do',
href: `${$page.url.pathname}/workshop-two/ro-to?next=${$page.url.pathname}`
},
{
title: safeTranslate(m.ebiosWs2_2()),
status: 'to_do',
href: `${$page.url.pathname}/workshop-two/ro-to?next=${$page.url.pathname}`
},
{
title: safeTranslate(m.ebiosWs2_3()),
status: 'to_do',
href: `${$page.url.pathname}/workshop-two/ro-to?next=${$page.url.pathname}`
}
],
ws3: [
{ title: safeTranslate(m.ebiosWs3_1()), status: 'to_do', href: '#' },
Expand Down
Loading
Loading