Skip to content

Commit f644a44

Browse files
committed
fix: remove the oai set during the organisation creation
* Removes the automatic oai set creation during the organisation creation. * Adds the organisation OAI sets during the setup. Co-Authored-by: Johnny Mariéthoz <[email protected]>
1 parent 3c8cb08 commit f644a44

File tree

8 files changed

+61
-53
lines changed

8 files changed

+61
-53
lines changed

scripts/setup

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ invenio flask_wiki index
9696
section "Create organisations" "info"
9797
invenio fixtures organisations import "${fixtures_folder}/organisations/data.json"
9898

99+
# Create OAI sets for organisations
100+
section "Create organisations OAI sets" "info"
101+
invenio documents oai create-set rero "RERO+" 'organisation.code:rero'
102+
invenio documents oai create-set fictivededicated "Université des sciences fictives" 'organisation.code:fictivededicated'
103+
invenio documents oai create-set fictiveshared "École des hautes études partagées" 'organisation.code:fictiveshared'
104+
99105
section "Create users" "info"
100106
invenio fixtures users import "${fixtures_folder}/users/data.json"
101107

sonar/modules/documents/cli/documents.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
from invenio_db import db
2424
from rero_invenio_files.pdf import PDFGenerator
2525

26-
from sonar.modules.documents.cli.rerodoc import rerodoc
27-
from sonar.modules.documents.cli.urn import urn
2826
from sonar.modules.documents.serializers.schemas.dc import DublinCoreSchema
2927

3028
from ..api import DocumentIndexer, DocumentRecord
29+
from .oai import oai
30+
from .rerodoc import rerodoc
31+
from .urn import urn
3132

3233

3334
@click.group()
@@ -37,6 +38,7 @@ def documents():
3738

3839
documents.add_command(rerodoc)
3940
documents.add_command(urn)
41+
documents.add_command(oai)
4042

4143

4244
@click.group("documents")
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Swiss Open Access Repository
2-
# Copyright (C) 2021 RERO
2+
# Copyright (C) 2022 RERO
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU Affero General Public License as published by
@@ -13,25 +13,32 @@
1313
# You should have received a copy of the GNU Affero General Public License
1414
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

16-
"""Test OAI sets corresponding to organisation."""
16+
"""OAI specific CLI commands."""
1717

18+
import click
19+
from flask.cli import with_appcontext
20+
from invenio_db import db
1821
from invenio_oaiserver.models import OAISet
1922

2023

21-
def test_oai_set(organisation, document):
22-
"""Test OAI set synchronisation with organisation."""
23-
# Document has a `_oai` property
24-
assert document["_oai"]["id"] == f"oai:sonar.ch:{document['pid']}"
24+
@click.group()
25+
def oai():
26+
"""URN specific commands."""
2527

26-
# Set for organisation exists
27-
sets = OAISet.query.all()
28-
assert len(sets) == 1
29-
assert sets[0].spec == "org"
30-
assert sets[0].name == "org"
3128

32-
# New name is updated
33-
organisation.update({"code": "org", "name": "New name"})
34-
sets = OAISet.query.all()
35-
assert len(sets) == 1
36-
assert sets[0].spec == "org"
37-
assert sets[0].name == "New name"
29+
@oai.command()
30+
@click.argument("code")
31+
@click.argument("name")
32+
@click.argument("pattern")
33+
@with_appcontext
34+
def create_set(code, name, pattern):
35+
oaiset = OAISet(
36+
spec=code,
37+
name=name,
38+
search_pattern=pattern,
39+
system_created=True,
40+
)
41+
db.session.add(oaiset)
42+
db.session.commit()
43+
click.secho(f"OAI set '{code}' created.", fg="green")
44+
click.secho("Please reindex existing documents if needed.", fg="yellow")

sonar/modules/documents/dojson/rerodoc/overdo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def create_organisation(organisation_key):
4747
"isShared": False,
4848
"isDedicated": False,
4949
},
50+
with_bucket=True,
5051
dbcommit=True,
5152
)
5253
organisation.reindex()

sonar/modules/organisations/api.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
from flask import has_request_context
2121
from flask.globals import request_ctx
22-
from invenio_db import db
23-
from invenio_oaiserver.models import OAISet
2422
from werkzeug.local import LocalProxy
2523

2624
from sonar.modules.users.api import current_user_record
@@ -115,21 +113,6 @@ class OrganisationRecord(SonarRecord):
115113
provider = OrganisationProvider
116114
schema = "organisations/organisation-v1.0.0.json"
117115

118-
@classmethod
119-
def create(cls, data, id_=None, dbcommit=False, with_bucket=True, **kwargs):
120-
"""Create organisation record."""
121-
# Create OAI set
122-
code = data["code"]
123-
oaiset = OAISet(
124-
spec=code,
125-
name=data["name"],
126-
search_pattern=f'organisation.code:"{code}"',
127-
system_created=True,
128-
)
129-
db.session.add(oaiset)
130-
131-
return super().create(data, id_=id_, dbcommit=dbcommit, with_bucket=with_bucket, **kwargs)
132-
133116
@classmethod
134117
def get_or_create(cls, code, name=None):
135118
"""Get or create an organisation.
@@ -147,18 +130,6 @@ def get_or_create(cls, code, name=None):
147130
organisation.reindex()
148131
return organisation
149132

150-
def update(self, data):
151-
"""Update data for record."""
152-
# Update OAI set name according to organisation's name
153-
oaiset = OAISet.query.filter(OAISet.spec == data["code"]).first()
154-
155-
if oaiset:
156-
oaiset.name = data["name"]
157-
db.session.commit()
158-
159-
super().update(data)
160-
return self
161-
162133

163134
class OrganisationIndexer(SonarIndexer):
164135
"""Indexing documents in Elasticsearch."""

sonar/modules/organisations/cli/organisations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def import_organisations(file):
5454
files = record.pop("files", [])
5555

5656
# Register record to DB
57-
db_record = OrganisationRecord.create(record)
57+
db_record = OrganisationRecord.create(record, with_bucket=True)
5858

5959
# Add files
6060
for file in files:
@@ -68,6 +68,7 @@ def import_organisations(file):
6868

6969
indexer.index(db_record)
7070
except Exception as error:
71+
raise error
7172
click.secho(
7273
f"Organisation {record} could not be imported: {error}",
7374
fg="red",

tests/conftest.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from invenio_access.models import ActionUsers, Role
3434
from invenio_db import db as db_
3535
from invenio_files_rest.models import Location
36+
from invenio_oaiserver.models import OAISet
3637
from invenio_queues.proxies import current_queues
3738
from invenio_search import current_search
3839
from sqlalchemy_utils import create_database, database_exists
@@ -87,7 +88,7 @@ def search(appctx):
8788
should used the function-scoped :py:data:`search_clear` fixture to leave the
8889
indexes clean for the following tests.
8990
"""
90-
from invenio_search import current_search, current_search_client
91+
from invenio_search import current_search_client
9192
from invenio_search.errors import IndexAlreadyExistsError
9293

9394
try:
@@ -236,16 +237,35 @@ def _make_organisation(code, is_shared=True):
236237
record = OrganisationRecord.get_record_by_pid(code)
237238

238239
if not record:
239-
record = OrganisationRecord.create(data, dbcommit=True)
240+
record = OrganisationRecord.create(data, dbcommit=True, with_bucket=True)
240241
db.session.commit()
241242
record.reindex()
242-
current_search.flush_and_refresh(index="documents-document-v1.0.0-percolators")
243+
current_search.flush_and_refresh(index="organisations")
243244

244245
return record
245246

246247
return _make_organisation
247248

248249

250+
@pytest.fixture()
251+
def org_oaiset(db, organisation):
252+
"""Create an OAISet for organisation."""
253+
code = organisation["code"]
254+
oaiset = OAISet(
255+
spec=code,
256+
name=organisation["name"],
257+
search_pattern=f'organisation.code:"{code}"',
258+
system_created=True,
259+
)
260+
db.session.add(oaiset)
261+
db.session.commit()
262+
current_search.flush_and_refresh(index="documents-document-v1.0.0-percolators")
263+
yield oaiset
264+
db.session.delete(oaiset)
265+
db.session.commit()
266+
current_search.flush_and_refresh(index="documents-document-v1.0.0-percolators")
267+
268+
249269
@pytest.fixture()
250270
def organisation(make_organisation):
251271
"""Create an organisation."""

tests/ui/documents/test_oaipmh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"""Test OAIPMH URLS."""
1919

2020

21-
def test_oaipmh_get(client, app, document):
21+
def test_oaipmh_get(client, org_oaiset, document):
2222
"""Test OAIPMH API."""
2323
res = client.get("/oai2d?verb=ListSets")
2424
assert res.status_code == 200

0 commit comments

Comments
 (0)