From d658f175de2d09561ebab8fae29bb9f0bb521799 Mon Sep 17 00:00:00 2001 From: Pedro Soares Date: Mon, 8 Sep 2025 15:55:31 +0200 Subject: [PATCH] feat-PB-1837: create layersTable endpoint --- chsdi/__init__.py | 1 + chsdi/templates/layers_table.mako | 53 ++++++++++++++++++++++++ chsdi/views/layers.py | 20 +++++++++ tests/integration/test_layers_service.py | 34 +++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 chsdi/templates/layers_table.mako diff --git a/chsdi/__init__.py b/chsdi/__init__.py index e317a3b6aa..4d184fd31e 100644 --- a/chsdi/__init__.py +++ b/chsdi/__init__.py @@ -91,6 +91,7 @@ def datetime_adapter(obj, request): config.add_route('topics', '/rest/services', request_method=request_method) config.add_route('mapservice', '/rest/services/{map}/MapServer', request_method=request_method) config.add_route('layersConfig', '/rest/services/{map}/MapServer/layersConfig', request_method=request_method) + config.add_route('layersTable', '/rest/services/{map}/MapServer/layersTable', request_method=request_method) config.add_route('catalog', '/rest/services/{map}/CatalogServer', request_method=request_method) config.add_route('identify', '/rest/services/{map}/MapServer/identify', request_method=request_method) config.add_route('find', '/rest/services/{map}/MapServer/find', request_method=request_method) diff --git a/chsdi/templates/layers_table.mako b/chsdi/templates/layers_table.mako new file mode 100644 index 0000000000..4b22b0a64d --- /dev/null +++ b/chsdi/templates/layers_table.mako @@ -0,0 +1,53 @@ + + + + Layers Table + + + +

Layers Configuration Table

+ + + + + + + + + + + + % for layer_id, layer_config in layers.items(): + + + + + + + + % endfor + +
Technical NameOfficial NameLayer TypeHas TooltipIs Searchable
${layer_config.get('serverLayerName', '-')}${layer_config.get('label', '-')}${layer_config.get('type', '-')}${'Yes' if layer_config.get('tooltip', False) else 'No'}${'Yes' if layer_config.get('searchable', False) else 'No'}
+ + diff --git a/chsdi/views/layers.py b/chsdi/views/layers.py index c58d02ead8..17856762dc 100644 --- a/chsdi/views/layers.py +++ b/chsdi/views/layers.py @@ -52,6 +52,26 @@ def layers_config(request): return layers +@view_config(route_name='layersTable', renderer='jsonp') +def layers_table(request): + params = BaseLayersValidation(request) + query = params.request.db.query(LayersConfig) + layers = {} + for layer in get_layers_config_for_params(params, query, LayersConfig): + layers = dict(chain(layers.items(), layer.items())) + + response = render_to_response( + 'chsdi:templates/layers_table.mako', + {'layers': layers}, + request=request + ) + + if params.cbName is None: + return response + + return response.body.decode('utf8') + + @view_config(route_name='legend', renderer='jsonp') def legend(request): params = BaseLayersValidation(request) diff --git a/tests/integration/test_layers_service.py b/tests/integration/test_layers_service.py index 3a30831526..c4d3ae7ab5 100644 --- a/tests/integration/test_layers_service.py +++ b/tests/integration/test_layers_service.py @@ -220,3 +220,37 @@ def test_layer_attributes_datetime(self): self.assertEqual(resp.content_type, 'application/json') resp = self.testapp.get('/rest/services/api/MapServer/ch.bfs.gebaeude_wohnungs_register', params={'callback': 'cb_'}, status=200) self.assertEqual(resp.content_type, 'application/javascript') + + def test_layerstable_valid(self): + resp = self.testapp.get('/rest/services/ech/MapServer/layersTable', status=200) + self.assertEqual(resp.content_type, 'text/html') + resp.mustcontain('Technical Name') + resp.mustcontain('Official Name') + resp.mustcontain('Layer Type') + resp.mustcontain('Has Tooltip') + resp.mustcontain('Is Searchable') + + def test_layerstable_valid_with_callback(self): + resp = self.testapp.get('/rest/services/ech/MapServer/layersTable', params={'callback': 'cb_'}, status=200) + self.assertEqual(resp.content_type, 'application/javascript') + resp.mustcontain('cb_(') + + def test_layerstable_content_check(self): + import re + resp = self.testapp.get('/rest/services/ech/MapServer/layersTable', status=200) + self.assertEqual(resp.content_type, 'text/html') + html_content = resp.text + + # Search for the row containing ch.swisstopo.pixelkarte-farbe + layer_row_pattern = r'.*?ch\.swisstopo\.pixelkarte-farbe(.*?)' + row_match = re.search(layer_row_pattern, html_content, re.DOTALL) + + self.assertIsNotNone(row_match, "Could not find row for ch.swisstopo.pixelkarte-farbe") + row_content = row_match.group(0) + + # Check that all required values are present in the row + self.assertIn('ch.swisstopo.pixelkarte-farbe', row_content) + self.assertIn('National Maps (color)', row_content) + self.assertIn('wmts', row_content) + self.assertTrue(row_content.count('No') >= 2, + "Expected at least 2 'No' values for tooltip and searchable attributes")