Skip to content

Commit

Permalink
urn: add new successor command.
Browse files Browse the repository at this point in the history
* Adds a new command to set a successor, typically for duplicate documents.

Co-Authored-by: Johnny Mariéthoz <[email protected]>
Co-Authored-by: Pascal Repond <[email protected]>
  • Loading branch information
jma and PascalRepond committed Jul 9, 2024
1 parent 42444ab commit d7b2663
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
23 changes: 22 additions & 1 deletion sonar/modules/documents/cli/urn.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from invenio_pidstore.models import PersistentIdentifier, PIDStatus

from sonar.modules.documents.api import DocumentRecord
from sonar.modules.documents.dnb import DnbUrnService
from sonar.modules.documents.dnb import DnbServerError, DnbUrnService
from sonar.modules.documents.urn import Urn
from sonar.snl.ftp import SNLRepository

Expand Down Expand Up @@ -105,6 +105,7 @@ def register():
click.secho(f'{idx} URN registered.', fg='green')



@urn.command('snl-upload-file')
@click.argument('urn_code')
@with_appcontext
Expand Down Expand Up @@ -184,3 +185,23 @@ def snl_list_files():
)
snl_repository.connect()
snl_repository.list()

@urn.command()
@click.argument('urn')
@click.argument('successor_urn')
@with_appcontext
def successor(urn, successor_urn):
"""Set a successor for a given run.
:param urn: str - URN identifier
:param successor_urn: str - Sucessor URN identifier
"""
try:
DnbUrnService().set_successor(urn, successor_urn)
click.secho(
f'Added successfully a successor.',
fg='green')
except DnbServerError as err:
click.secho(
str(err),
fg='red')
21 changes: 21 additions & 0 deletions sonar/modules/documents/dnb.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ def update(cls, urn_code, urls):
f'when we update the information of the following '
f'urn: {urn_code}')

@classmethod
def set_successor(cls, urn_code, successor_urn):
"""Set the successor of a given urn.
:param urn_code: str - the urn code.
:param successor_urn: str - the urn code of the successor.
"""
response = requests.request(
'PATCH',
f"{cls.base_url()}/urn/{urn_code}",
headers=cls.headers(),
data=json.dumps(dict(successor=f'{cls.base_url()}/urn/{successor_urn}'))
)
if response.status_code != 204:
msg = response.json().get('developerMessage', '')
raise DnbServerError(
f'Bad DNB server response status {response.status_code}, '
f'when we update the information of the following '
f'urn: {urn_code}',
f'{msg}')

@classmethod
def create(cls, data):
"""Register a new URN to the DBN service with a list of urls.
Expand Down
1 change: 0 additions & 1 deletion tests/api/documents/test_dnb_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_dnb_rest_api_verify_exist(app):
urn_code = 'urn:nbn:ch:rero-006-119656'
assert DnbUrnService.exists(urn_code)


def test_dnb_rest_api_verify_not_exist(app):
"""Test dnb rest api verify code does not exist."""
with requests_mock.mock() as response:
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/documents/test_dnb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2024 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/>.

"""Test DnbUrnService API."""


import requests_mock

from sonar.modules.documents.dnb import DnbUrnService


def test_dnb_sucessor(app):
"""Test a successor assignment."""
with requests_mock.mock() as response:
response.patch(requests_mock.ANY, status_code=204)
DnbUrnService().set_successor(
'urn:nbn:ch:rero-002-old', 'urn:nbn:ch:rero-002-new')

0 comments on commit d7b2663

Please sign in to comment.