-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
organisation: create default organisation
* Creates a default organisation during setup. * Associates default organisation to users. * Adds a CLI command for creating organisations from file. * Sets organisation as required in user JSON schema. Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
- Loading branch information
Sébastien Délèze
committed
May 20, 2020
1 parent
dbd3371
commit b1d1d5c
Showing
15 changed files
with
203 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"code": "rero", | ||
"name": "RERO" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Swiss Open Access Repository | ||
# Copyright (C) 2019 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/>. | ||
|
||
"""Documents CLI commands.""" | ||
|
||
import json | ||
|
||
import click | ||
from click.exceptions import ClickException | ||
from flask.cli import with_appcontext | ||
from invenio_db import db | ||
|
||
from sonar.modules.organisations.api import OrganisationIndexer, \ | ||
OrganisationRecord | ||
|
||
|
||
@click.group() | ||
def organisations(): | ||
"""Organisations CLI commands.""" | ||
|
||
|
||
@organisations.command('import') | ||
@click.argument('file', type=click.File('r')) | ||
@with_appcontext | ||
def import_organisations(file): | ||
"""Import organisations from JSON file.""" | ||
click.secho('Importing organisations from {file}'.format(file=file.name)) | ||
|
||
indexer = OrganisationIndexer() | ||
|
||
for record in json.load(file): | ||
try: | ||
# Check existence in DB | ||
db_record = OrganisationRecord.get_record_by_pid(record['code']) | ||
|
||
if db_record: | ||
raise ClickException('Record already exists in DB') | ||
|
||
# Register record to DB | ||
db_record = OrganisationRecord.create(record) | ||
db.session.commit() | ||
|
||
indexer.index(db_record) | ||
except Exception as error: | ||
click.secho( | ||
'Organisation {org} could not be imported: {error}'.format( | ||
org=record, error=str(error)), | ||
fg='red') | ||
|
||
click.secho('Finished', fg='green') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/ui/organisations/cli/test_organisations_cli_organisations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Swiss Open Access Repository | ||
# Copyright (C) 2019 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 CLI for organisations.""" | ||
|
||
from click.testing import CliRunner | ||
|
||
import sonar.modules.organisations.cli.organisations as Cli | ||
from sonar.modules.organisations.api import OrganisationRecord | ||
|
||
|
||
def test_import_organisations(app, script_info): | ||
"""Test import organisations.""" | ||
runner = CliRunner() | ||
|
||
datastore = app.extensions['security'].datastore | ||
datastore.create_role(name='admin') | ||
|
||
# Import ok | ||
result = runner.invoke(Cli.import_organisations, | ||
['./tests/ui/organisations/data/valid.json'], | ||
obj=script_info) | ||
organisation = OrganisationRecord.get_record_by_pid('test') | ||
assert organisation | ||
assert organisation['pid'] == 'test' | ||
|
||
# Already existing | ||
result = runner.invoke(Cli.import_organisations, | ||
['./tests/ui/organisations/data/valid.json'], | ||
obj=script_info) | ||
assert result.output.find( | ||
'Organisation {\'code\': \'test\', \'name\': \'Test\'} could not be ' | ||
'imported: Record already exists in DB' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"code": "test", | ||
"name": "Test" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.