arcgis: add support for formatting strings as list (#72632)

This commit is contained in:
Frédéric Péters 2022-12-20 10:20:19 +01:00 committed by Gitea
parent ae4e05a09e
commit 709a29d551
3 changed files with 23 additions and 6 deletions

View File

@ -32,8 +32,13 @@ class Migration(migrations.Migration):
models.TextField(
blank=True,
help_text=(
"<span>Use syntax <tt>{name}</tt> to introduce a string parameter and <tt>{name:d}</tt> for a decimal parameter. ex.:<br/>"
"<tt>adress LIKE ('%' || UPPER({adress}) || '%')</tt><br/><tt>population < {population:d}</tt></span>"
'<div>Use syntax <tt>{name}</tt> to introduce a string parameter and '
'<tt>{name:d}</tt> for a decimal parameter. Use syntax <tt>{name:l}</tt> '
'for a parameter that should be interpreted a a list of strings '
'(comma-separated). ex.:</div>'
'<tt>adress LIKE (\'%\' || UPPER({adress}) || \'%\')</tt><br>'
'<tt>population < {population:d}</tt><br>'
'<tt>ID IN {ids:l}</tt> (with ids being "11,13,17")'
),
validators=[passerelle.apps.arcgis.models.validate_where],
verbose_name='ArcGis Where Clause',

View File

@ -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(
_(
'<span>Use syntax <tt>{name}</tt> to introduce a string '
'parameter and <tt>{name:d}</tt> for a decimal parameter. ex.:<br/>'
'<tt>adress LIKE (\'%\' || UPPER({adress}) || \'%\')</tt><br/>'
'<tt>population < {population:d}</tt></span>'
'<div>Use syntax <tt>{name}</tt> to introduce a string '
'parameter and <tt>{name:d}</tt> for a decimal parameter. '
'Use syntax <tt>{name:l}</tt> for a parameter that should be '
'interpreted a a list of strings (comma-separated). ex.:</div>'
'<tt>adress LIKE (\'%\' || UPPER({adress}) || \'%\')</tt><br>'
'<tt>population < {population:d}</tt><br>'
'<tt>ID IN {ids:l}</tt> (with ids being "11,13,17")'
)
),
)

View File

@ -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):