Skip to content

Commit b3bbe03

Browse files
jmaPascalRepond
andcommitted
urn: add new successor command.
* 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]>
1 parent 42444ab commit b3bbe03

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

sonar/modules/documents/cli/urn.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
2828

2929
from sonar.modules.documents.api import DocumentRecord
30-
from sonar.modules.documents.dnb import DnbUrnService
30+
from sonar.modules.documents.dnb import DnbServerError, DnbUrnService
3131
from sonar.modules.documents.urn import Urn
3232
from sonar.snl.ftp import SNLRepository
3333

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

107107

108+
108109
@urn.command('snl-upload-file')
109110
@click.argument('urn_code')
110111
@with_appcontext
@@ -184,3 +185,23 @@ def snl_list_files():
184185
)
185186
snl_repository.connect()
186187
snl_repository.list()
188+
189+
@urn.command()
190+
@click.argument('urn')
191+
@click.argument('successor_urn')
192+
@with_appcontext
193+
def successor(urn, successor_urn):
194+
"""Set a successor for a given run.
195+
196+
:param urn: str - URN identifier
197+
:param successor_urn: str - Sucessor URN identifier
198+
"""
199+
try:
200+
DnbUrnService().set_successor(urn, successor_urn)
201+
click.secho(
202+
f'Added successfully a successor.',
203+
fg='green')
204+
except DnbServerError as err:
205+
click.secho(
206+
str(err),
207+
fg='red')

sonar/modules/documents/dnb.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ def update(cls, urn_code, urls):
148148
f'when we update the information of the following '
149149
f'urn: {urn_code}')
150150

151+
@classmethod
152+
def set_successor(cls, urn_code, successor_urn):
153+
"""Set the successor of a given urn.
154+
155+
:param urn_code: str - the urn code.
156+
:param successor_urn: str - the urn code of the successor.
157+
"""
158+
response = requests.request(
159+
'PATCH',
160+
f"{cls.base_url()}/urn/{urn_code}",
161+
headers=cls.headers(),
162+
data=json.dumps(dict(successor=f'{cls.base_url()}/urn/{successor_urn}'))
163+
)
164+
if response.status_code != 204:
165+
msg = response.json().get('developerMessage', '')
166+
raise DnbServerError(
167+
f'Bad DNB server response status {response.status_code}, '
168+
f'when we update the information of the following '
169+
f'urn: {urn_code}',
170+
f'{msg}')
171+
151172
@classmethod
152173
def create(cls, data):
153174
"""Register a new URN to the DBN service with a list of urls.

tests/api/documents/test_dnb_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def test_dnb_rest_api_verify_exist(app):
3333
urn_code = 'urn:nbn:ch:rero-006-119656'
3434
assert DnbUrnService.exists(urn_code)
3535

36-
3736
def test_dnb_rest_api_verify_not_exist(app):
3837
"""Test dnb rest api verify code does not exist."""
3938
with requests_mock.mock() as response:

tests/ui/documents/test_dnb.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Swiss Open Access Repository
4+
# Copyright (C) 2024 RERO
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as published by
8+
# the Free Software Foundation, version 3 of the License.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
"""Test DnbUrnService API."""
19+
20+
21+
import requests_mock
22+
23+
from sonar.modules.documents.dnb import DnbUrnService
24+
25+
26+
def test_dnb_sucessor(app):
27+
"""Test a successor assignment."""
28+
with requests_mock.mock() as response:
29+
response.patch(requests_mock.ANY, status_code=204)
30+
DnbUrnService().set_successor(
31+
'urn:nbn:ch:rero-002-old', 'urn:nbn:ch:rero-002-new')

0 commit comments

Comments
 (0)