Commit f66d39d8 authored by Pierre Dittgen's avatar Pierre Dittgen

Better catalog urls handling

parent dc9d75d0
......@@ -25,13 +25,16 @@ def schema_from_url(url):
return tableschema.Schema(url)
# load config.json and catalog_schema_toml
# load config.json
ui_config = json.load(config.UI_CONFIG_FILE.open('rt', encoding='utf-8')) if config.UI_CONFIG_FILE else []
# super ugly way to access catalog_toml url
# TODO: improve it
schema_catalog_url = ui_config['sections'][0]['catalog']
table_schema_catalog = opendataschema.SchemaCatalog(schema_catalog_url, download_func=download_with_cache)
# And load schema catalogs which urls are found in config.json
schema_catalog_map = {}
for section in ui_config['sections']:
if isinstance(section['catalog'], str) and section['catalog'].startswith('http'):
code = section['code']
url = section['catalog']
schema_catalog_map[code] = opendataschema.SchemaCatalog(url, download_func=download_with_cache)
# Flask things
app = flask.Flask(__name__)
......
......@@ -22,7 +22,7 @@ from validata_core import compute_badge, messages
import tabulator
from . import app, config, ui_config, table_schema_catalog, schema_from_url
from . import app, config, ui_config, schema_catalog_map, schema_from_url
from .ui_util import flash_error, flash_warning
from .validata_util import ValidataResource, URLValidataResource, UploadedFileValidataResource
......@@ -44,7 +44,7 @@ class SchemaInstance():
self.versions = versions
@staticmethod
def from_parameters(parameter_dict, table_schema_catalog):
def from_parameters(parameter_dict, schema_catalog_map):
"""Initializes schema instance from requests dict and tableschema catalog (for name ref)
"""
schema_url, schema_name, schema_ref, versions = None, None, None, None
......@@ -58,8 +58,20 @@ class SchemaInstance():
schema_name = parameter_dict['schema_name']
schema_ref = parameter_dict.get('schema_ref')
# Check schema name
chunks = schema_name.split('.')
if len(chunks) != 2:
return None
section_code, ref_name = chunks
# Look for schema catalog first
table_schema_catalog = schema_catalog_map.get(section_code)
if table_schema_catalog is None:
return None
# Unknown schema name?
table_schema_reference = table_schema_catalog.reference_by_name.get(schema_name)
table_schema_reference = table_schema_catalog.reference_by_name.get(ref_name)
if table_schema_reference is None:
return None
......@@ -379,23 +391,27 @@ def bytes_data(f):
return iob.getvalue()
def hydrate_ui_config(ui_config, table_schema_catalog):
hydrated_ui_config = ui_config.copy()
table_schema_ref_list = []
for ref in table_schema_catalog.references:
table_schema = schema_from_url(ref.get_schema_url())
info = {
"name": ref.name,
**{k: v for k, v in table_schema.descriptor.items() if k != 'fields'}
}
table_schema_ref_list.append(info)
# TODO: change this hard-coded affectation
hydrated_ui_config['sections'][0]['catalog'] = table_schema_ref_list
return hydrated_ui_config
def ui_config_with_schema_metadata(ui_config, schema_catalog_map):
"""Replace catalog url within ui_config by schema references
containing schema metadata properties"""
extended_ui_config = ui_config.copy()
for section in extended_ui_config['sections']:
section_code = section['code']
if section_code not in schema_catalog_map:
continue
schema_catalog = schema_catalog_map[section_code]
schema_list = []
for ref in schema_catalog.references:
# Loads default table schema for each schema reference
table_schema = schema_from_url(ref.get_schema_url())
schema_list.append({
'name': '{}.{}'.format(section_code, ref.name),
# Extracts title, description, ...
**extract_schema_metadata(table_schema)
})
section['catalog'] = schema_list
return extended_ui_config
# Routes
......@@ -404,7 +420,8 @@ def hydrate_ui_config(ui_config, table_schema_catalog):
def home():
""" Home page """
flash_warning('Ce service est fourni en mode beta - certains problèmes peuvent subsister - nous mettons tout en œuvre pour améliorer son fonctionnement en continu.')
return render_template('home.html', title='Accueil', config=hydrate_ui_config(ui_config, table_schema_catalog))
home_config = ui_config_with_schema_metadata(ui_config, schema_catalog_map)
return render_template('home.html', title='Accueil', config=home_config)
@app.route('/pdf')
......@@ -417,7 +434,7 @@ def pdf_report():
flash_error(err_prefix + ': URL non fournie')
return redirect(url_for('home'))
schema_instance = SchemaInstance.from_parameters(request.args, table_schema_catalog)
schema_instance = SchemaInstance.from_parameters(request.args, schema_catalog_map)
if schema_instance is None:
flash_error(err_prefix + ': Information de schema non fournie')
return redirect(url_for('home'))
......@@ -459,12 +476,22 @@ def pdf_report():
return response
def extract_schema_metadata(table_schema: tableschema.Schema):
"""Gets author, contibutor, version...metadata from schema header"""
return {k: v for k, v in table_schema.descriptor.items() if k != 'fields'}
def compute_schema_info(table_schema: tableschema.Schema, schema_url):
"""Factor code for validator form page"""
schema_info = {k: v for k, v in table_schema.descriptor.items() if k != 'fields'}
schema_info['url'] = schema_url
title = "Schéma « {} »".format(schema_info.get('title'))
# Schema URL + schema metadata info
schema_info = {
'url': schema_url,
**extract_schema_metadata(table_schema)
}
meta_title = schema_info.get('title')
title = "Schéma « {} »".format(meta_title if meta_title else '...')
return schema_info, title
......@@ -494,7 +521,7 @@ def custom_validator():
# url of resource to be validated
url_param = request.args.get("url")
schema_instance = SchemaInstance.from_parameters(request.args, table_schema_catalog)
schema_instance = SchemaInstance.from_parameters(request.args, schema_catalog_map)
if schema_instance is None:
flash_error("Aucun schéma passé en paramètre")
return redirect(url_for('home'))
......@@ -528,7 +555,7 @@ def custom_validator():
else: # POST
schema_instance = SchemaInstance.from_parameters(request.form, table_schema_catalog)
schema_instance = SchemaInstance.from_parameters(request.form, schema_catalog_map)
if schema_instance is None:
flash_error('Aucun schéma défini')
return redirect(url_for('home'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment