Skip to content

Commit

Permalink
Merge pull request #1366 from dandi/save-version-validate-asset-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt authored Nov 15, 2022
2 parents a140937 + 0276628 commit 8dbfda7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dandiapi/api/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,28 @@ def validate_asset_metadata(asset_id: int) -> None:
try:
metadata = asset.published_metadata()
validate(metadata, schema_key='PublishedAsset', json_validation=True)
logger.info('Successfully validated asset %s', asset_id)
asset.status = Asset.Status.VALID
asset.validation_errors = []
except dandischema.exceptions.ValidationError as e:
logger.info('Error while validating asset %s', asset_id)
asset.status = Asset.Status.INVALID

validation_errors = collect_validation_errors(e)
asset.validation_errors = validation_errors
asset.save()
return
except ValueError as e:
# A bare ValueError is thrown when dandischema generates its own exceptions, like a
# mismatched schemaVersion.
asset.status = Asset.Status.INVALID
asset.validation_errors = [{'field': '', 'message': str(e)}]
asset.save()
return
logger.info('Successfully validated asset %s', asset_id)
asset.status = Asset.Status.VALID
asset.validation_errors = []

# Save asset
asset.save()

# Save any associated draft versions
for version in asset.versions.filter(version='draft'):
version.save()


@shared_task(soft_time_limit=30)
@atomic
Expand Down
17 changes: 17 additions & 0 deletions dandiapi/api/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import hashlib

from django.conf import settings
Expand Down Expand Up @@ -181,6 +182,22 @@ def test_validate_asset_metadata_malformed_keywords(asset: Asset):
]


@pytest.mark.django_db
def test_validate_asset_metadata_saves_version(draft_asset: Asset, draft_version: Version):
draft_version.assets.add(draft_asset)

# Update version with queryset so modified isn't auto incremented
old_datetime = datetime.datetime.fromtimestamp(1573782390).astimezone(datetime.timezone.utc)
Version.objects.filter(id=draft_version.id).update(modified=old_datetime)

# Run task
tasks.validate_asset_metadata(draft_asset.id)

# Test that version has new modified timestamp
draft_version.refresh_from_db()
assert draft_version.modified != old_datetime


@pytest.mark.django_db
def test_validate_version_metadata(version: Version, asset: Asset):
version.assets.add(asset)
Expand Down

0 comments on commit 8dbfda7

Please sign in to comment.