Skip to content

Commit

Permalink
Align backend serialized criticality display with frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
nas-tabchiche committed Dec 16, 2024
1 parent b47f41d commit 8047aba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 15 additions & 0 deletions backend/ebios_rm/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -362,6 +363,20 @@ def residual_criticality(self):
self.residual_trust,
)

def get_current_criticality_display(self) -> str:
return (
f"{self.current_criticality:.2f}".rstrip("0").rstrip(".")
if "." in f"{self.current_criticality:.2f}"
else f"{self.current_criticality:.2f}"
)

def get_residual_criticality_display(self) -> str:
return (
f"{self.residual_criticality:.2f}".rstrip("0").rstrip(".")
if "." in f"{self.residual_criticality:.2f}"
else f"{self.residual_criticality:.2f}"
)


class AttackPath(NameDescriptionMixin, FolderMixin):
ebios_rm_study = models.ForeignKey(
Expand Down
16 changes: 10 additions & 6 deletions backend/ebios_rm/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ class Meta:


class StakeholderWriteSerializer(BaseModelSerializer):
current_criticality = serializers.DecimalField(
max_digits=5, decimal_places=2, read_only=True
current_criticality = serializers.CharField(
source="get_current_criticality_display"
)
residual_criticality = serializers.DecimalField(
max_digits=5, decimal_places=2, read_only=True
residual_criticality = serializers.CharField(
source="get_residual_criticality_display"
)

class Meta:
Expand All @@ -124,8 +124,12 @@ class StakeholderReadSerializer(BaseModelSerializer):
applied_controls = FieldsRelatedField(many=True)

category = serializers.CharField(source="get_category_display")
current_criticality = serializers.DecimalField(max_digits=5, decimal_places=2)
residual_criticality = serializers.DecimalField(max_digits=5, decimal_places=2)
current_criticality = serializers.CharField(
source="get_current_criticality_display"
)
residual_criticality = serializers.CharField(
source="get_residual_criticality_display"
)

class Meta:
model = Stakeholder
Expand Down

0 comments on commit 8047aba

Please sign in to comment.