-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create CRUD endpoints for EBIOS RM studies and feared events (#1120)
- Loading branch information
Showing
8 changed files
with
219 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from core.serializers import ( | ||
BaseModelSerializer, | ||
FieldsRelatedField, | ||
AssessmentReadSerializer, | ||
) | ||
from core.models import StoredLibrary, RiskMatrix | ||
from .models import EbiosRMStudy, FearedEvent | ||
from rest_framework import serializers | ||
import logging | ||
|
||
|
||
class EbiosRMStudyWriteSerializer(BaseModelSerializer): | ||
risk_matrix = serializers.PrimaryKeyRelatedField( | ||
queryset=RiskMatrix.objects.all(), required=False | ||
) | ||
|
||
def create(self, validated_data): | ||
if not validated_data.get("risk_matrix"): | ||
try: | ||
ebios_matrix = RiskMatrix.objects.filter( | ||
urn="urn:intuitem:risk:matrix:risk-matrix-4x4-ebios-rm" | ||
).first() | ||
if not ebios_matrix: | ||
ebios_matrix_library = StoredLibrary.objects.get( | ||
urn="urn:intuitem:risk:library:risk-matrix-4x4-ebios-rm" | ||
) | ||
ebios_matrix_library.load() | ||
ebios_matrix = RiskMatrix.objects.get( | ||
urn="urn:intuitem:risk:matrix:risk-matrix-4x4-ebios-rm" | ||
) | ||
validated_data["risk_matrix"] = ebios_matrix | ||
except (StoredLibrary.DoesNotExist, RiskMatrix.DoesNotExist) as e: | ||
logging.error(f"Error loading risk matrix: {str(e)}") | ||
raise serializers.ValidationError( | ||
"An error occurred while loading the risk matrix." | ||
) | ||
return super().create(validated_data) | ||
|
||
class Meta: | ||
model = EbiosRMStudy | ||
exclude = ["created_at", "updated_at"] | ||
|
||
|
||
class EbiosRMStudyReadSerializer(BaseModelSerializer): | ||
str = serializers.CharField(source="__str__") | ||
project = FieldsRelatedField(["id", "folder"]) | ||
folder = FieldsRelatedField() | ||
risk_matrix = FieldsRelatedField() | ||
reference_entity = FieldsRelatedField() | ||
assets = FieldsRelatedField(many=True) | ||
compliance_assessments = FieldsRelatedField(many=True) | ||
risk_assessments = FieldsRelatedField(many=True) | ||
authors = FieldsRelatedField(many=True) | ||
reviewers = FieldsRelatedField(many=True) | ||
|
||
class Meta: | ||
model = EbiosRMStudy | ||
fields = "__all__" | ||
|
||
|
||
class FearedEventWriteSerializer(BaseModelSerializer): | ||
class Meta: | ||
model = FearedEvent | ||
exclude = ["created_at", "updated_at", "folder"] | ||
|
||
|
||
class FearedEventReadSerializer(BaseModelSerializer): | ||
str = serializers.CharField(source="__str__") | ||
ebios_rm_study = FieldsRelatedField() | ||
folder = FieldsRelatedField() | ||
|
||
class Meta: | ||
model = FearedEvent | ||
fields = "__all__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.urls import include, path | ||
from rest_framework import routers | ||
|
||
from ebios_rm.views import EbiosRMStudyViewSet, FearedEventViewSet | ||
|
||
router = routers.DefaultRouter() | ||
|
||
router.register(r"studies", EbiosRMStudyViewSet, basename="studies") | ||
router.register(r"feared-events", FearedEventViewSet, basename="feared-events") | ||
|
||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
from django.shortcuts import render | ||
from core.views import BaseModelViewSet as AbstractBaseModelViewSet | ||
from .models import EbiosRMStudy, FearedEvent | ||
from django.utils.decorators import method_decorator | ||
from django.views.decorators.cache import cache_page | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
|
||
# Create your views here. | ||
LONG_CACHE_TTL = 60 # mn | ||
|
||
|
||
class BaseModelViewSet(AbstractBaseModelViewSet): | ||
serializers_module = "ebios_rm.serializers" | ||
|
||
|
||
class EbiosRMStudyViewSet(BaseModelViewSet): | ||
""" | ||
API endpoint that allows ebios rm studies to be viewed or edited. | ||
""" | ||
|
||
model = EbiosRMStudy | ||
|
||
@method_decorator(cache_page(60 * LONG_CACHE_TTL)) | ||
@action(detail=False, name="Get status choices") | ||
def status(self, request): | ||
return Response(dict(EbiosRMStudy.Status.choices)) | ||
|
||
|
||
class FearedEventViewSet(BaseModelViewSet): | ||
model = FearedEvent |