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

Asset info #681

Merged
merged 5 commits into from
Jan 10, 2022
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
43 changes: 41 additions & 2 deletions dandiapi/api/tests/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,12 @@ def test_asset_populate_metadata_zarr(draft_asset_factory, zarr_archive):


@pytest.mark.django_db
def test_asset_rest_list(api_client, version, asset):
def test_asset_rest_list(api_client, version, asset, asset_factory):
version.assets.add(asset)

# Create an extra asset so that there are multiple assets to filter down
asset_factory()

assert api_client.get(
f'/api/dandisets/{version.dandiset.identifier}/versions/{version.version}/assets/'
).json() == {
Expand Down Expand Up @@ -344,9 +347,12 @@ def test_asset_rest_list_ordering(api_client, version, asset_factory, order_para


@pytest.mark.django_db
def test_asset_rest_retrieve(api_client, version, asset):
def test_asset_rest_retrieve(api_client, version, asset, asset_factory):
version.assets.add(asset)

# Create an extra asset so that there are multiple assets to filter down
asset_factory()

assert (
api_client.get(
f'/api/dandisets/{version.dandiset.identifier}/'
Expand All @@ -372,6 +378,25 @@ def test_asset_rest_retrieve_no_sha256(api_client, version, asset):
)


@pytest.mark.django_db
def test_asset_rest_info(api_client, version, asset):
version.assets.add(asset)
dchiquito marked this conversation as resolved.
Show resolved Hide resolved

assert api_client.get(
f'/api/dandisets/{version.dandiset.identifier}/'
f'versions/{version.version}/assets/{asset.asset_id}/info/'
).json() == {
'asset_id': str(asset.asset_id),
'blob': str(asset.blob.blob_id),
'zarr': None,
'path': asset.path,
'size': asset.size,
'metadata': asset.metadata,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
}


@pytest.mark.django_db
@pytest.mark.parametrize(
'status,validation_error',
Expand Down Expand Up @@ -971,6 +996,20 @@ def test_asset_direct_metadata(api_client, asset):
assert json.loads(api_client.get(f'/api/assets/{asset.asset_id}/').content) == asset.metadata


@pytest.mark.django_db
def test_asset_direct_info(api_client, asset):
assert api_client.get(f'/api/assets/{asset.asset_id}/info/').json() == {
'asset_id': str(asset.asset_id),
'blob': str(asset.blob.blob_id),
'zarr': None,
'path': asset.path,
'size': asset.size,
'metadata': asset.metadata,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
}


@pytest.mark.parametrize(
('embargo_status'),
[
Expand Down
10 changes: 8 additions & 2 deletions dandiapi/api/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ def test_version_publish_version(draft_version, asset):


@pytest.mark.django_db
def test_version_rest_list(api_client, version):
def test_version_rest_list(api_client, version, draft_version_factory):
# Create an extra version so that there are multiple versions to filter down
draft_version_factory()

assert api_client.get(f'/api/dandisets/{version.dandiset.identifier}/versions/').data == {
'count': 1,
'next': None,
Expand All @@ -345,7 +348,10 @@ def test_version_rest_list(api_client, version):


@pytest.mark.django_db
def test_version_rest_retrieve(api_client, version):
def test_version_rest_retrieve(api_client, version, draft_version_factory):
# Create an extra version so that there are multiple versions to filter down
draft_version_factory()

assert (
api_client.get(
f'/api/dandisets/{version.dandiset.identifier}/versions/{version.version}/'
Expand Down
3 changes: 2 additions & 1 deletion dandiapi/api/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .asset import AssetViewSet, asset_download_view, asset_metadata_view
from .asset import AssetViewSet, asset_download_view, asset_info_view, asset_metadata_view
from .auth import auth_token_view, authorize_view, user_questionnaire_form_view
from .dandiset import DandisetViewSet
from .info import info_view
Expand All @@ -21,6 +21,7 @@
'ZarrViewSet',
'authorize_view',
'asset_download_view',
'asset_info_view',
'asset_metadata_view',
'auth_token_view',
'blob_read_view',
Expand Down
23 changes: 23 additions & 0 deletions dandiapi/api/views/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ def asset_metadata_view(request, asset_id):
return JsonResponse(asset.metadata)


@swagger_auto_schema(
method='GET',
operation_summary='Django serialization of an asset',
manual_parameters=[ASSET_ID_PARAM],
)
@api_view(['GET', 'HEAD'])
def asset_info_view(request, asset_id):
asset = get_object_or_404(Asset, asset_id=asset_id)
serializer = AssetDetailSerializer(instance=asset)
return Response(serializer.data, status=status.HTTP_200_OK)


class AssetRequestSerializer(serializers.Serializer):
metadata = serializers.JSONField()
blob_id = serializers.UUIDField(required=False)
Expand Down Expand Up @@ -201,6 +213,17 @@ def retrieve(self, request, **kwargs):
asset = self.get_object()
return Response(asset.metadata, status=status.HTTP_200_OK)

@swagger_auto_schema(
manual_parameters=[ASSET_ID_PARAM, VERSIONS_DANDISET_PK_PARAM, VERSIONS_VERSION_PARAM],
responses={200: AssetDetailSerializer()},
)
@action(detail=True, methods=['GET'])
def info(self, request, **kwargs):
"""Django serialization of an asset."""
asset = self.get_object()
serializer = AssetDetailSerializer(instance=asset)
return Response(serializer.data, status=status.HTTP_200_OK)

@swagger_auto_schema(
responses={200: AssetValidationSerializer()},
manual_parameters=[ASSET_ID_PARAM, VERSIONS_DANDISET_PK_PARAM, VERSIONS_VERSION_PARAM],
Expand Down
5 changes: 0 additions & 5 deletions dandiapi/api/views/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ def retrieve(self, request, **kwargs):
version = self.get_object()
return Response(version.metadata, status=status.HTTP_200_OK)

# TODO clean up this action
# Originally retrieve() returned this, but the API specification was modified so that
# retrieve() only returns the metadata for a version, instead of a serialization.
# Unfortunately the web UI is built around VersionDetailSerializer, so this endpoint was
# added to avoid rewriting the web UI.
@swagger_auto_schema(
manual_parameters=[DANDISET_PK_PARAM, VERSION_PARAM],
responses={200: VersionDetailSerializer()},
Expand Down
6 changes: 6 additions & 0 deletions dandiapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
VersionViewSet,
ZarrViewSet,
asset_download_view,
asset_info_view,
asset_metadata_view,
auth_token_view,
authorize_view,
Expand Down Expand Up @@ -87,6 +88,11 @@ def to_url(self, value):
asset_download_view,
name='asset-direct-download',
),
re_path(
r'api/assets/(?P<asset_id>[0-9a-f\-]{36})/info/',
asset_info_view,
name='asset-direct-info',
),
path('api/auth/token/', auth_token_view, name='auth-token'),
path('api/stats/', stats_view),
path('api/info/', info_view),
Expand Down