misc: add |check_no_duplicates filter tag (#86530)
gitea/wcs/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-02-08 10:35:56 +01:00
parent 2f3bd2a38b
commit 8b2cd7d0e5
2 changed files with 22 additions and 0 deletions

View File

@ -1649,3 +1649,16 @@ def test_with_auth(pub):
Template('{{ service_url|with_auth:"username:password" }}').render(context)
== 'https://username:password@www.example.net/api/whatever?x=y'
)
def test_check_no_duplicates(pub):
pub.loggederror_class.wipe()
context = {'value1': ['a', 'b', 'c'], 'value2': ['a', 'a', 'b', 'c'], 'value3': None, 'value4': '12'}
assert Template('{% if value1|check_no_duplicates %}ok{% else %}nok{% endif %}').render(context) == 'ok'
assert Template('{% if value2|check_no_duplicates %}ok{% else %}nok{% endif %}').render(context) == 'nok'
assert Template('{% if value3|check_no_duplicates %}ok{% else %}nok{% endif %}').render(context) == 'ok'
assert pub.loggederror_class.count() == 0
assert Template('{% if value4|check_no_duplicates %}ok{% else %}nok{% endif %}').render(context) == 'nok'
assert pub.loggederror_class.count() == 1
logged_error = pub.loggederror_class.select()[0]
assert logged_error.summary == '|check_no_duplicates not used on a list (12)'

View File

@ -1289,3 +1289,12 @@ def with_auth(value, arg):
@register.filter
def wbr(value):
return mark_safe(value.replace('_', '_<wbr/>'))
@register.filter
def check_no_duplicates(value):
value = unlazy(value)
if not isinstance(value, (type(None), tuple, list, set)):
get_publisher().record_error(_('|check_no_duplicates not used on a list (%s)') % value)
return False
return bool(len(value or []) == len(set(value or [])))