Skip to content

Commit 063100e

Browse files
committed
Also store aliases for WFS attributes in resources, clean wfs layer/attribute names
1 parent a595174 commit 063100e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/config_generator/capabilities_reader.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ def read_wfs_service_capabilities(self, service_name, item):
637637
wfs_layer = OrderedDict()
638638

639639
layer_name = layer.find('%sName' % np, ns).text
640+
# NOTE: ensure special characters in layer names are consistently replaced (varies across QGIS WFS versions)
641+
layer_name = layer_name.replace(' ', '_').replace(':', '-')
642+
640643
wfs_layer['name'] = layer_name
641644
wfs_layer['title'] = layer.find('%sTitle' % np, ns).text
642645
wfs_layer['attributes'] = wfs_layers_attributes.get(layer_name, [])
@@ -720,15 +723,19 @@ def collect_wfs_layers_attributes(self, full_url):
720723
np = ''
721724

722725
layers_attributes = {}
726+
replace_unicode_pat = re.compile(r'[^\w.\-_]', flags=re.UNICODE)
723727

724728
for complex_type in root.findall('%scomplexType' % np, ns):
725729
# extract layer name from complexType by removing "Type" suffix
726730
# e.g. "edit_pointsType" -> "edit_points"
727731
layer_name = complex_type.get('name').removesuffix('Type')
728732

729-
attributes = []
733+
attributes = {}
730734
for element in complex_type.findall('%scomplexContent/%sextension/%ssequence/%selement' % (np, np, np, np), ns):
731-
attributes.append(element.get('name'))
735+
attribute_name = element.get('name')
736+
# NOTE: ensure special characters in attribute names are consistently replaced (varies across QGIS WFS versions)
737+
attribute_name = replace_unicode_pat.sub('', attribute_name.replace(' ', '_'))
738+
attributes[attribute_name] = element.get('alias', attribute_name)
732739

733740
layers_attributes[layer_name] = attributes
734741

src/config_generator/ogc_service_config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import OrderedDict
2+
import re
23

34
from .permissions_query import PermissionsQuery
45
from .service_config import ServiceConfig
@@ -656,6 +657,24 @@ def wfs_permissions(self, role, session):
656657
'attributes': non_public_resources('attribute', session)
657658
}
658659

660+
# NOTE: replace special characters in layer/attribute names
661+
# (WFS capabilities/etc report cleaned names)
662+
replace_unicode_pat = re.compile(r'[^\w.\-_]', flags=re.UNICODE)
663+
clean_layer_name = lambda layer_name: layer_name.replace(' ', '_').replace(':', '-')
664+
clean_attr_name = lambda attr_name: replace_unicode_pat.sub('', attr_name.replace(' ', '_'))
665+
666+
for perm in (role_permissions, public_permissions, public_restrictions):
667+
for service_name in perm['layers']:
668+
perm['layers'][service_name] = dict([
669+
(clean_layer_name(layer_name), {}) for layer_name in perm['layers'][service_name]
670+
])
671+
for service_name in perm['attributes']:
672+
perm['attributes'][service_name] = dict([
673+
(clean_layer_name(layer_name_attrs[0]), dict([
674+
(clean_attr_name(attr_name), {}) for attr_name in layer_name_attrs[1]
675+
])) for layer_name_attrs in perm['attributes'][service_name].items()
676+
])
677+
659678
is_public_role = (role == self.permissions_query.public_role())
660679

661680
for service_name in self.themes_reader.wfs_service_names():

0 commit comments

Comments
 (0)