From 709a29d5510b9507a741b20723838b8889ce0f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 20 Dec 2022 10:20:19 +0100 Subject: [PATCH] arcgis: add support for formatting strings as list (#72632) --- .../arcgis/migrations/0005_auto_20200310_1517.py | 9 +++++++-- passerelle/apps/arcgis/models.py | 13 +++++++++---- tests/test_arcgis.py | 7 +++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/passerelle/apps/arcgis/migrations/0005_auto_20200310_1517.py b/passerelle/apps/arcgis/migrations/0005_auto_20200310_1517.py index 46bfd811..d8b449e4 100644 --- a/passerelle/apps/arcgis/migrations/0005_auto_20200310_1517.py +++ b/passerelle/apps/arcgis/migrations/0005_auto_20200310_1517.py @@ -32,8 +32,13 @@ class Migration(migrations.Migration): models.TextField( blank=True, help_text=( - "Use syntax {name} to introduce a string parameter and {name:d} for a decimal parameter. ex.:
" - "adress LIKE ('%' || UPPER({adress}) || '%')
population < {population:d}
" + '
Use syntax {name} to introduce a string parameter and ' + '{name:d} for a decimal parameter. Use syntax {name:l} ' + 'for a parameter that should be interpreted a a list of strings ' + '(comma-separated). ex.:
' + 'adress LIKE (\'%\' || UPPER({adress}) || \'%\')
' + 'population < {population:d}
' + 'ID IN {ids:l} (with ids being "11,13,17")' ), validators=[passerelle.apps.arcgis.models.validate_where], verbose_name='ArcGis Where Clause', diff --git a/passerelle/apps/arcgis/models.py b/passerelle/apps/arcgis/models.py index c605bd0a..5f4ee18a 100644 --- a/passerelle/apps/arcgis/models.py +++ b/passerelle/apps/arcgis/models.py @@ -389,6 +389,8 @@ class SqlFormatter(string.Formatter): def format_field(self, value, format_spec): if format_spec and format_spec[-1].isalpha() and format_spec[-1] == 'd': value = int(value) + if format_spec and format_spec[-1] == 'l': + return '(' + ', '.join(SqlFormatter().format('{x}', x=x) for x in value.split(',')) + ')' formatted = super().format_field(value, format_spec) if not format_spec or not format_spec[-1].isalpha() or format_spec[-1] == 's': formatted = "'%s'" % formatted.replace("'", "''") @@ -421,10 +423,13 @@ class Query(BaseQuery): validators=[validate_where], help_text=mark_safe_lazy( _( - 'Use syntax {name} to introduce a string ' - 'parameter and {name:d} for a decimal parameter. ex.:
' - 'adress LIKE (\'%\' || UPPER({adress}) || \'%\')
' - 'population < {population:d}
' + '
Use syntax {name} to introduce a string ' + 'parameter and {name:d} for a decimal parameter. ' + 'Use syntax {name:l} for a parameter that should be ' + 'interpreted a a list of strings (comma-separated). ex.:
' + 'adress LIKE (\'%\' || UPPER({adress}) || \'%\')
' + 'population < {population:d}
' + 'ID IN {ids:l} (with ids being "11,13,17")' ) ), ) diff --git a/tests/test_arcgis.py b/tests/test_arcgis.py index 29f4462a..05ca6742 100644 --- a/tests/test_arcgis.py +++ b/tests/test_arcgis.py @@ -703,6 +703,13 @@ def test_validate_where(format_string, fail): }, ValueError, ), + ( + 'ID IN {ids:l}', + { + 'ids': '1,3,5', + }, + "ID IN ('1', '3', '5')", + ), ], ) def test_sql_formatter(format_string, kwargs, expected):