Skip to content

Commit

Permalink
Fix for 'comma' inside value of a field filter. (#876) (#877)
Browse files Browse the repository at this point in the history
Co-authored-by: wlorenzetti <[email protected]>
(cherry picked from commit e78c93a)
  • Loading branch information
wlorenzetti committed Jun 11, 2024
1 parent 4cebea2 commit 8ec4d48
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions g3w-admin/core/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from urllib.parse import unquote
from core.utils.qgisapi import get_qgis_layer, get_qgis_features
from qdjango.models import Layer
import re

class BaseFilterBackend():
"""Base class for QGIS request filters"""
Expand Down Expand Up @@ -287,7 +288,8 @@ def apply_filter(self, request, metadata_layer, qgis_feature_request, view):
# field can be multiple separated by ','
# i.e. $field=name|eq|Rome,state|eq|Italy

fields = suggest_value.split(',')
pattern = r',(?=(?:[^()]*\([^()]*\))*[^()]*$)'
fields = re.split(pattern, suggest_value)

count = 0
nfields = len(fields)
Expand All @@ -307,6 +309,8 @@ def apply_filter(self, request, metadata_layer, qgis_feature_request, view):
raise ParseError(
'Invalid field string supplied for parameter field')

# Make lowercase field_operator
field_operator = field_operator.lower()
if not self._is_valid_field(qgis_layer, field_name):
raise Exception(
f"{field_name} doesn't belong from layer {qgis_layer.name()}!")
Expand Down Expand Up @@ -339,6 +343,8 @@ def apply_filter(self, request, metadata_layer, qgis_feature_request, view):
),
)
)


qfr.combineFilterExpression(vr_single_search_expression)
features = get_qgis_features(relation_qgs_layer, qfr)

Expand All @@ -352,7 +358,7 @@ def apply_filter(self, request, metadata_layer, qgis_feature_request, view):
quoted_field_value = self._quote_value(
f'{pre_post_operator}{unquote(field_value)}{pre_post_operator}')
else:
quoted_field_value = field_value
quoted_field_value = unquote(field_value)

single_search_expression = '{field_name} {field_operator} {field_value}'.format(
field_name=self._quote_identifier(field_name),
Expand Down
27 changes: 27 additions & 0 deletions g3w-admin/qdjango/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,33 @@ def test_server_filters_combination_api(self):

self.assertEqual(resp['vector']['count'], total_count)

qgs_request = QgsFeatureRequest()
qgs_request.setFilterExpression('"ISO2_CODE" IN (\'IT\', \'FR\')')
total_count = len([f for f in qgis_layer.getFeatures(qgs_request)])

# For IN comparator
# .................
# Test http 'get' method:
resp = json.loads(self._testApiCall('core-vector-api',
['data', 'qdjango', self.project310.instance.pk,
cities.qgs_layer_id],
{
'field': 'ISO2_CODE|in|(\'IT\', \'FR\')'
}).content)

self.assertEqual(resp['vector']['count'], total_count)

# Test http 'post' method:
resp = json.loads(self._testApiCall('core-vector-api',
['data', 'qdjango', self.project310.instance.pk,
cities.qgs_layer_id],
{
'field': 'ISO2_CODE|in|(\'IT\', \'FR\')'
},
method='post').content)

self.assertEqual(resp['vector']['count'], total_count)

qgs_request = QgsFeatureRequest()
qgs_request.setFilterExpression(
'"ISO2_CODE" = \'IT\' OR "ISO2_CODE" = \'FR\'')
Expand Down

0 comments on commit 8ec4d48

Please sign in to comment.