From ad526a4f04ea29526be2a91f3657468d3c67c85a Mon Sep 17 00:00:00 2001 From: Pal Kerecsenyi Date: Thu, 18 Jun 2026 17:33:35 +0200 Subject: [PATCH] WIP: start refactor of public record view handler --- site/cds_rdm/requests/ep_public_record.py | 33 +++++++++++++++++++++++ site/cds_rdm/requests/views.py | 14 ++++------ 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 site/cds_rdm/requests/ep_public_record.py diff --git a/site/cds_rdm/requests/ep_public_record.py b/site/cds_rdm/requests/ep_public_record.py new file mode 100644 index 00000000..e67422f4 --- /dev/null +++ b/site/cds_rdm/requests/ep_public_record.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Invenio. +# Copyright (C) 2026 CERN. +# +# Invenio is free software; you can redistribute it and/or modify it +# under the terms of the GPL-2.0 License; see LICENSE file for more details. + +"""Helpers for creating a public record following an accepted EP Approval request.""" + +"""Create a public approved record from an approved draft. + +Requires the calling user to be a community manager/owner of the +record's enrolled community. + +Steps: +1. Read the approved draft — must have ep_approval.reportnumber set on parent. +2. Build a new public record: copy metadata + files, set access=public. +3. Create draft, import files, write ep_approval to both parents, publish. +4. Return the new public record id and links. +""" + + +from invenio_pidstore.models import PersistentIdentifier +from invenio_rdm_records.records.api import RDMRecord + + +def get_record_ep_approval(pid_value: str): + """Return the EP Approval object for a record by PID.""" + src_pid_obj = PersistentIdentifier.get("recid", pid_value) + src_rec_obj = RDMRecord.get_record(src_pid_obj.object_uuid) + ea = (src_rec_obj.parent.get("permission_flags") or {}).get("ep_approval") or {} + return ea diff --git a/site/cds_rdm/requests/views.py b/site/cds_rdm/requests/views.py index 6b29752f..e3a79ffe 100644 --- a/site/cds_rdm/requests/views.py +++ b/site/cds_rdm/requests/views.py @@ -27,6 +27,8 @@ from invenio_requests.proxies import current_requests_service from invenio_requests.resolvers.registry import ResolverRegistry +from cds_rdm.requests.ep_public_record import get_record_ep_approval + from .ep_approval import EPApprovalRequest @@ -99,10 +101,7 @@ def publish_public_record(pid_value): return jsonify({"message": str(e)}), 403 # Read ep_approval from the internal draft's parent. - src_pid_obj = PersistentIdentifier.get("recid", pid_value) - src_rec_obj = RDMRecord.get_record(src_pid_obj.object_uuid) - ea = (src_rec_obj.parent.get("permission_flags") or {}).get("ep_approval") or {} - report_number = ea.get("reportnumber") + ea = get_record_ep_approval(pid_value) if not report_number: return jsonify({"message": "Record has no approved report number."}), 400 @@ -114,10 +113,9 @@ def publish_public_record(pid_value): if not default_community_id: return jsonify({"message": "Record has no default community."}), 400 - identity = g.identity allowed_roles = ("curator", "manager", "owner") if not any( - CommunityRoleNeed(default_community_id, role) in identity.provides + CommunityRoleNeed(default_community_id, role) in g.identity.provides for role in allowed_roles ): return jsonify({"message": "Permission denied"}), 403 @@ -128,9 +126,7 @@ def publish_public_record(pid_value): try: appr_pid = PersistentIdentifier.get("recid", approved_version_recid) appr_rec = RDMRecord.get_record(appr_pid.object_uuid) - cur_pid = PersistentIdentifier.get("recid", pid_value) - cur_rec = RDMRecord.get_record(cur_pid.object_uuid) - if cur_rec.versions.index < appr_rec.versions.index: + if src_rec_obj.versions.index < appr_rec.versions.index: return ( jsonify( {