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