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

Specify asset path at top level in API #1700

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 6 additions & 2 deletions dandiapi/api/management/commands/create_dev_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def create_dev_dandiset(name: str, email: str):
'schemaVersion': settings.DANDI_SCHEMA_VERSION,
'encodingFormat': 'text/plain',
'schemaKey': 'Asset',
'path': 'foo/bar.txt',
}
asset_path = 'foo/bar.txt'
asset = add_asset_to_version(
user=owner, version=draft_version, asset_blob=asset_blob, metadata=asset_metadata
user=owner,
version=draft_version,
asset_blob=asset_blob,
metadata=asset_metadata,
path=asset_path,
)

calculate_sha256(blob_id=asset_blob.blob_id)
Expand Down
11 changes: 5 additions & 6 deletions dandiapi/api/services/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def change_asset(
version: Version,
new_asset_blob: AssetBlob | EmbargoedAssetBlob | None = None,
new_zarr_archive: ZarrArchive | None = None,
new_path: str,
new_metadata: dict,
) -> tuple[Asset, bool]:
"""
Expand All @@ -55,26 +56,24 @@ def change_asset(
assert (
new_asset_blob or new_zarr_archive
), 'One of new_zarr_archive or new_asset_blob must be given to change_asset_metadata'
assert 'path' in new_metadata, 'Path must be present in new_metadata'

if not user.has_perm('owner', version.dandiset):
raise DandisetOwnerRequired()
elif version.version != 'draft':
raise DraftDandisetNotModifiable()

path = new_metadata['path']
new_metadata_stripped = Asset.strip_metadata(new_metadata)

if not asset.is_different_from(
asset_blob=new_asset_blob,
zarr_archive=new_zarr_archive,
metadata=new_metadata_stripped,
path=path,
path=new_path,
):
return asset, False

# Verify we aren't changing path to the same value as an existing asset
if version.assets.filter(path=path).exclude(asset_id=asset.asset_id).exists():
if version.assets.filter(path=new_path).exclude(asset_id=asset.asset_id).exists():
raise AssetAlreadyExists()

with transaction.atomic():
Expand All @@ -85,6 +84,7 @@ def change_asset(
version=version,
asset_blob=new_asset_blob,
zarr_archive=new_zarr_archive,
path=new_path,
metadata=new_metadata,
)
# Set previous asset and save
Expand All @@ -100,21 +100,20 @@ def add_asset_to_version(
version: Version,
asset_blob: AssetBlob | EmbargoedAssetBlob | None = None,
zarr_archive: ZarrArchive | None = None,
path: str,
metadata: dict,
) -> Asset:
"""Create an asset, adding it to a version."""
assert (
asset_blob or zarr_archive
), 'One of zarr_archive or asset_blob must be given to add_asset_to_version'
assert 'path' in metadata, 'Path must be present in metadata'

if not user.has_perm('owner', version.dandiset):
raise DandisetOwnerRequired()
elif version.version != 'draft':
raise DraftDandisetNotModifiable()

# Check if there are already any assets with the same path
path = metadata['path']
if version.assets.filter(path=path).exists():
raise AssetAlreadyExists()

Expand Down
Loading
Loading