astech: add filters to column results (#82963)
gitea/passerelle/pipeline/head This commit looks good
Details
gitea/passerelle/pipeline/head This commit looks good
Details
This commit is contained in:
parent
0154defcce
commit
fa50ff9129
|
@ -220,6 +220,48 @@ class ASTech(BaseResource, HTTPResource):
|
|||
json_response = self.call_json(method, url, params=params, **kwargs)
|
||||
return json_response
|
||||
|
||||
def get_view_schema(self, view_code):
|
||||
cache_key = 'astech-%s-%s-schema' % (self.id, view_code)
|
||||
schema = cache.get(cache_key)
|
||||
if schema:
|
||||
return schema
|
||||
endpoint = 'apicli/data/%s/columns' % view_code
|
||||
columns = self.call(endpoint).get('columns', [])
|
||||
schema = {}
|
||||
for column in columns:
|
||||
column.pop('des')
|
||||
code = column.pop('code')
|
||||
if column['type'] == 'NUM':
|
||||
column['operator'] = '='
|
||||
else:
|
||||
column['operator'] = 'is_equal'
|
||||
schema[code] = column
|
||||
cache.set(cache_key, schema)
|
||||
return schema
|
||||
|
||||
def build_view_filters(self, view_code, filters):
|
||||
if not filters:
|
||||
return []
|
||||
schema = self.get_view_schema(view_code)
|
||||
filters_expression = []
|
||||
for expression in filters.split(';'):
|
||||
try:
|
||||
name, value = expression.split('=')
|
||||
except ValueError:
|
||||
continue
|
||||
if value and schema[name]['length'] and len(value) > int(schema[name]['length']):
|
||||
raise APIError(
|
||||
_('Value of %s exceeds authorized length (%s)') % (name, schema[name]['length'])
|
||||
)
|
||||
filters_expression.append(
|
||||
{
|
||||
'field': name,
|
||||
'type': schema[name]['type'],
|
||||
'filter': {'value': value, 'operator': schema[name]['operator']},
|
||||
}
|
||||
)
|
||||
return filters_expression
|
||||
|
||||
@endpoint(
|
||||
name='connections',
|
||||
description=_('See all possible connections codes (see configuration)'),
|
||||
|
@ -492,11 +534,16 @@ class ASTech(BaseResource, HTTPResource):
|
|||
'description': _('Name of column contaning the label'),
|
||||
'example_value': 'DESIGNATION',
|
||||
},
|
||||
'filters': {
|
||||
'description': _('Semicolon separated filter expressions'),
|
||||
'example_value': 'GENRE=SIT;SECTEUR=S1',
|
||||
},
|
||||
},
|
||||
)
|
||||
def get_view_data(self, request, code, id_column, text_column):
|
||||
def get_view_data(self, request, code, id_column, text_column, filters=None):
|
||||
endpoint = 'apicli/data/%s/results' % code
|
||||
results = self.call(endpoint, json={'data': {'filters': []}})
|
||||
filters = self.build_view_filters(code, filters)
|
||||
results = self.call(endpoint, json={'data': {'filters': filters}})
|
||||
for result in results:
|
||||
result['id'] = result[id_column]
|
||||
result['text'] = result[text_column]
|
||||
|
|
|
@ -85,7 +85,8 @@ VIEWS_RESPONSE = """
|
|||
|
||||
COLUMNS_RESPONSE = """
|
||||
{"columns":[{"code":"BIEN_ID","des":"Identifiant du bien AS-TECH","type":"NUM","length":"18"},
|
||||
{"code":"ANCETREID","des":"Identifiant de l\u0027anc\u00eatre du bien","type":"NUM","length":""}]}
|
||||
{"code":"SECTEUR","des":"Secteur","type":"TXT","length":"10"},
|
||||
{"code":"GENRE","des":"Genre - 1er niveau obligatoire de classification ","type":"TXT","length":"5"}]}
|
||||
"""
|
||||
|
||||
RESULTS_RESPONSE = """[{"BIEN_ID": "2219", "CODE_BIEN": "AC-849-YE", "DESIGNATION": "RENAULT KANGOO"},
|
||||
|
@ -505,6 +506,7 @@ def test_view_data(mocked_auth, mocked_request, app, setup):
|
|||
)
|
||||
assert mocked_request.call_args[0][0] == 'post'
|
||||
assert mocked_request.call_args[0][1].endswith('apicli/data/ASTECH_BIENS/results')
|
||||
assert response.json['err'] == 0
|
||||
assert response.json['data']
|
||||
for r in response.json['data']:
|
||||
assert 'id' in r
|
||||
|
@ -521,3 +523,41 @@ def test_view_data(mocked_auth, mocked_request, app, setup):
|
|||
params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION', 'q': 'KANGOO'},
|
||||
)
|
||||
assert len(response.json['data']) == 2
|
||||
|
||||
mocked_request.side_effect = [
|
||||
tests.utils.FakedResponse(content=COLUMNS_RESPONSE, status_code=200),
|
||||
tests.utils.FakedResponse(content=RESULTS_RESPONSE, status_code=200),
|
||||
]
|
||||
|
||||
response = app.get(
|
||||
endpoint,
|
||||
params={
|
||||
'code': 'ASTECH_BIENS',
|
||||
'id_column': 'BIEN_ID',
|
||||
'text_column': 'DESIGNATION',
|
||||
'filters': 'GENRE=SIT;SECTEUR=S1',
|
||||
},
|
||||
)
|
||||
|
||||
assert mocked_request.call_args[0][0] == 'post'
|
||||
assert mocked_request.call_args[0][1].endswith('apicli/data/ASTECH_BIENS/results')
|
||||
assert mocked_request.call_args[1]['json'] == {
|
||||
'data': {
|
||||
'filters': [
|
||||
{'field': 'GENRE', 'type': 'TXT', 'filter': {'value': 'SIT', 'operator': 'is_equal'}},
|
||||
{'field': 'SECTEUR', 'type': 'TXT', 'filter': {'value': 'S1', 'operator': 'is_equal'}},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
response = app.get(
|
||||
endpoint,
|
||||
params={
|
||||
'code': 'ASTECH_BIENS',
|
||||
'id_column': 'BIEN_ID',
|
||||
'text_column': 'DESIGNATION',
|
||||
'filters': 'GENRE=TESTING',
|
||||
},
|
||||
)
|
||||
assert response.json['err'] == 1
|
||||
assert response.json['err_desc'] == 'Value of GENRE exceeds authorized length (5)'
|
||||
|
|
Loading…
Reference in New Issue