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

Fix publish #460

Merged
merged 5 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 13 additions & 26 deletions dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,41 +148,28 @@ def _populate_metadata(self):
)
return metadata

@classmethod
def published_asset(cls, asset: Asset):
"""
Generate a published asset + metadata without saving it.

This is useful to validate asset metadata without saving it.
"""
def published_metadata(self):
"""Generate the metadata of this asset as if it were being published."""
now = datetime.datetime.utcnow()
# Inject the publishedBy and datePublished fields
published_metadata, _ = AssetMetadata.objects.get_or_create(
metadata={
**asset.metadata.metadata,
'publishedBy': asset.metadata.published_by(now),
**self.metadata.metadata,
'publishedBy': self.metadata.published_by(now),
'datePublished': now.isoformat(),
},
)
return published_metadata

# Create the published model
published_asset = Asset(
path=asset.path,
blob=asset.blob,
metadata=published_metadata,
# If we're publishing, just assume that the asset was valid
status=Asset.Status.VALID,
previous=asset,
published=True,
)

# Recompute the metadata
published_metadata, _ = AssetMetadata.objects.get_or_create(
metadata=published_asset._populate_metadata(),
)
published_asset.metadata = published_metadata
def publish(self):
"""
Modify the metadata of this asset as if it were being published.

return published_asset
This is useful to validate asset metadata without saving it.
To actually publish this Asset, simply save() after calling publish().
"""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the link to bulk_update

self.metadata = self.published_metadata()
self.published = True

def save(self, *args, **kwargs):
metadata = self._populate_metadata()
Expand Down
3 changes: 1 addition & 2 deletions dandiapi/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def validate_asset_metadata(asset_id: int) -> None:
asset.save()

try:
publish_asset = Asset.published_asset(asset)
metadata = publish_asset.metadata.metadata
metadata = asset.published_metadata().metadata
validate(metadata, schema_key='PublishedAsset')
except Exception as e:
logger.error('Error while validating asset %s', asset_id)
Expand Down
10 changes: 4 additions & 6 deletions dandiapi/api/views/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ def publish(self, request, **kwargs):
from dandiapi.api.models import Asset

draft_assets = old_version.assets.filter(published=False).all()
new_published_assets = [Asset.published_asset(draft_asset) for draft_asset in draft_assets]
Asset.objects.bulk_create(new_published_assets)
for draft_asset in draft_assets:
draft_asset.publish()
Asset.objects.bulk_update(draft_assets, ['metadata', 'published'])

AssetVersions.objects.bulk_create(
[
AssetVersions(asset_id=asset.id, version_id=new_version.id)
for asset in new_published_assets
]
[AssetVersions(asset_id=asset.id, version_id=new_version.id) for asset in draft_assets]
)

# Save again to recompute metadata, specifically assetsSummary
Expand Down