Skip to content

Commit 89633c5

Browse files
author
Alan Christie
committed
fix: Improved target experiment file handling
1 parent d389549 commit 89633c5

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

viewer/target_loader.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -1737,11 +1737,17 @@ def load_target(
17371737
archive.extractall(target_loader.raw_data)
17381738
msg = f"Data extraction complete: {data_bundle}"
17391739
logger.info("%s%s", target_loader.report.task_id, msg)
1740-
except FileNotFoundError as exc:
1741-
msg = f"{data_bundle} file does not exist"
1742-
logger.exception("%s%s", target_loader.report.task_id, msg)
1743-
target_loader.experiment_upload.message = exc.args[0]
1744-
raise FileNotFoundError(msg) from exc
1740+
except Exception as exc:
1741+
# Handle _any_ underlying problem with the file.
1742+
logger.error('Got an exception opening the file: %s', str(exc))
1743+
target_loader.report.log(
1744+
Level.FATAL,
1745+
f"Decompression of '{bundle_filename}' has failed. Is it a Target Experiment file?",
1746+
)
1747+
target_loader.report.final(
1748+
f"Failed to decompress '{target_loader.data_bundle}'", success=False
1749+
)
1750+
return
17451751

17461752
target_loader.report.log(Level.INFO, f"Decompressed '{bundle_filename}'")
17471753

@@ -1760,23 +1766,23 @@ def load_target(
17601766
# Any problem with the underlying data is transmitted in the report.
17611767
logger.debug(exc, exc_info=True)
17621768
target_loader.report.final(
1763-
f"Uploading {target_loader.data_bundle} failed", success=False
1769+
f"Failed to process '{target_loader.data_bundle}'", success=False
17641770
)
17651771
return
1766-
17671772
else:
1768-
# Move the uploaded file to its final location
1769-
target_loader.abs_final_path.mkdir(parents=True)
1770-
target_loader.raw_data.rename(target_loader.abs_final_path)
1771-
Path(target_loader.bundle_path).rename(
1772-
target_loader.abs_final_path.parent.joinpath(target_loader.data_bundle)
1773-
)
1773+
_move_and_save_target(target_loader)
17741774

1775-
set_directory_permissions(target_loader.abs_final_path, 0o755)
17761775

1777-
target_loader.report.final(
1778-
f"{target_loader.data_bundle} uploaded successfully"
1779-
)
1780-
target_loader.experiment_upload.message = target_loader.report.json()
1776+
def _move_and_save_target(target_loader):
1777+
# Move the uploaded file to its final location
1778+
target_loader.abs_final_path.mkdir(parents=True)
1779+
target_loader.raw_data.rename(target_loader.abs_final_path)
1780+
Path(target_loader.bundle_path).rename(
1781+
target_loader.abs_final_path.parent.joinpath(target_loader.data_bundle)
1782+
)
1783+
1784+
set_directory_permissions(target_loader.abs_final_path, 0o755)
17811785

1782-
target_loader.experiment_upload.save()
1786+
target_loader.report.final(f"{target_loader.data_bundle} uploaded successfully")
1787+
target_loader.experiment_upload.message = target_loader.report.json()
1788+
target_loader.experiment_upload.save()

viewer/views.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1552,11 +1552,13 @@ def create(self, request, *args, **kwargs):
15521552
if not user.is_authenticated:
15531553
return redirect(settings.LOGIN_URL)
15541554
else:
1555-
if target_access_string not in self.get_proposals_for_user(user):
1555+
if target_access_string not in self.get_proposals_for_user(
1556+
user, restrict_to_membership=True
1557+
):
15561558
return Response(
15571559
{
15581560
"target_access_string": [
1559-
f"You are not authorized to upload data to {target_access_string}"
1561+
f"You are not authorized to upload data to '{target_access_string}'"
15601562
]
15611563
},
15621564
status=status.HTTP_403_FORBIDDEN,

0 commit comments

Comments
 (0)