Skip to content

Commit

Permalink
service: improve read_latest
Browse files Browse the repository at this point in the history
* Allows to fetch the latest version by
  passing the parent id
* closes zenodo/rdm-project#174
  • Loading branch information
jrcastro2 committed Aug 9, 2023
1 parent 50ae2d1 commit e59bd50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions invenio_drafts_resources/services/records/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,23 @@ def read_draft(self, identity, id_, expand=False):
)

def read_latest(self, identity, id_, expand=False):
"""Retrieve latest record."""
"""Retrieve latest record.
If provided with the ID of a parent record it will resolve it and return the latest version of the record.
"""
# Resolve and require permission
record = self.record_cls.pid.resolve(id_)

try:
record = self.record_cls.pid.resolve(id_)
except NoResultFound:
parent_pid = self.record_cls.parent_record_cls.pid.resolve(id_)
version_state = self.record_cls.versions.resolve(
parent_id=parent_pid.pid.object_uuid
)
if version_state and version_state.latest_id:
record = self.record_cls.get_record(version_state.latest_id)
else:
raise NoResultFound("Failed to fetch the record versions.")

# Retrieve latest if record is not
if not record.versions.is_latest:
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/test_record_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_search_versions(client, headers, input_data, location, search_clear):


def test_create_publish_new_revision(
client, headers, input_data, location, search_clear
app, db, client, headers, input_data, location, search_clear
):
"""Test draft creation of an existing record and publish it."""
recid = _create_and_publish(client, headers, input_data)
Expand Down

0 comments on commit e59bd50

Please sign in to comment.