Skip to content

Commit

Permalink
create display expression for domains with translation considerations…
Browse files Browse the repository at this point in the history
… - this fixes #115 and opengisch/QgisModelBaker#982
  • Loading branch information
signedav committed Nov 7, 2024
1 parent eac8ec9 commit ce6ab45
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
6 changes: 6 additions & 0 deletions modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ def get_available_languages(self, irrelevant_models: list[str]) -> list[str]:
"""
return []

def get_domain_dispnames(self, tablename):
"""
Get the domain display names with consideration of the translation
"""
return []


class DBConnectorError(Exception):
"""This error is raised when DbConnector could not connect to database.
Expand Down
20 changes: 20 additions & 0 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,23 @@ def get_available_languages(self, irrelevant_models=[]):
records = cursor.fetchall()
cursor.close()
return [record["lang"] for record in records]

def get_domain_dispnames(self, tablename):
if not self._table_exists or not self._table_exists(GPKG_NLS_TABLE):
return []

cursor = self.conn.cursor()
cursor.execute(
"""SELECT t.iliCode as code, nls.label as label
FROM "{tablename}" t
LEFT JOIN "{t_ili2db_nls}" nls
ON nls.ilielement = (t.thisClass||'.'||t.iliCode) and lang = '{lang}'
;
""".format(
tablename=tablename, t_ili2db_nls=GPKG_NLS_TABLE, lang=self._lang
)
)
records = cursor.fetchall()
cursor.close()

return records
23 changes: 23 additions & 0 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,3 +1393,26 @@ def get_available_languages(self, irrelevant_models=[]):
)
return [row["lang"] for row in cur.fetchall()]
return []

def get_domain_dispnames(self, tablename):
if self.schema and self._table_exists and self._table_exists(PG_NLS_TABLE):
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute(
sql.SQL(
"""SELECT t.iliCode as code, nls.label as label
FROM {schema}.{tablename} t
LEFT JOIN {schema}.{t_ili2db_nls} nls
ON nls.ilielement = (t.thisClass||'.'||t.iliCode) and lang = %s
;
"""
).format(
schema=sql.Identifier(self.schema),
tablename=sql.Identifier(tablename),
t_ili2db_nls=sql.Identifier(PG_NLS_TABLE),
),
(self._lang,),
)
records = cur.fetchall()

return records
return []
24 changes: 24 additions & 0 deletions modelbaker/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@ def layers(self, filter_layer_list: list = []) -> list[Layer]:
]:
display_expression = attr_record["attr_value"]

# for the domain we create a translated mapping expression in case there is not yet a displayexpression defined
if not display_expression and is_domain:
mapped_dispnames = self.get_domain_dispnames(record["tablename"])
if mapped_dispnames:
case_expression = "CASE\n"
for entry in mapped_dispnames:
if entry["label"]:
case_expression += (
"WHEN iliCode = '{code}' THEN '{label}'\n".format(
code=entry["code"], label=entry["label"]
)
)
else:
case_expression += (
"WHEN iliCode = '{code}' THEN dispName\n".format(
code=entry["code"]
)
)
case_expression += "END"
display_expression = case_expression

coord_decimals = (
record["coord_decimals"] if "coord_decimals" in record else None
)
Expand Down Expand Up @@ -954,3 +975,6 @@ def get_iliname_dbname_mapping(self):

def get_basket_handling(self):
return self._db_connector.get_basket_handling()

def get_domain_dispnames(self, tablename):
return self._db_connector.get_domain_dispnames(tablename)
32 changes: 30 additions & 2 deletions tests/test_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,22 @@ def test_translated_db_objects_gpkg(self):
assert len(tab_list) == len(expected_tab_list)
assert set(tab_list) == set(expected_tab_list)

# check domain table and translated domains
if layer.name == "rechtsstatus":
count += 1
assert layer.alias == "StatutJuridique"
assert layer.layer.displayExpression() == "\n".join(
[
"CASE",
"WHEN iliCode = 'AenderungOhneVorwirkung' THEN 'ModificationSansEffetAnticipe'",
"WHEN iliCode = 'inKraft' THEN 'enVigueur'",
"WHEN iliCode = 'AenderungMitVorwirkung' THEN 'ModificationAvecEffetAnticipe'",
"END",
]
)

# check if the layers have been considered
assert count == 1
assert count == 2
assert fr_layer

# Check translated relation
Expand Down Expand Up @@ -189,8 +203,22 @@ def test_translated_db_objects_pg(self):
assert len(tab_list) == len(expected_tab_list)
assert set(tab_list) == set(expected_tab_list)

# check domain table and translated domains
if layer.name == "rechtsstatus":
count += 1
assert layer.alias == "StatutJuridique"
assert layer.layer.displayExpression() == "\n".join(
[
"CASE",
"WHEN iliCode = 'AenderungOhneVorwirkung' THEN 'ModificationSansEffetAnticipe'",
"WHEN iliCode = 'AenderungMitVorwirkung' THEN 'ModificationAvecEffetAnticipe'",
"WHEN iliCode = 'inKraft' THEN 'enVigueur'",
"END",
]
)

# check if the layers have been considered
assert count == 1
assert count == 2
assert fr_layer

# Check translated relation
Expand Down

0 comments on commit ce6ab45

Please sign in to comment.