Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 22 additions & 4 deletions backend/data_wizard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2572,8 +2572,16 @@ def _process_compliance_assessment(
results = {"successful": 0, "failed": 0, "errors": []}
try:
# Get the perimeter object to extract its folder ID
perimeter = Perimeter.objects.get(id=perimeter_id)
folder_id = perimeter.folder.id
perimeter = None
if perimeter_id is not None:
perimeter = Perimeter.objects.get(id=perimeter_id)

if perimeter is not None:
folder_id = perimeter.folder.id
elif folder_id is None:
raise AssertionError(
"A folder must be specified when there's no perimeter!"
)
Comment on lines 2572 to +2584
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Return a normal import result here instead of raising.

This branch is newly reachable now that perimeter is optional. Raising here falls into the broad except below, which returns a DRF Response from a helper that otherwise returns a dict; process_excel_file() then wraps that value in another response. Please fail with the same result shape as the other import helpers.

💡 Minimal fix
-            elif folder_id is None:
-                raise AssertionError(
-                    "A folder must be specified when there's no perimeter!"
-                )
+            elif folder_id is None:
+                results["failed"] = len(records)
+                results["errors"].append(
+                    {"error": "A folder must be specified when there's no perimeter."}
+                )
+                return results
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/data_wizard/views.py` around lines 2529 - 2541, The code currently
raises an AssertionError when neither perimeter nor folder_id is provided;
instead, return a normal import result dict (the same shape as the existing
results variable) so callers like process_excel_file() receive a dict not an
exception-wrapped Response. In the branch where perimeter is None and folder_id
is None, populate results["failed"] (e.g., set to 1) and append an explanatory
message to results["errors"] (e.g., "A folder must be specified when there's no
perimeter"), then return results immediately; update the block around
Perimeter.objects.get, perimeter, folder_id to perform this return rather than
raising. Ensure the returned shape matches the existing results dict.


timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
assessment_name = f"Assessment_{timestamp}"
Expand Down Expand Up @@ -3654,8 +3662,18 @@ def _process_risk_assessment(

try:
# Get the perimeter and its domain
perimeter = Perimeter.objects.get(id=perimeter_id)
domain = perimeter.folder
perimeter = None
if perimeter_id is not None:
perimeter = Perimeter.objects.get(id=perimeter_id)

if perimeter is not None:
domain = perimeter.folder
else:
if folder_id is None:
raise AssertionError(
"A folder must be specified when there's no perimeter!"
)
domain = Folder.objects.get(id=folder_id)

# Get the risk matrix
risk_matrix = RiskMatrix.objects.get(id=matrix_id)
Expand Down
4 changes: 2 additions & 2 deletions cli/clica.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def upload_data_wizard_file(
"model_type": "ComplianceAssessment",
"help": "Import compliance assessments using the Data Wizard backend.",
"requires_folder": False,
"requires_perimeter": True,
"requires_perimeter": False,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost, now we're in a situation where domain (folder) and perimeter are optional

"requires_framework": True,
"requires_matrix": False,
},
Expand All @@ -343,7 +343,7 @@ def upload_data_wizard_file(
"model_type": "RiskAssessment",
"help": "Import risk assessments using the Data Wizard backend.",
"requires_folder": False,
"requires_perimeter": True,
"requires_perimeter": False,
"requires_framework": False,
"requires_matrix": True,
},
Expand Down
Loading