Skip to content

Commit

Permalink
records: JSON schema API endpoint
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
sonar.modules.pdf_extractor.views.client:blueprint'
],
'invenio_base.api_blueprints': [
'sonar = sonar.theme.api_views:blueprint',
'pdf_extractor = sonar.modules.pdf_extractor.views.api:blueprint',
'deposits = sonar.modules.deposits.rest:blueprint'
],
Expand Down
59 changes: 59 additions & 0 deletions sonar/theme/api_views.py
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)
5 changes: 5 additions & 0 deletions tests/ui/data/json_to_compile.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
}
},
"required": [
"pid",
"test"
],
"additionalProperties": false,
"properties": {
"pid": {
"title": "PID",
"type": "string"
},
"test": {
"title": "Test",
"type": "string",
Expand Down
50 changes: 50 additions & 0 deletions tests/ui/test_api_views.py
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

0 comments on commit d1f9d99

Please sign in to comment.