api: allow multi values for filter-internal-id and operators eq & ne (#68013)
gitea-wip/wcs/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2022-08-08 16:21:05 +02:00
parent c985e039c2
commit 6ecfd58a02
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 76 additions and 27 deletions

View File

@ -1422,6 +1422,48 @@ def test_api_list_formdata_internal_id_filter(pub, local_user):
)
assert resp.json['err_desc'] == 'Invalid value "blabla" for "filter-internal-id-value"'
# multi-ids
resp = get_app(pub).get(
sign_uri(
'/api/forms/test/list?filter-internal-id=1&filter-internal-id=3&filter-internal-id-operator=eq',
user=local_user,
)
)
assert len(resp.json) == 2
resp = get_app(pub).get(
sign_uri(
'/api/forms/test/list?filter-internal-id=1&filter-internal-id=3&filter-internal-id-operator=ne',
user=local_user,
)
)
assert len(resp.json) == 9
for operator in ['lt', 'lte', 'gt', 'gte']:
# list of values not allowed with theese operators
resp = get_app(pub).get(
sign_uri(
'/api/forms/test/list?filter-internal-id=1&filter-internal-id=3&filter-internal-id-operator=%s'
% operator,
user=local_user,
),
status=400,
)
assert (
resp.json['err_desc']
== 'Invalid value "[\'1\', \'3\']" for "filter-internal-id" and operator "%s"' % operator
)
# not integers
resp = get_app(pub).get(
sign_uri(
'/api/forms/test/list?filter-internal-id=1&filter-internal-id=a&filter-internal-id-operator=eq',
user=local_user,
),
status=400,
)
assert (
resp.json['err_desc'] == 'Invalid value "[\'1\', \'a\']" for "filter-internal-id" and operator "eq"'
)
def test_api_list_formdata_number_filter(pub, local_user):
pub.role_class.wipe()

View File

@ -1892,6 +1892,26 @@ class FormPage(Directory):
# check value types
if filter_field.type == 'internal-id':
def _report_error(value, operator):
if custom_view:
get_publisher().record_error(
_(
'Invalid value "%s" for custom view "%s", CardDef "%s", field "internal-id", operator "%s"'
)
% (
value,
custom_view.slug,
custom_view.formdef.name,
operator,
)
)
else:
report_error(
_('Invalid value "%s" for "filter-internal-id" and operator "%s"')
% (value, operator)
)
if Template.is_template_string(filter_field_value):
if keep_templates:
criterias.append(criteria('id', filter_field_value))
@ -1906,34 +1926,21 @@ class FormPage(Directory):
criterias.append(Nothing())
continue
def record_error(value, operator):
get_publisher().record_error(
_(
'Invalid value "%s" for custom view "%s", CardDef "%s", field "internal-id", operator "%s"'
)
% (
value,
custom_view.slug,
custom_view.formdef.name,
operator,
)
)
if isinstance(filter_field_value, list):
try:
[int(v) for v in filter_field_value]
except ValueError:
record_error(filter_field_value, filter_field_operator)
criterias.append(Nothing())
continue
if filter_field_operator == 'eq':
criterias.append(Contains('id', filter_field_value))
elif filter_field_operator == 'ne':
criterias.append(NotContains('id', filter_field_value))
else:
record_error(filter_field_value, filter_field_operator)
criterias.append(Nothing())
if isinstance(filter_field_value, list):
try:
[int(v) for v in filter_field_value]
except ValueError:
_report_error(filter_field_value, filter_field_operator)
criterias.append(Nothing())
continue
if filter_field_operator == 'eq':
criterias.append(Contains('id', filter_field_value))
elif filter_field_operator == 'ne':
criterias.append(NotContains('id', filter_field_value))
else:
_report_error(filter_field_value, filter_field_operator)
criterias.append(Nothing())
continue
try:
filter_field_value = int(filter_field_value)
except ValueError: