Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix field filter for comma separated values ​​requests (e.g. "field" IN('value1', 'value2')). #876

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -283,7 +284,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 @@ -303,6 +305,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 @@ -335,6 +339,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 @@ -348,7 +354,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
Binary file modified g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg
Binary file not shown.
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 @@ -1576,6 +1576,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
Loading