opendatasoft: remove operators form query parameter (#43235)

This commit is contained in:
Nicolas Roche 2022-05-27 12:09:37 +02:00
parent cdf9e3ea9d
commit 377bfada8e
2 changed files with 19 additions and 3 deletions

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
from urllib import parse as urlparse
from django.db import models
@ -77,7 +78,10 @@ class OpenDataSoft(BaseResource):
if id is not None:
params['q'] = 'recordid:%s' % id
elif q is not None:
params['q'] = q
# remove query language operators
terms = re.split(r'[^\w]', q)
terms = [term for term in terms if len(term) > 1 and term.lower() not in ['and', 'or', 'not']]
params['q'] = ' '.join(terms)
elif sort:
params['sort'] = sort
if self.api_key:

View File

@ -244,7 +244,7 @@ def test_search_using_q(mocked_get, app, connector):
'apikey': 'my_secret',
'dataset': 'referentiel-adresse-test',
'rows': '3',
'q': "rue de l'aubepine",
'q': "rue de aubepine",
}
assert not resp.json['err']
assert len(resp.json['data']) == 3
@ -263,6 +263,18 @@ def test_search_using_q(mocked_get, app, connector):
# check additional attributes
assert [x['numero'] for x in resp.json['data']] == ['33', '19', '29']
# check operators are removed
params['q'] = 'please, do NOT send boolean operators like And, OR and nOt'
resp = app.get(endpoint, params=params, status=200)
assert mocked_get.call_args[1]['params']['q'] == 'please do send boolean operators like'
params['q'] = 'field operators are almost ignored too:, -, ==, >, <, >=, <=, [start_date TO end_date]'
resp = app.get(endpoint, params=params, status=200)
assert (
mocked_get.call_args[1]['params']['q']
== 'field operators are almost ignored too start_date TO end_date'
)
@mock.patch('passerelle.utils.Request.get')
def test_search_using_id(mocked_get, app, connector):
@ -332,7 +344,7 @@ def test_query_q_using_q(mocked_get, app, query):
'refine.source': ['Ville et Eurométropole de Strasbourg'],
'exclude.numero': ['42', '43'],
'rows': 3,
'q': "rue de l'aubepine",
'q': "rue de aubepine",
}
assert not resp.json['err']
assert len(resp.json['data']) == 3