Commit ff3c5fa8 authored by Pierre Dittgen's avatar Pierre Dittgen

refactor code

parent 1999b7bf
......@@ -108,15 +108,15 @@ class SchemaInstance:
self.schema = tableschema_from_url(self.url)
except json.JSONDecodeError as e:
log.exception(e)
flash_error("Format de schéma non reconnu")
flash_error("Le format du schéma n'est pas reconnu")
abort(redirect(url_for('home')))
except datapackage.exceptions.ValidationError as e:
log.exception(e)
flash_error("Le schéma {} n'est pas valide".format(self.url))
flash_error("Le schéma {} comporte des erreurs".format(self.url))
abort(redirect(url_for('home')))
except Exception as e:
log.exception(e)
flash_error("Erreur lors de l'obtention du schéma")
flash_error("Impossible de récupérer le schéma")
abort(redirect(url_for('home')))
def request_parameters(self):
......@@ -602,43 +602,64 @@ def home():
"""Yield sections of the home page, filled with schema metadata."""
if not config.HOMEPAGE_CONFIG:
return
# Iterate on all sections
for section in config.HOMEPAGE_CONFIG['sections']:
home_section = {k: v for k, v in section.items() if k != 'catalog'}
if "catalog" in section:
# section with only links to external validators
if "links" in section:
yield section
# section with catalog
if not "catalog" in section:
# skip section
continue
# error while getting schema catatalog
try:
schema_catalog = get_schema_catalog(section['name'])
except Exception as exc:
log.exception(exc)
err_msg = "une erreur s'est produite"
if isinstance(exc, requests.ConnectionError):
err_msg = "problème de connexion"
elif isinstance(exc, json.decoder.JSONDecodeError):
err_msg = "format JSON incorrect"
elif isinstance(exc, jsonschema.exceptions.ValidationError):
err_msg = "le catalogue ne respecte pas le schéma de référence"
yield {
**{k: v for k,v in section.items() if k != 'catalog'},
"err": err_msg
}
# Working on catalog
schema_info_list = []
for schema_reference in schema_catalog.references:
# Loads default table schema for each schema reference
schema_info = {
'name': schema_reference.name
}
try:
schema_catalog = get_schema_catalog(section['name'])
except Exception as exc:
log.exception(exc)
err_msg = "une erreur s'est produite"
if isinstance(exc, requests.ConnectionError):
err_msg = "problème de connexion"
elif isinstance(exc, json.decoder.JSONDecodeError):
err_msg = "format JSON incorrect"
elif isinstance(exc, jsonschema.exceptions.ValidationError):
err_msg = "le catalogue ne respecte pas le schéma de référence"
home_section['err'] = err_msg
table_schema = tableschema_from_url(schema_reference.get_schema_url())
except json.JSONDecodeError:
schema_info['err'] = True
schema_info['title'] = f"le format du schéma « {schema_info['name']} » n'est pas reconnu"
except datapackage.exceptions.ValidationError:
schema_info['err'] = True
schema_info['title'] = f"le schéma « {schema_info['name']} » comporte des erreurs"
except Exception as e:
schema_info['err'] = True
schema_info['title'] = f"le schéma « {schema_info['name']} » n'est pas disponible"
else:
home_section_catalog = []
for schema_reference in schema_catalog.references:
# Loads default table schema for each schema reference
schema_info = {
'name': schema_reference.name
}
try:
table_schema = tableschema_from_url(schema_reference.get_schema_url())
except:
schema_info['err'] = True
schema_info['title'] = 'Schéma "{}" non disponible'.format(schema_reference.name)
else:
schema_info['title'] = table_schema.descriptor.get("title") or schema_reference.name
home_section_catalog.append(schema_info)
home_section['catalog'] = sorted(
home_section_catalog, key=lambda sc: strip_accents(sc['title'].lower()))
if "links" in section:
home_section["links"] = section["links"]
yield home_section
schema_info['title'] = table_schema.descriptor.get("title") or schema_info['name']
schema_info_list.append(schema_info)
schema_info_list = sorted(
schema_info_list, key=lambda sc: strip_accents(sc['title'].lower()))
yield {
**{k: v for k, v in section.items() if k != 'catalog'},
"catalog": schema_info_list,
}
return render_template('home.html', sections=list(iter_sections()))
......
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