From ec61ff0b9d49d176db86da815df6fc9fefbe7378 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:41:15 +0200 Subject: [PATCH] Fix explode_expression (#921) (#922) * Fix regex expression. * Update test. * Optional blank spaces --------- Co-authored-by: wlorenzetti (cherry picked from commit 2efd07d952cfeddf4c4a57ac201cde40bb2a6418) Co-authored-by: Walter Lorenzetti --- g3w-admin/qdjango/tests/test_utils.py | 54 +++++++++++++++++++++++++++ g3w-admin/qdjango/utils/qgis.py | 3 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/g3w-admin/qdjango/tests/test_utils.py b/g3w-admin/qdjango/tests/test_utils.py index c9381232d..d8972a3dd 100644 --- a/g3w-admin/qdjango/tests/test_utils.py +++ b/g3w-admin/qdjango/tests/test_utils.py @@ -696,6 +696,60 @@ def test_explode_expression(self): self.assertTrue('referencing_fields' in ee) self.assertEqual(ee['referencing_fields'].sort(), expected['referencing_fields'].sort()) + # Test current_value with spaces before '(' after 'current_value' + expr = "sqrt( $area ) *2 + \"gid\" + current_value ( \'doublespace\' )" + + expected = { + 'expression': expr, + 'referenced_columns': ['gid', 'doublespace'], + 'referenced_functions': ['sqrt', '$area', 'current_value'], + 'referencing_fields': ['doublespace'] + } + + ee = explode_expression(expr) + + self.assertEqual(ee['expression'], expected['expression']) + self.assertEqual(ee['referenced_columns'].sort(), expected['referenced_columns'].sort()) + self.assertEqual(ee['referenced_functions'].sort(), expected['referenced_functions'].sort()) + self.assertTrue('referencing_fields' in ee) + self.assertEqual(ee['referencing_fields'].sort(), expected['referencing_fields'].sort()) + + # Test current_value with spaces before '(' after 'current_value' + expr = "sqrt( $area ) *2 + \"gid\" + current_value ( \'doublespace\' )" + + expected = { + 'expression': expr, + 'referenced_columns': ['gid', 'doublespace'], + 'referenced_functions': ['sqrt', '$area', 'current_value'], + 'referencing_fields': ['doublespace'] + } + + ee = explode_expression(expr) + + self.assertEqual(ee['expression'], expected['expression']) + self.assertEqual(ee['referenced_columns'].sort(), expected['referenced_columns'].sort()) + self.assertEqual(ee['referenced_functions'].sort(), expected['referenced_functions'].sort()) + self.assertTrue('referencing_fields' in ee) + self.assertEqual(ee['referencing_fields'].sort(), expected['referencing_fields'].sort()) + + # Test current_value + expr = "sqrt( $area ) *2 + \"gid\" + current_value ( 'doublespace' )" + + expected = { + 'expression': expr, + 'referenced_columns': ['gid', 'doublespace'], + 'referenced_functions': ['sqrt', '$area', 'current_value'], + 'referencing_fields': ['doublespace'] + } + + ee = explode_expression(expr) + + self.assertEqual(ee['expression'], expected['expression']) + self.assertEqual(ee['referenced_columns'].sort(), expected['referenced_columns'].sort()) + self.assertEqual(ee['referenced_functions'].sort(), expected['referenced_functions'].sort()) + self.assertTrue('referencing_fields' in ee) + self.assertEqual(ee['referencing_fields'].sort(), expected['referencing_fields'].sort()) + def test_expression_default_api_layer_config(self): """ Test Layer API REST config with expression default value diff --git a/g3w-admin/qdjango/utils/qgis.py b/g3w-admin/qdjango/utils/qgis.py index 63bfe436b..371e62b01 100644 --- a/g3w-admin/qdjango/utils/qgis.py +++ b/g3w-admin/qdjango/utils/qgis.py @@ -31,7 +31,8 @@ def explode_expression(expression): # For current_values function in filter expression get parameter field fo it if "current_value" in filter_expression['referenced_functions']: - groups = re.findall(r'current_value\([^\S]?[\'"](.*?)["\'][^\S]?\)|(\w+=\w+)', filter_expression['expression']) + groups = re.findall(r'current_value[^\S]*\([^\S]?[\'"|\\\'](.*?)["\'|\\\'][^\S]?\)|(\w+=\w+)', + filter_expression['expression']) filter_expression['referencing_fields'] = [g[0] for g in groups] return filter_expression \ No newline at end of file