diff --git a/validata_ui/__init__.py b/validata_ui/__init__.py index 073ff058ec6c0d8a8b80e5a029fb40c32e69c63a..f388d04339863d466721d891dcb7c553250c6821 100644 --- a/validata_ui/__init__.py +++ b/validata_ui/__init__.py @@ -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] - try: - catalog = opendataschema.SchemaCatalog(ref, session=self.session) - except requests.exceptions.RequestException as exc: - log.exception(exc) - return None - return catalog - return None + 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 caching_session = cachecontrol.CacheControl(requests.Session()) diff --git a/validata_ui/templates/home.html b/validata_ui/templates/home.html index 2f0c52c3c6ed3d61e9fdda20c9f5eeee2202a39f..ff805d8f1ecf310a705e1cf853c6ebfb62f11cef 100644 --- a/validata_ui/templates/home.html +++ b/validata_ui/templates/home.html @@ -4,17 +4,17 @@ {% block content %}
-{% for section in config.sections %} +{% for section in sections %} {% if section.catalog %}
-

{{section.title}}

+

{{ section.title }}

@@ -53,7 +53,7 @@
-{% for section in config.sections %} +{% for section in sections %} {% if section.links %}

{{ section.title }}

diff --git a/validata_ui/views.py b/validata_ui/views.py index d396d4fd71f09db914e59c50d9a4c2834f207a55..bebefe7d1628c66477517c116f5833002ebdf71b 100644 --- a/validata_ui/views.py +++ b/validata_ui/views.py @@ -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')