Commit 848d5bae authored by Christophe Benz's avatar Christophe Benz

Fix homepage sections generation, use schema_reference name

parent 34204acc
......@@ -43,15 +43,15 @@ class SchemaCatalogRegistry:
self.ref_map[name] = ref
def build_schema_catalog(self, name):
if name in self.ref_map:
ref = self.ref_map[name]
ref = self.ref_map.get(name)
if not ref:
return None
try:
catalog = opendataschema.SchemaCatalog(ref, session=self.session)
except requests.exceptions.RequestException as exc:
log.exception(exc)
return None
return catalog
return None
caching_session = cachecontrol.CacheControl(requests.Session())
......
......@@ -4,17 +4,17 @@
{% block content %}
<div class="container">
{% for section in config.sections %}
{% for section in sections %}
{% if section.catalog %}
<div class="mb-5">
<h2 class="my-4">{{section.title}}</h2>
<h2 class="my-4">{{ section.title }}</h2>
<form action="{{ url_for('custom_validator') }}" method="GET">
<div class="form-row">
<div class="form-group col-lg-9">
<select required name="schema_name" class="form-control">
<option disabled selected value="">Choisissez un schéma</option>
{% for item in section.catalog | sort(attribute='title') %}
<option value="{{ item.name }}">{{ item.title }}</option>
<option value="{{ section.name }}.{{ item.name }}">{{ item.title }}</option>
{% endfor %}
</select>
</div>
......@@ -53,7 +53,7 @@
</form>
</div>
{% for section in config.sections %}
{% for section in sections %}
{% if section.links %}
<div class="my-5">
<h2 class="my-4">{{ section.title }}</h2>
......
......@@ -425,37 +425,36 @@ def bytes_data(f):
return iob.getvalue()
def homepage_config_with_schema_metadata(ui_config):
"""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_name = section['name']
schema_catalog = get_schema_catalog(section_name)
if schema_catalog is None:
section['catalog'] = []
continue
schema_list = []
for ref in schema_catalog.references:
# Loads default table schema for each schema reference
table_schema = tableschema_from_url(ref.get_schema_url())
schema_list.append({
'name': '{}.{}'.format(section_name, ref.name),
# Extracts title, description, ...
**extract_schema_metadata(table_schema)
})
section['catalog'] = schema_list
return extended_ui_config
# Routes
@app.route('/')
def home():
""" Home page """
home_config = homepage_config_with_schema_metadata(config.HOMEPAGE_CONFIG)
return render_template('home.html', config=home_config)
def iter_sections():
"""Yield sections of the home page, filled with schema metadata."""
if not config.HOMEPAGE_CONFIG:
return
for section in config.HOMEPAGE_CONFIG['sections']:
home_section = {"name": section['name'], "title": section["title"]}
if "catalog" in section:
schema_catalog = get_schema_catalog(section['name'])
if schema_catalog:
home_section_catalog = []
for schema_reference in schema_catalog.references:
# Loads default table schema for each schema reference
table_schema = tableschema_from_url(schema_reference.get_schema_url())
home_section_catalog.append({
"name": schema_reference.name,
"title": table_schema.descriptor.get("title") or schema_reference.name
})
home_section['catalog'] = home_section_catalog
if "links" in section:
home_section["links"] = section["links"]
yield home_section
return render_template('home.html', sections=list(iter_sections()))
@app.route('/pdf')
......
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