Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template: remove pid from template record #2270

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions rero_ils/modules/templates/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +57,8 @@ class Meta:
class Template(IlsRecord):
"""Templates class."""

_extensions = [RemoveDataPidExtension()]

minter = template_id_minter
fetcher = template_id_fetcher
provider = TemplateProvider
Expand Down
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
Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand Down