Skip to content

Commit

Permalink
Include datasource filter set in QGIS project in data-service configu…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
manisandro committed Jan 13, 2025
1 parent d8f6ec9 commit 784c24c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/config_generator/data_service_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def _datasets(self, config, session):
dataset['name'] = qgs_name + '.' + map_dataset
dataset['db_url'] = meta.get('database')
dataset['schema'] = meta.get('schema')
dataset['datasource_filter'] = meta.get('datasource_filter')
dataset['table_name'] = meta.get('table_name')
dataset['primary_key'] = meta.get('primary_key')

Expand Down Expand Up @@ -174,6 +175,7 @@ def _datasets(self, config, session):
dataset = OrderedDict()
dataset['name'] = key
dataset['db_url'] = value.get('database')
dataset['datasource_filter'] = value.get('datasource_filter')
dataset['schema'] = value.get('schema')
dataset['table_name'] = value.get('table_name')
dataset['primary_key'] = value.get('primary_key')
Expand Down
21 changes: 17 additions & 4 deletions src/config_generator/qgs_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
import html
import io
import math
import os
Expand Down Expand Up @@ -181,7 +182,9 @@ def layer_metadata(self, layer_name):
continue

datasource = maplayer.find('datasource').text
config['database'] = self.__db_connection(datasource)
database, datasource_filter = self.__db_connection(datasource)
config['database'] = database
config['datasource_filter'] = datasource_filter
config.update(self.__table_metadata(datasource, maplayer))

self.__lookup_attribute_data_types(config)
Expand Down Expand Up @@ -250,6 +253,7 @@ def __db_connection(self, datasource):
:param str datasource: QGIS datasource URI
"""
connection_string = None
datasource_filter = None

if 'service=' in datasource:
# PostgreSQL connection service
Expand Down Expand Up @@ -296,7 +300,12 @@ def __db_connection(self, datasource):

connection_string += "%s:%s/%s" % (host, port, dbname)

return connection_string
# sql appears last
m = re.search(r"sql=(.*)$", datasource)
if m is not None:
datasource_filter = html.unescape(m.group(1))

return connection_string, datasource_filter

def __table_metadata(self, datasource, maplayer=None):
"""Parse QGIS datasource URI and return table metadata.
Expand Down Expand Up @@ -373,7 +382,9 @@ def __attributes_metadata(self, maplayer):

if not jointable in jointables:
jointables[jointable] = self.__table_metadata(joinlayer.find('datasource').text, joinlayer)
jointables[jointable]['database'] = self.__db_connection(joinlayer.find('datasource').text)
database, datasource_filter = self.__db_connection(joinlayer.find('datasource').text)
jointables[jointable]['database'] = database
jointables[jointable]['datasource_filter'] = datasource_filter
jointables[jointable]['targetField'] = join.get('targetFieldName')
jointables[jointable]['joinField'] = join.get('joinFieldName')

Expand Down Expand Up @@ -533,7 +544,9 @@ def __edit_widget_constraints(self, maplayer, field, keyvaltables):
keyvaltables[self.map_prefix + "." + layerName] = self.__table_metadata(layerSource)
keyvaltables[self.map_prefix + "." + layerName]['qgs_name'] = self.map_prefix
keyvaltables[self.map_prefix + "." + layerName]['layername'] = layerName
keyvaltables[self.map_prefix + "." + layerName]['database'] = self.__db_connection(layerSource)
database, datasource_filter = self.__db_connection(layerSource)
keyvaltables[self.map_prefix + "." + layerName]['database'] = database
keyvaltables[self.map_prefix + "." + layerName]['datasource_filter'] = datasource_filter
keyvaltables[self.map_prefix + "." + layerName]['fields'] = {
key: {},
value: {}
Expand Down

0 comments on commit 784c24c

Please sign in to comment.