Skip to content

Commit c1af5e6

Browse files
committed
Don't add html_content if not custom HTML template is defined
1 parent f0e2950 commit c1af5e6

File tree

3 files changed

+40
-122
lines changed

3 files changed

+40
-122
lines changed

src/feature_info_service.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from info_modules.sql import layer_info as sql_layer_info
2222
from info_modules.wms import layer_info as wms_layer_info
23-
from info_templates import default_info_template, layer_template
23+
from info_templates import layer_template
2424
from utils import geom_center
2525

2626

@@ -87,6 +87,7 @@ def __init__(self, tenant, logger):
8787
config_handler = RuntimeConfig("featureInfo", logger)
8888
config = config_handler.tenant_config(tenant)
8989

90+
self.default_info_template = None
9091
self.default_info_template_dir = None
9192
if config.get('default_info_template'):
9293
self.default_info_template = config.get('default_info_template')
@@ -105,8 +106,6 @@ def __init__(self, tenant, logger):
105106
config.get('default_info_template_base64'),
106107
default_info_template, "default info template"
107108
)
108-
else:
109-
self.default_info_template = default_info_template
110109

111110
self.default_wms_url = config.get(
112111
'default_qgis_server_url', 'http://localhost:8001/ows/')
@@ -504,33 +503,34 @@ def get_layer_info(self, identity, origin, service_name, layer, style, x, y, crs
504503
geometry = feature.get('geometry')
505504

506505
info_html = None
507-
try:
508-
# render feature template
509-
templateLoader = None
510-
if template_dir:
511-
templateLoader = jinja2.FileSystemLoader(searchpath=template_dir)
512-
templateEnv = jinja2.Environment(loader=templateLoader, autoescape=True)
513-
feature_template = templateEnv.from_string(template)
514-
info_html = feature_template.render(
515-
feature=info_feature, fid=fid, bbox=bbox,
516-
geometry=geometry, layer=layer, x=x, y=y, crs=crs,
517-
render_value=self.render_value,
518-
locale=locale
519-
)
520-
except jinja2.TemplateSyntaxError as e:
521-
error_msg = (
522-
"TemplateSyntaxError on line %d: %s" % (e.lineno, e)
523-
)
524-
except jinja2.TemplateError as e:
525-
error_msg = "TemplateError: %s" % e
526-
except Exception as e:
527-
error_msg = "TemplateError: %s" % e
528-
if error_msg is not None:
529-
self.logger.error(error_msg)
530-
info_html = (
531-
'<span class="info_error" style="color: red">%s</span>' %
532-
error_msg
533-
)
506+
if template:
507+
try:
508+
# render feature template
509+
templateLoader = None
510+
if template_dir:
511+
templateLoader = jinja2.FileSystemLoader(searchpath=template_dir)
512+
templateEnv = jinja2.Environment(loader=templateLoader, autoescape=True)
513+
feature_template = templateEnv.from_string(template)
514+
info_html = feature_template.render(
515+
feature=info_feature, fid=fid, bbox=bbox,
516+
geometry=geometry, layer=layer, x=x, y=y, crs=crs,
517+
render_value=self.render_value,
518+
locale=locale
519+
)
520+
except jinja2.TemplateSyntaxError as e:
521+
error_msg = (
522+
"TemplateSyntaxError on line %d: %s" % (e.lineno, e)
523+
)
524+
except jinja2.TemplateError as e:
525+
error_msg = "TemplateError: %s" % e
526+
except Exception as e:
527+
error_msg = "TemplateError: %s" % e
528+
if error_msg is not None:
529+
self.logger.error(error_msg)
530+
info_html = (
531+
'<span class="info_error" style="color: red">%s</span>' %
532+
error_msg
533+
)
534534

535535
if geomcentroid and geometry:
536536
gj = wkt.loads(geometry.upper().replace('Z',''))
@@ -542,7 +542,7 @@ def get_layer_info(self, identity, origin, service_name, layer, style, x, y, crs
542542

543543
features.append({
544544
'fid': fid,
545-
'html_content': self.html_content(info_html) if with_htmlcontent else "",
545+
'html_content': self.html_content(info_html) if with_htmlcontent and info_html else "",
546546
'plain_html': info_html,
547547
'bbox': bbox if with_bbox else None,
548548
'wkt_geom': geometry,
@@ -553,13 +553,19 @@ def get_layer_info(self, identity, origin, service_name, layer, style, x, y, crs
553553
output = ""
554554
for feature in features:
555555
output += "Feature: {fid}\n".format(fid=feature['fid'])
556-
output += '\n'.join(map(lambda attr: "{alias}: {value}".format(alias=attr['alias'], value=attr['value']), feature['attributes'])) + '\n\n'
556+
output += '\n'.join(map(lambda attr: f"{attr['alias']}: {attr['value']}", feature['attributes'])) + '\n\n'
557557
return "Layer: {layer_title}\n\n{output}\n\n\n".format(layer_title=layer_title, output=output)
558558
elif output_info_format == 'text/html':
559559
output = ""
560560
for feature in features:
561-
output += "<h2><i>Feature</i>: {fid}</h2>\n{plain_html}\n".format(fid=feature['fid'], plain_html=feature['plain_html'])
562-
return "<h1><i>Layer</i>: {layer_title}</h1>\n{output}\n<br />\n".format(layer_title=layer_title, output=output)
561+
output += f"<h2><i>Feature</i>: {feature['fid']}</h2>\n"
562+
if feature['plain_html']:
563+
output += f"{feature['plain_html']}\n"
564+
else:
565+
output += "<table>\n"
566+
output += '\n'.join(map(lambda attr: f"<tr><td>{attr['alias']}</td><td>{attr['value']}</td></tr>", feature['attributes'])) + '\n'
567+
output += "</table>\n"
568+
return f"<h1><i>Layer</i>: {layer_title}</h1>\n{output}\n<br />\n"
563569
elif output_info_format == 'application/json':
564570
return {
565571
"type": "Feature",

src/info_templates.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -27,89 +27,3 @@
2727
{% endfor -%}
2828
</Layer>
2929
""")
30-
31-
# default info template for feature info HTML
32-
default_info_template = """
33-
<table class="attribute-list">
34-
<tbody>
35-
{% for attr in feature._attributes -%}
36-
{% if attr['type'] == 'list' -%}
37-
{# attribute is a list #}
38-
<tr>
39-
<td class="identify-attr-title wrap"><i>{{ attr['alias']|e }}</i></td>
40-
<td>
41-
<table class="identify-attr-subtable">
42-
<tbody>
43-
{%- for item in attr['value'] %}
44-
{%- if item is mapping -%}
45-
{# item is a dict #}
46-
{% for key in item -%}
47-
{% if not attr['json_aliases'] %}
48-
{% set alias = key %}
49-
{% elif key in attr['json_aliases'] %}
50-
{% set alias = attr['json_aliases'][key] %}
51-
{% endif %}
52-
{% if alias %}
53-
<tr>
54-
<td class="identify-attr-title wrap">
55-
<i>{{ alias|e }}</i>
56-
</td>
57-
<td class="identify-attr-value wrap">
58-
{{ render_value(item[key])|safe }}
59-
</td>
60-
</tr>
61-
{% endif %}
62-
{%- endfor %}
63-
{%- else -%}
64-
<tr>
65-
<td class="identify-attr-value identify-attr-single-value wrap" colspan="2">
66-
{{ render_value(item)|safe }}
67-
</td>
68-
</tr>
69-
{%- endif %}
70-
<tr>
71-
<td class="identify-attr-spacer" colspan="2"></td>
72-
</tr>
73-
{%- endfor %}
74-
</tbody>
75-
</table>
76-
</td>
77-
</tr>
78-
79-
{%- elif attr['type'] in ['dict', 'OrderedDict'] -%}
80-
{# attribute is a dict #}
81-
<tr>
82-
<td class="identify-attr-title wrap"><i>{{ attr['alias']|e }}</i></td>
83-
<td>
84-
<table class="identify-attr-subtable">
85-
<tbody>
86-
{% for key in attr['value'] -%}
87-
<tr>
88-
<td class="identify-attr-title wrap">
89-
<i>{{ key|e }}</i>
90-
</td>
91-
<td class="identify-attr-value wrap">
92-
{{ render_value(attr['value'][key])|safe }}
93-
</td>
94-
</tr>
95-
{%- endfor %}
96-
</tbody>
97-
</table>
98-
</td>
99-
</tr>
100-
101-
{%- else -%}
102-
{# other attributes #}
103-
<tr>
104-
<td class="identify-attr-title wrap">
105-
<i>{{ attr['alias']|e }}</i>
106-
</td>
107-
<td class="identify-attr-value wrap">
108-
{{ render_value(attr['value'])|safe }}
109-
</td>
110-
</tr>
111-
{%- endif %}
112-
{%- endfor %}
113-
</tbody>
114-
</table>
115-
"""

tests/api_tests.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ def test_info_module(self):
4949
features = layers[0].getElementsByTagName("Feature")
5050
self.assertEqual(len(features), 1)
5151
self.assertEqual(features[0].getAttribute("id"), "1")
52-
htmlContents = features[0].getElementsByTagName("HtmlContent")
53-
self.assertEqual(len(htmlContents), 1)

0 commit comments

Comments
 (0)