Skip to content

Commit

Permalink
Fix explode_expression (#921) (#922)
Browse files Browse the repository at this point in the history
* Fix regex expression.

* Update test.

* Optional blank spaces

---------

Co-authored-by: wlorenzetti <lorenzett@gis3w.it>
(cherry picked from commit 2efd07d)

Co-authored-by: Walter Lorenzetti <lorenzetti@gis3w.it>
  • Loading branch information
github-actions[bot] and wlorenzetti authored Aug 2, 2024
1 parent 595624c commit ec61ff0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
54 changes: 54 additions & 0 deletions g3w-admin/qdjango/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion g3w-admin/qdjango/utils/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec61ff0

Please sign in to comment.