-
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.
* Creates an API endpoint to get the JSON schema corresponding to a resource. Co-Authored-by: Sébastien Délèze <[email protected]>
- Loading branch information
Sébastien Délèze
committed
Jan 30, 2020
1 parent
1af2e46
commit d1f9d99
Showing
4 changed files
with
115 additions
and
0 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
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,59 @@ | ||
# -*- 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/>. | ||
|
||
"""Blueprint used for loading templates. | ||
The sole purpose of this blueprint is to ensure that Invenio can find the | ||
templates and static files located in the folders of the same names next to | ||
this file. | ||
""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
import copy | ||
import re | ||
|
||
from flask import Blueprint, abort, jsonify | ||
from invenio_jsonschemas import current_jsonschemas | ||
from invenio_jsonschemas.errors import JSONSchemaNotFound | ||
|
||
blueprint = Blueprint('api_sonar', __name__) | ||
|
||
|
||
def prepare_jsonschema(schema): | ||
"""Json schema prep.""" | ||
schema = copy.deepcopy(schema) | ||
if schema.get('$schema'): | ||
del schema['$schema'] | ||
schema['required'].remove('pid') | ||
return schema | ||
|
||
|
||
@blueprint.route('/schemaform/<document_type>') | ||
def schemaform(document_type): | ||
"""Return schema and form options for the editor.""" | ||
doc_type = document_type | ||
doc_type = re.sub('ies$', 'y', doc_type) | ||
doc_type = re.sub('s$', '', doc_type) | ||
try: | ||
current_jsonschemas.get_schema.cache_clear() | ||
schema_name = '{}/{}-v1.0.0.json'.format(document_type, doc_type) | ||
schema = current_jsonschemas.get_schema(schema_name) | ||
|
||
return jsonify({'schema': prepare_jsonschema(schema)}) | ||
except JSONSchemaNotFound: | ||
abort(404) |
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,50 @@ | ||
# -*- 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 SONAR api views.""" | ||
|
||
import json | ||
import os | ||
|
||
from invenio_app.factory import create_api | ||
|
||
from sonar.theme.api_views import prepare_jsonschema | ||
|
||
create_app = create_api | ||
|
||
|
||
def test_prepare_jsonschema(): | ||
"""Test JSON schema transformation before returning by API endpoint.""" | ||
with open( | ||
os.path.dirname(os.path.abspath(__file__)) + | ||
'/data/json_to_compile.json') as json_file: | ||
schema = json.load(json_file) | ||
assert '$schema' in schema | ||
assert 'pid' in schema['required'] | ||
|
||
schema = prepare_jsonschema(schema) | ||
assert '$schema' not in schema | ||
assert 'pid' not in schema['required'] | ||
|
||
|
||
def test_json_schema_form(client): | ||
"""Test JSON schema form api.""" | ||
res = client.get('/schemaform/documents') | ||
assert res.status_code == 200 | ||
|
||
res = client.get('/schemaform/not_existing') | ||
assert res.status_code == 404 |