From 17fbe9e6add2620f2c813bfe835c7ffd85dd7c27 Mon Sep 17 00:00:00 2001 From: Aly Badr Date: Wed, 4 Aug 2021 16:46:59 +0200 Subject: [PATCH] template: remove pid from template record * Closes #2269 Co-Authored-by: Aly Badr --- rero_ils/modules/templates/api.py | 3 ++ rero_ils/modules/templates/extensions.py | 33 ++++++++++++++++++++++ tests/api/templates/test_templates_rest.py | 7 +++++ 3 files changed, 43 insertions(+) create mode 100644 rero_ils/modules/templates/extensions.py diff --git a/rero_ils/modules/templates/api.py b/rero_ils/modules/templates/api.py index 6c986e4097..0470b9cfa6 100644 --- a/rero_ils/modules/templates/api.py +++ b/rero_ils/modules/templates/api.py @@ -20,6 +20,7 @@ from functools import partial +from .extensions import RemoveDataPidExtension from .models import TemplateIdentifier, TemplateMetadata from ..api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch from ..fetchers import id_fetcher @@ -56,6 +57,8 @@ class Meta: class Template(IlsRecord): """Templates class.""" + _extensions = [RemoveDataPidExtension()] + minter = template_id_minter fetcher = template_id_fetcher provider = TemplateProvider diff --git a/rero_ils/modules/templates/extensions.py b/rero_ils/modules/templates/extensions.py new file mode 100644 index 0000000000..9dd13a82db --- /dev/null +++ b/rero_ils/modules/templates/extensions.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2021 RERO +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +"""Template record extensions.""" + +from invenio_records.extensions import RecordExtension + + +class RemoveDataPidExtension(RecordExtension): + """Defines the methods needed by an extension.""" + + def post_init(self, record, data, model=None, **kwargs): + """Called after a record is initialized. + + :param data: The dict passed to the record's constructor + :param model: The model class used for initialization. + """ + # force removing of record pid + record.get('data', {}).pop('pid', None) diff --git a/tests/api/templates/test_templates_rest.py b/tests/api/templates/test_templates_rest.py index 044f52f9de..7bc63035a1 100644 --- a/tests/api/templates/test_templates_rest.py +++ b/tests/api/templates/test_templates_rest.py @@ -207,12 +207,16 @@ def test_template_secure_api_create(client, json_header, post_entrypoint = 'invenio_records_rest.tmpl_list' del templ_doc_public_martigny_data['pid'] + # add a pid to the record data + templ_doc_public_martigny_data['data']['pid'] = 'toto' res, _ = postdata( client, post_entrypoint, templ_doc_public_martigny_data ) assert res.status_code == 201 + # ensure that pid is removed from recordds + assert 'pid' not in res.json['metadata']['data'] # Sion login_user_via_session(client, system_librarian_sion.user) @@ -251,12 +255,15 @@ def test_template_secure_api_update(client, login_user_via_session(client, librarian_martigny.user) data = templ_doc_private_martigny_data data['name'] = 'Test Name' + data['data']['pid'] = 'toto' res = client.put( record_url, data=json.dumps(data), headers=json_header ) assert res.status_code == 200 + # ensure that pid is removed from recordds + assert 'pid' not in res.json['metadata']['data'] data = templ_doc_private_martigny_data data['visibility'] = 'public'