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

Bugfix when deleting & exporting DFIQ objects #1126

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
15 changes: 8 additions & 7 deletions core/web/apiv2/dfiq.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,24 @@ async def to_archive(request: DFIQSearchRequest) -> FileResponse:
os.makedirs(f"{tempdir.name}/{dir_name}")

for obj in public_objs:
with open(f"{tempdir.name}/public/{obj.dfiq_id}.yaml", "w") as f:
with open(f"{tempdir.name}/public/{obj.uuid}.yaml", "w") as f:
f.write(obj.to_yaml())

for obj in internal_objs:
with open(f"{tempdir.name}/internal/{obj.dfiq_id}.yaml", "w") as f:
with open(f"{tempdir.name}/internal/{obj.uuid}.yaml", "w") as f:
f.write(obj.to_yaml())

with tempfile.NamedTemporaryFile(delete=False) as archive:
with ZipFile(archive, "w") as zipf:
for obj in public_objs:
zipf.write(
f"{tempdir.name}/public/{obj.dfiq_id}.yaml",
f"public/{_TYPE_TO_DUMP_DIR[obj.type]}/{obj.dfiq_id}.yaml",
f"{tempdir.name}/public/{obj.uuid}.yaml",
f"public/{_TYPE_TO_DUMP_DIR[obj.type]}/{obj.uuid}.yaml",
)
for obj in internal_objs:
zipf.write(
f"{tempdir.name}/internal/{obj.dfiq_id}.yaml",
f"internal/{_TYPE_TO_DUMP_DIR[obj.type]}/{obj.dfiq_id}.yaml",
f"{tempdir.name}/internal/{obj.uuid}.yaml",
f"internal/{_TYPE_TO_DUMP_DIR[obj.type]}/{obj.uuid}.yaml",
)

return FileResponse(archive.name, media_type="application/zip", filename="dfiq.zip")
Expand Down Expand Up @@ -278,7 +278,8 @@ async def delete(dfiq_id: str) -> None:
all_children, _ = dfiq.DFIQBase.filter(query_args={"parent_ids": db_dfiq.uuid})
if db_dfiq.dfiq_id:
children, _ = dfiq.DFIQBase.filter(query_args={"parent_ids": db_dfiq.dfiq_id})
all_children.extend(children)
if children:
all_children.extend(children)
for child in all_children:
if db_dfiq.dfiq_id in child.parent_ids:
child.parent_ids.remove(db_dfiq.dfiq_id)
Expand Down
18 changes: 10 additions & 8 deletions tests/apiv2/dfiq.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,23 +481,25 @@ def test_to_archive(self):
with ZipFile(io.BytesIO(response.content)) as archive:
files = archive.namelist()
self.assertEqual(len(files), 4)
self.assertIn("public/scenarios/S1003.yaml", files)
self.assertIn("internal/scenarios/S0003.yaml", files)
self.assertIn("public/questions/Q1020.yaml", files)
self.assertIn("internal/questions/Q1020.yaml", files)
self.assertIn("public/scenarios/test_scenario_uuid.yaml", files)
self.assertIn("internal/scenarios/test_private_scenario_uuid.yaml", files)
self.assertIn("public/questions/test_question_uuid.yaml", files)
self.assertIn("internal/questions/test_question_uuid.yaml", files)

with archive.open("public/scenarios/S1003.yaml") as f:
with archive.open("public/scenarios/test_scenario_uuid.yaml") as f:
content = f.read().decode("utf-8")
self.assertIn("public_scenario", content)
with archive.open("internal/scenarios/S0003.yaml") as f:
with archive.open(
"internal/scenarios/test_private_scenario_uuid.yaml"
) as f:
content = f.read().decode("utf-8")
self.assertIn("private_scenario", content)
with archive.open("public/questions/Q1020.yaml") as f:
with archive.open("public/questions/test_question_uuid.yaml") as f:
content = f.read().decode("utf-8")
self.assertIn("semi_private_question", content)
self.assertIn("public_approach", content)
self.assertNotIn("internal_approach", content)
with archive.open("internal/questions/Q1020.yaml") as f:
with archive.open("internal/questions/test_question_uuid.yaml") as f:
content = f.read().decode("utf-8")
self.assertIn("semi_private_question", content)
self.assertIn("public_approach", content)
Expand Down
Loading