diff --git a/dandiapi/api/models/asset.py b/dandiapi/api/models/asset.py index 1b6e0b743..99b030299 100644 --- a/dandiapi/api/models/asset.py +++ b/dandiapi/api/models/asset.py @@ -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 @@ -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 diff --git a/dandiapi/api/services/asset/__init__.py b/dandiapi/api/services/asset/__init__.py index 219579e00..a87fd4fc5 100644 --- a/dandiapi/api/services/asset/__init__.py +++ b/dandiapi/api/services/asset/__init__.py @@ -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 diff --git a/dandiapi/api/tests/test_asset.py b/dandiapi/api/tests/test_asset.py index c0f250cd8..d048e7329 100644 --- a/dandiapi/api/tests/test_asset.py +++ b/dandiapi/api/tests/test_asset.py @@ -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)