Skip to content

Commit

Permalink
template: remove pid from template record
Browse files Browse the repository at this point in the history
* Closes rero#2269

Co-Authored-by: Aly Badr <aly.badr@rero.ch>
Aly Badr committed Aug 10, 2021
1 parent 2cca223 commit 17fbe9e
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rero_ils/modules/templates/api.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions rero_ils/modules/templates/extensions.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

"""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)
7 changes: 7 additions & 0 deletions tests/api/templates/test_templates_rest.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 17fbe9e

Please sign in to comment.