Skip to content

Commit

Permalink
results: added support for drafts
Browse files Browse the repository at this point in the history
* Added support for drafts in the results list
  • Loading branch information
alejandromumo committed Mar 25, 2024
1 parent 07b94f4 commit ae88913
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
FromConfigRequiredPIDs,
)
from .permissions import RDMRecordPermissionPolicy
from .results import RDMRecordList
from .result_items import GrantItem, GrantList, SecretLinkItem, SecretLinkList
from .schemas import RDMParentSchema, RDMRecordSchema
from .schemas.community_records import CommunityRecordsSchema
Expand Down Expand Up @@ -251,6 +252,7 @@ class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
link_result_list_cls = SecretLinkList
grant_result_item_cls = GrantItem
grant_result_list_cls = GrantList
result_list_cls = RDMRecordList

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

Expand Down
31 changes: 30 additions & 1 deletion invenio_rdm_records/services/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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.records.results import ExpandableField
from invenio_records_resources.services.records.results import ExpandableField, RecordList
from invenio_users_resources.proxies import current_user_resources

from .dummy import DummyExpandingService
Expand Down Expand Up @@ -69,3 +69,32 @@ def get_value_service(self, value):
def pick(self, identity, resolved_rec):
"""Pick fields defined in the entity resolver."""
return resolved_rec

class RDMRecordList(RecordList):
"""Record list with custom fields."""

@property
def hits(self):
"""Iterator over the hits."""
for hit in self._results:
# Load dump
record_dict = hit.to_dict()
if record_dict["is_published"]:
record = self._service.record_cls.loads(record_dict)
else:
record = self._service.draft_cls.loads(record_dict)

# Project the record
projection = self._schema.dump(
record,
context=dict(
identity=self._identity,
record=record,
),
)
if self._links_item_tpl:
projection["links"] = self._links_item_tpl.expand(
self._identity, record
)

yield projection

0 comments on commit ae88913

Please sign in to comment.