Skip to content

Commit

Permalink
resources: expose record revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Jan 22, 2025
1 parent 3be6878 commit 7e0cf40
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions invenio_rdm_records/resources/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,22 @@ def p(route):
route("POST", p(routes["set-record-quota"]), self.set_record_quota),
# TODO: move to users?
route("POST", routes["set-user-quota"], self.set_user_quota),
route("GET", p(f'{routes["item"]}/revisions'), self.read_revisions),
]

return url_rules

@request_extra_args
@request_view_args
@response_handler(many=True)
def read_revisions(self):
"""Discard a previously reserved PID."""
item = self.service.read_revisions(
identity=g.identity, id_=resource_requestctx.view_args["pid_value"]
)

return item.to_dict(), 200

@request_extra_args
@request_read_args
@request_view_args
Expand Down
3 changes: 2 additions & 1 deletion invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
)
from .permissions import RDMRecordPermissionPolicy
from .result_items import GrantItem, GrantList, SecretLinkItem, SecretLinkList
from .results import RDMRecordList
from .results import RDMRecordList, RDMRecordRevisionsList
from .schemas import RDMParentSchema, RDMRecordSchema
from .schemas.community_records import CommunityRecordsSchema
from .schemas.parent.access import AccessSettingsSchema
Expand Down Expand Up @@ -422,6 +422,7 @@ class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
grant_result_item_cls = GrantItem
grant_result_list_cls = GrantList
result_list_cls = RDMRecordList
revision_result_list_cls = RDMRecordRevisionsList

default_files_enabled = FromConfig("RDM_DEFAULT_FILES_ENABLED", default=True)

Expand Down
2 changes: 2 additions & 0 deletions invenio_rdm_records/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class RDMRecordPermissionPolicy(RecordPermissionPolicy):
# Allow submitting new record
can_create = can_authenticated

can_read_revisions = [Administration()]

#
# Drafts
#
Expand Down
33 changes: 33 additions & 0 deletions invenio_rdm_records/services/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from invenio_communities.communities.entity_resolvers import pick_fields
from invenio_communities.communities.schema import CommunityGhostSchema
from invenio_communities.proxies import current_communities
from invenio_records_resources.services.base.results import (
ServiceItemResult,
ServiceListResult,
)
from invenio_records_resources.services.records.results import (
ExpandableField,
RecordList,
Expand Down Expand Up @@ -105,3 +109,32 @@ def hits(self):
)

yield projection


class RDMRecordRevisionsList(ServiceListResult):
"""Record revisions list.
We need a custom result class to handle the record revisions list as they are stored only in DB.
"""

def __init__(self, identity, revisions):
"""Instantiate a Collection tree list item."""
self._identity = identity
self._revisions = revisions

def to_dict(self):
"""Serialize the collection tree list to a dictionary."""
res = map(
lambda revision: {
"updated": revision.updated,
"created": revision.created,
"revision_id": revision.transaction_id,
"json": revision.json,
},
self._revisions,
)
return list(res)

def __iter__(self):
"""Iterate over the collection revisions."""
return iter(self._revisions)
12 changes: 12 additions & 0 deletions invenio_rdm_records/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,15 @@ def set_user_quota(
db.session.add(user_quota)

return True

def read_revisions(self, identity, id_):
"""Return a list of record revisions."""
record = self.record_cls.pid.resolve(id_)
# Check permissions
self.require_permission(identity, "read_revisions", record=record)
revisions = list(reversed(record.model.versions.all()))

return self.config.revision_result_list_cls(
identity,
revisions,
)

0 comments on commit 7e0cf40

Please sign in to comment.