Skip to content
Open
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
70 changes: 49 additions & 21 deletions cds_migrator_kit/rdm/records/load/entities/approval_request_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"""Creates and approves a migrated EP committee approval request."""

from contextlib import contextmanager

from cds_rdm.requests.committee_approval import APPRN_PID_TYPE, CommitteeApprovalRequest
from flask import current_app
from invenio_access.permissions import system_identity
Expand All @@ -15,6 +17,7 @@
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_rdm_records.records.api import RDMParent
from invenio_records_resources.services.uow import RecordCommitOp
from invenio_requests.customizations import actions
from invenio_requests.customizations.event_types import (
LogEventType,
ReviewersUpdatedType,
Expand All @@ -27,6 +30,22 @@
from .approval_request import ApprovalRequest


@contextmanager
def _create_without_submit(request_type):
"""Use plain CreateAction for ``request_type`` (no submit side-effects/emails).

Same idea as ``CommunitySubmission`` / ``RequestLoad``: create in ``created``
status, then set status by hand. ``CommitteeApprovalRequest`` normally maps
create → create-and-submit (validates, grants referees, emails).
"""
original = request_type.available_actions["create"]
request_type.available_actions["create"] = actions.CreateAction
try:
yield
finally:
request_type.available_actions["create"] = original


class ApprovalRequestLoad:
"""Creates and approves a migrated EP committee approval request.

Expand Down Expand Up @@ -74,8 +93,8 @@ def create(self, restricted_record_state, uow=None):
)
self._mint_apprn_pid(restricted_record_state["latest_version_object_uuid"])

def _get_referee_group(self, restricted_parent):
"""Get the EP approval referee group from the restricted record."""
def _get_community_config(self, restricted_parent):
"""Return the CDS_COMMITTEE_APPROVAL_COMMUNITIES config for the record."""
default_community_id = restricted_parent.get("communities", {}).get("default")
if not default_community_id:
raise UnexpectedValue(
Expand All @@ -95,7 +114,7 @@ def _get_referee_group(self, restricted_parent):
stage="load",
priority="critical",
)
return ep_config["referee_group"]
return ep_config

def _mint_apprn_pid(self, restricted_version_uuid):
"""Mint the APPRN PID."""
Expand Down Expand Up @@ -213,28 +232,37 @@ def _create_request(self, restricted_recid, restricted_parent, uow=None):
inner_uow.commit()

def _build_request(self, restricted_recid, restricted_parent, uow):
"""Register the request creation and its updates on the given uow."""
"""Register the request creation and its updates on the given uow.

Mirrors ``RequestLoad.create_submission_request``: create without
submit/accept actions, then set status (and timeline) by hand so no
referee emails are queued.
"""
approval_request = self.approval_request
expires_at = approval_request.parse_legacy_datetime(
approval_request.waiting_entry.get("deadline")
)
referee_group = self._get_referee_group(restricted_parent)

request_item = current_requests_service.create(
system_identity,
data={
"title": f'EP approval for "{approval_request.title}"',
"payload": {},
},
request_type=CommitteeApprovalRequest,
receiver={"group": referee_group},
creator=approval_request.resolve_user_by_email(
approval_request.waiting_entry.get("submitted_by"), "submitter"
),
topic={"record": restricted_recid},
expires_at=expires_at,
uow=uow,
)
community_config = self._get_community_config(restricted_parent)
# Same source as cds-rdm's submit view — used in UI and email templates.
approval_label = community_config.get("label") or "Committee approval"

with _create_without_submit(CommitteeApprovalRequest):
request_item = current_requests_service.create(
system_identity,
data={
"title": f'EP approval for "{approval_request.title}"',
"payload": {"approval_label": approval_label},
},
request_type=CommitteeApprovalRequest,
receiver={"group": community_config["referee_group"]},
creator=approval_request.resolve_user_by_email(
approval_request.waiting_entry.get("submitted_by"), "submitter"
),
topic={"record": restricted_recid},
expires_at=expires_at,
uow=uow,
)

request = request_item._record
request.number = f"lrecid:{approval_request.legacy_recid}:ep-approval"
request.status = "submitted"
Expand Down
Loading