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

Sort security objectives and disaster recovery objectives based on default objectives #1306

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ class StoredLibrary(LibraryMixin):

REQUIRED_FIELDS = {"urn", "name", "version", "objects"}
FIELDS_VERIFIERS = {}
HASH_CHECKSUM_SET = set() # For now a library isn't updated if its SHA256 checksum has already been registered in the database.
# For now a library isn't updated if its SHA256 checksum has already been registered in the database.
HASH_CHECKSUM_SET = set()

@classmethod
def __init_class__(cls):
Expand All @@ -259,7 +260,8 @@ def store_library_content(
) -> "StoredLibrary | None":
hash_checksum = sha256(library_content)
if hash_checksum in StoredLibrary.HASH_CHECKSUM_SET:
return None # We do not store the library if its hash checksum is in the database.
# We do not store the library if its hash checksum is in the database.
return None
try:
library_data = yaml.safe_load(library_content)
if not isinstance(library_data, dict):
Expand Down Expand Up @@ -322,7 +324,8 @@ def store_library_content(
objects_meta=objects_meta,
dependencies=dependencies,
is_loaded=is_loaded,
builtin=builtin, # We have to add a "builtin: true" line to every builtin library file.
# We have to add a "builtin: true" line to every builtin library file.
builtin=builtin,
hash_checksum=hash_checksum,
content=library_objects,
)
Expand Down Expand Up @@ -439,7 +442,8 @@ def update_library(self) -> Union[str, None]:
"packager",
self.new_library.packager,
), # A user can fake a builtin library in this case because he can update a builtin library by adding its own library with the same URN as a builtin library.
("ref_id", self.new_library.ref_id), # Should we even update the ref_id ?
# Should we even update the ref_id ?
("ref_id", self.new_library.ref_id),
("description", self.new_library.description),
("annotation", self.new_library.annotation),
("translations", self.new_library.translations),
Expand Down Expand Up @@ -932,7 +936,8 @@ def render_grid_as_colors(self):

@property
def get_json_translated(self):
return update_translations_as_string(self.json_definition, "fr") # Why "fr" ?
# Why "fr" ?
return update_translations_as_string(self.json_definition, "fr")

def __str__(self) -> str:
return self.get_name_translated
Expand Down Expand Up @@ -1629,7 +1634,10 @@ def get_security_objectives_display(self) -> list[dict[str, str]]:
{
"str": f"{key}: {self.SECURITY_OBJECTIVES_SCALES[scale][content.get('value', 0)]}",
}
for key, content in security_objectives.get("objectives", {}).items()
for key, content in sorted(
security_objectives.get("objectives", {}).items(),
key=lambda x: self.DEFAULT_SECURITY_OBJECTIVES.index(x[0]),
)
if content.get("is_enabled", False)
and content.get("value", -1) in range(0, 5)
]
Expand Down Expand Up @@ -1657,9 +1665,10 @@ def format_seconds(seconds: int) -> str:
disaster_recovery_objectives = self.get_disaster_recovery_objectives()
return [
{"str": f"{key}: {format_seconds(content.get('value', 0))}"}
for key, content in disaster_recovery_objectives.get(
"objectives", {}
).items()
for key, content in sorted(
disaster_recovery_objectives.get("objectives", {}).items(),
key=lambda x: self.DEFAULT_DISASTER_RECOVERY_OBJECTIVES.index(x[0]),
)
if content.get("value", 0)
]

Expand Down Expand Up @@ -1979,7 +1988,7 @@ class Status(models.TextChoices):
fields_to_check = ["name"]


## historical data
# historical data
class HistoricalMetric(models.Model):
date = models.DateField(verbose_name=_("Date"), db_index=True)
data = models.JSONField(verbose_name=_("Historical Data"))
Expand Down
Loading