Skip to content

Commit

Permalink
Merge pull request #1689 from dandi/fix-put-path
Browse files Browse the repository at this point in the history
Enable path-only changes to metadata to trigger asset change
  • Loading branch information
waxlamp authored Oct 4, 2023
2 parents d59d889 + ffef046 commit 8983b91
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def is_different_from(
asset_blob: AssetBlob | EmbargoedAssetBlob | None = None,
zarr_archive: ZarrArchive | EmbargoedZarrArchive | None = None,
metadata: dict,
path: str,
) -> bool:
from dandiapi.zarr.models import EmbargoedZarrArchive, ZarrArchive

Expand All @@ -263,6 +264,9 @@ def is_different_from(
if isinstance(zarr_archive, EmbargoedZarrArchive):
raise NotImplementedError

if self.path != path:
return True

if self.metadata != metadata:
return True

Expand Down
5 changes: 4 additions & 1 deletion dandiapi/api/services/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def change_asset(
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
asset_blob=new_asset_blob,
zarr_archive=new_zarr_archive,
metadata=new_metadata_stripped,
path=path,
):
return asset, False

Expand Down
26 changes: 26 additions & 0 deletions dandiapi/api/tests/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,32 @@ def test_asset_create_existing_path(api_client, user, draft_version, asset_blob,
assert resp.status_code == 409


@pytest.mark.django_db
def test_asset_rest_rename(api_client, user, draft_version, asset_blob):
assign_perm('owner', user, draft_version.dandiset)
api_client.force_authenticate(user=user)

# Create asset
metadata = {'path': 'foo/bar', 'schemaVersion': settings.DANDI_SCHEMA_VERSION}
asset = add_asset_to_version(
user=user, version=draft_version, asset_blob=asset_blob, metadata=metadata
)

# Change path and make update request
metadata['path'] = 'foo/bar2'
resp = api_client.put(
f'/api/dandisets/{draft_version.dandiset.identifier}/versions/{draft_version.version}/'
f'assets/{asset.asset_id}/',
{'metadata': metadata, 'blob_id': asset.blob.blob_id},
format='json',
)

# Ensure new asset with new path was created
assert resp.status_code == 200
assert resp.json()['path'] == metadata['path']
assert resp.json()['asset_id'] != str(asset.asset_id)


@pytest.mark.django_db
def test_asset_rest_update(api_client, user, draft_version, asset, asset_blob):
assign_perm('owner', user, draft_version.dandiset)
Expand Down

0 comments on commit 8983b91

Please sign in to comment.