Skip to content

Commit

Permalink
Record: Improve handling of draft PID in RecordCommunitiesService (#1822
Browse files Browse the repository at this point in the history
)

* Records: catch PIDUnregistered error
* Add draft class to RecordCommunitiesService
* Closes inveniosoftware/invenio-app-rdm#2857
  • Loading branch information
Samk13 authored Sep 30, 2024
1 parent be02ccd commit 34d67a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 12 additions & 3 deletions invenio_rdm_records/services/communities/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023-2024 CERN.
# Copyright (C) 2024 Graz University of Technology.
# Copyright (C) 2024 Graz University of Technology.
# Copyright (C) 2024 KTH Royal Institute of Technology.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -17,7 +18,7 @@
)
from invenio_i18n import lazy_gettext as _
from invenio_notifications.services.uow import NotificationOp
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_pidstore.errors import PIDDoesNotExistError, PIDUnregistered
from invenio_records_resources.services import (
RecordIndexerMixin,
Service,
Expand Down Expand Up @@ -67,6 +68,11 @@ def record_cls(self):
"""Factory for creating a record class."""
return self.config.record_cls

@property
def draft_cls(self):
"""Factory for creating a draft class."""
return self.config.draft_cls

def _exists(self, community_id, record):
"""Return the request id if an open request already exists, else None."""
results = current_requests_service.search(
Expand Down Expand Up @@ -264,7 +270,10 @@ def search(
**kwargs,
):
"""Search for record's communities."""
record = self.record_cls.pid.resolve(id_)
try:
record = self.record_cls.pid.resolve(id_)
except PIDUnregistered:
record = self.draft_cls.pid.resolve(id_, registered_only=False)
self.require_permission(identity, "read", record=record)

communities_ids = record.parent.communities.ids
Expand Down
4 changes: 3 additions & 1 deletion invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Copyright (C) 2020-2021 Northwestern University.
# Copyright (C) 2021 TU Wien.
# Copyright (C) 2021-2023 Graz University of Technology.
# Copyright (C) 2022 Universität Hamburg
# Copyright (C) 2022 Universität Hamburg
# Copyright (C) 2024 KTH Royal Institute of Technology.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -260,6 +261,7 @@ class RDMRecordCommunitiesConfig(ServiceConfig, ConfiguratorMixin):
service_id = "record-communities"

record_cls = FromConfig("RDM_RECORD_CLS", default=RDMRecord)
draft_cls = FromConfig("RDM_DRAFT_CLS", default=RDMDraft)
permission_policy_cls = FromConfig(
"RDM_PERMISSION_POLICY", default=RDMRecordPermissionPolicy, import_string=True
)
Expand Down

0 comments on commit 34d67a4

Please sign in to comment.