Skip to content

Commit

Permalink
schema: fix schema in production
Browse files Browse the repository at this point in the history
Co-Authored-by: Peter Weber <peter.weber@rero.ch>
  • Loading branch information
rerowep committed May 15, 2024
1 parent 6ff907d commit 55d4ee6
Show file tree
Hide file tree
Showing 35 changed files with 246 additions and 57 deletions.
13 changes: 10 additions & 3 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3248,11 +3248,18 @@ def _(x):

# JSONSchemas
# ===========
#: Hostname used in URLs for local JSONSchemas.
JSONSCHEMAS_URL_SCHEME = 'https'
"""Default json schema host."""
JSONSCHEMAS_HOST = 'bib.rero.ch'
JSONSCHEMAS_REPLACE_REFS = True
"""Default schema endpoint."""
JSONSCHEMAS_ENDPOINT = "/schemas"
"""Whether to resolve $ref before serving a schema."""
JSONSCHEMAS_REPLACE_REFS = False
"""Loader class used in ``JSONRef`` when replacing ``$ref``."""
JSONSCHEMAS_LOADER_CLS = 'rero_ils.jsonschemas.utils.JsonLoader'
"""Register the endpoints on the API app."""
JSONSCHEMAS_REGISTER_ENDPOINTS_API = False
"""Register the endpoints on the UI app."""
JSONSCHEMAS_REGISTER_ENDPOINTS_UI = False

# OAI-PMH
# =======
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Exhibition/course",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@
]
},
"descriptionConventions": {
"title": " Description conventions",
"title": "Description conventions",
"description": "Rules used for the descriptive content of the resource description",
"type": "array",
"minItems": 1,
"items": {
"title": " Description conventions",
"title": "Description conventions",
"type": "string",
"minLength": 1
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Link to an agent",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Agent (textual)",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Corporate body",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Entity (textual)",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Link to a genre, form",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Genre, form (textual)",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Link to a subject",
"type": "object",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ <h3>
{% if linked_documents_count < 2 %}
{{ _('article/volume') }}
{% else %}
{{ _(' articles/volumes') }}
{{ _('articles/volumes') }}
{% endif %}
</a>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Organisation",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Person",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Place",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Temporal",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Topic",
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Work",
"additionalProperties": false,
Expand Down
26 changes: 13 additions & 13 deletions rero_ils/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def register_import_api_blueprint(app):
def handle_bad_request(e):
return 'not found', 404

api_blueprint = Blueprint('api_imports', __name__)
blueprint = Blueprint('api_imports', __name__)
endpoints = app.config.get('RERO_IMPORT_REST_ENDPOINTS', {})
for key, config in endpoints.items():
# search view
Expand All @@ -233,7 +233,7 @@ def handle_bad_request(e):
import_class=config.get('import_class'),
import_size=config.get('import_size')
)
api_blueprint.add_url_rule(search_path, view_func=search_view)
blueprint.add_url_rule(search_path, view_func=search_view)

# record view
record_view_name = f'import_{key}_record'
Expand All @@ -242,44 +242,44 @@ def handle_bad_request(e):
record_view_name,
import_class=config.get('import_class')
)
api_blueprint.add_url_rule(record_path, view_func=record_view)
blueprint.add_url_rule(record_path, view_func=record_view)

api_blueprint.register_error_handler(
blueprint.register_error_handler(
ResultNotFoundOnTheRemoteServer,
handle_bad_request
)
app.register_blueprint(api_blueprint)
app.register_blueprint(blueprint)

@staticmethod
def register_users_api_blueprint(app):
"""User blueprints initialization."""
api_blueprint = Blueprint('api_users', __name__)
blueprint = Blueprint('api_users', __name__)

@api_blueprint.errorhandler(ValidationError)
@blueprint.errorhandler(ValidationError)
def validation_error(error):
"""Catch validation errors."""
return JSONSchemaValidationError(error=error).get_response()

api_blueprint.add_url_rule(
blueprint.add_url_rule(
'/users/<id>',
view_func=UsersResource.as_view('users_item')
)
api_blueprint.add_url_rule(
blueprint.add_url_rule(
'/users/',
view_func=UsersCreateResource.as_view('users_list')
)
app.register_blueprint(api_blueprint)
app.register_blueprint(blueprint)

@staticmethod
def register_sru_api_blueprint(app):
"""SRU blueprints initialization."""
api_blueprint = Blueprint('api_sru', __name__)
blueprint = Blueprint('api_sru', __name__)
sru_documents_search = SRUDocumentsSearch.as_view('documents')
api_blueprint.add_url_rule(
blueprint.add_url_rule(
'/sru/documents',
view_func=sru_documents_search
)
app.register_blueprint(api_blueprint)
app.register_blueprint(blueprint)

def init_config(self, app):
"""Initialize configuration."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Local fields",
"description": "Local fields allows to add data that is available at the organisation level only.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Operation log",
"description": "Operation log keeps the operation history of a resource.",
Expand Down
59 changes: 57 additions & 2 deletions rero_ils/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@

import copy
import re
from copy import deepcopy
from functools import wraps
from urllib.parse import urlparse

from flask import Blueprint, Response, abort, current_app, jsonify, redirect, \
render_template, request, session, url_for
from invenio_jsonschemas import current_jsonschemas
from invenio_jsonschemas.errors import JSONSchemaNotFound
from invenio_jsonschemas.proxies import current_refresolver_store

from rero_ils.modules.organisations.api import Organisation
from rero_ils.modules.utils import cached
from rero_ils.permissions import can_access_professional_view

from .menus import init_menu_lang, init_menu_profile, init_menu_tools
from ..modules.organisations.api import Organisation
from ..permissions import can_access_professional_view

blueprint = Blueprint(
'rero_ils',
Expand Down Expand Up @@ -210,6 +213,58 @@ def schemaform(document_type):
return jsonify(data)


def replace_ref_url(schema, new_host):
"""Replace all $refs with local $refs.
:param: schema: Schema to replace the $refs
:param: new_host: The host to replace the $ref with.
:returns: modified schema.
"""
jsonschema_host = current_app.config.get('JSONSCHEMAS_HOST')
for k, v in schema.items():
if isinstance(v, dict):
schema[k] = replace_ref_url(
schema=schema[k],
new_host=new_host
)
if '$ref' in schema and isinstance(schema['$ref'], str):
schema['$ref'] = schema['$ref'] \
.replace(jsonschema_host, new_host)
# Todo: local://
return schema


@blueprint.route('/schemas/<path>/<schema>')
def get_schema(path, schema):
"""Retrieve a schema."""
schema_path = f'{path}/{schema}'
try:
schema = deepcopy(current_refresolver_store[f'local://{schema_path}'])
except KeyError:
abort(404)
resolved = request.args.get(
"resolved", current_app.config.get("JSONSCHEMAS_RESOLVE_SCHEMA"),
type=int
)
new_host = urlparse(request.base_url).netloc
# Change schema['properties']['$schema']['default'] URL
if default := schema.get(
'properties', {}).get('$schema', {}).get('default'):
schema['properties']['$schema']['default'] = default.replace(
current_app.config.get('JSONSCHEMAS_HOST'), new_host)
# Change $refs
schema = replace_ref_url(
schema=schema,
new_host=new_host
)
if resolved:
if current_app.debug:
current_jsonschemas.get_schema.cache_clear()
schema = deepcopy(
current_jsonschemas.get_schema(schema_path, with_refs=True))
return jsonify(schema)


@blueprint.route('/professional/', defaults={'path': ''})
@blueprint.route('/professional/<path:path>')
@can_access_professional_view
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/translations/ar/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -5782,7 +5782,7 @@ msgstr "الغة المستخدمة في البيانات الوصفية"

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:145
#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:150
msgid " Description conventions"
msgid "Description conventions"
msgstr ""

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:146
Expand Down Expand Up @@ -9436,7 +9436,7 @@ msgstr "مقالة"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:275
#, fuzzy
msgid " articles/volumes"
msgid "articles/volumes"
msgstr "مقالة"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:312
Expand Down
8 changes: 4 additions & 4 deletions rero_ils/translations/de/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -5787,8 +5787,8 @@ msgstr "Verwendete Sprache für die Metadaten"

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:145
#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:150
msgid " Description conventions"
msgstr " Beschreibungskonventionen"
msgid "Description conventions"
msgstr "Beschreibungskonventionen"

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:146
msgid "Rules used for the descriptive content of the resource description"
Expand Down Expand Up @@ -9430,8 +9430,8 @@ msgid "article/volume"
msgstr "Artikel/Band"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:275
msgid " articles/volumes"
msgstr " Artikel/Bände"
msgid "articles/volumes"
msgstr "Artikel/Bände"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:312
#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:314
Expand Down
8 changes: 4 additions & 4 deletions rero_ils/translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -5783,8 +5783,8 @@ msgstr "Language used for the metadata"

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:145
#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:150
msgid " Description conventions"
msgstr " Description conventions"
msgid "Description conventions"
msgstr "Description conventions"

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:146
msgid "Rules used for the descriptive content of the resource description"
Expand Down Expand Up @@ -9399,8 +9399,8 @@ msgid "article/volume"
msgstr "article/volume"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:275
msgid " articles/volumes"
msgstr " articles/volumes"
msgid "articles/volumes"
msgstr "articles/volumes"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:312
#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:314
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/translations/eo/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -5764,7 +5764,7 @@ msgstr ""

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:145
#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:150
msgid " Description conventions"
msgid "Description conventions"
msgstr ""

#: rero_ils/modules/documents/jsonschemas/documents/document_admin_metadata-v0.0.1.json:146
Expand Down Expand Up @@ -9281,7 +9281,7 @@ msgid "article/volume"
msgstr "Ekzemple: volumo"

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:275
msgid " articles/volumes"
msgid "articles/volumes"
msgstr ""

#: rero_ils/modules/documents/templates/rero_ils/detailed_view_documents.html:312
Expand Down

0 comments on commit 55d4ee6

Please sign in to comment.