Skip to content

Commit

Permalink
simpify backend code (recursion)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-intuitem committed Apr 9, 2024
1 parent 6cc4e5e commit aee47d8
Showing 1 changed file with 1 addition and 45 deletions.
46 changes: 1 addition & 45 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_sorted_requirement_nodes_rec(
start: the initial list
"""
result = {}
for node in start:
for node in sorted(start, key=lambda x: x.order_id):
children = [
requirement_node
for requirement_node in requirement_nodes
Expand All @@ -247,50 +247,6 @@ def get_sorted_requirement_nodes_rec(
requirement_nodes, requirements_assessed, children
),
}
for req in sorted(
[
requirement_node
for requirement_node in requirement_nodes
if requirement_node.parent_urn == node.urn
],
key=lambda x: x.order_id,
):
if requirements_assessed:
req_as = requirement_assessment_from_requirement_id[str(req.id)]
result[str(node.id)]["children"][str(req.id)].update(
{
"urn": req.urn,
"ref_id": req.ref_id,
"name": req.name,
"description": req.description,
"ra_id": str(req_as.id),
"status": req_as.status,
"status_display": req_as.get_status_display(),
"status_i18n": camel_case(req_as.status),
"threats": ThreatReadSerializer(
req.threats.all(), many=True
).data,
"reference_controls": ReferenceControlReadSerializer(
req.reference_controls.all(), many=True
).data,
}
)
else:
result[str(node.id)]["children"][str(req.id)].update(
{
"urn": req.urn,
"ref_id": req.ref_id,
"name": req.name,
"description": req.description,
"style": "leaf",
"threats": ThreatReadSerializer(
req.threats.all(), many=True
).data,
"reference_controls": ReferenceControlReadSerializer(
req.reference_controls.all(), many=True
).data,
}
)
return result

tree = get_sorted_requirement_nodes_rec(
Expand Down

0 comments on commit aee47d8

Please sign in to comment.