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

metadata: auto-populate publication_date on new versions #1616

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions invenio_rdm_records/services/components/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ class CustomFieldsComponent(MetadataComponent):

field = "custom_fields"
new_version_skip_fields = []
new_version_generate_fields = []
10 changes: 8 additions & 2 deletions invenio_rdm_records/services/components/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""RDM service component for metadata."""

from copy import copy
from datetime import datetime

from invenio_drafts_resources.services.records.components import ServiceComponent

Expand All @@ -19,6 +20,9 @@ class MetadataComponent(ServiceComponent):

field = "metadata"
new_version_skip_fields = ["publication_date", "version"]
new_version_generated_fields = {
Copy link
Member

Choose a reason for hiding this comment

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

minor: why not removing publication_date from new_version_skip_fields? It would simply import the previous version's publication date and then the user could modify it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered that initially, but this is how we had it in legacy and is the reason we ended up with many records that just had the exact same publication_date across all versions.

"publication_date": lambda draft, record: datetime.today(timezone.utc).isoformat(),
}

def create(self, identity, data=None, record=None, **kwargs):
"""Inject parsed metadata to the record."""
Expand All @@ -42,5 +46,7 @@ def new_version(self, identity, draft=None, record=None, **kwargs):
# Remove fields that should not be copied to the new version
# (publication date and version)
field_values = getattr(draft, self.field)
for f in self.new_version_skip_fields:
field_values.pop(f, None)
for key in self.new_version_skip_fields:
field_values.pop(key, None)
for key, func in self.new_version_generated_fields.items():
field_values[key] = func(draft, record)
Loading