From bdc8a59ff09ce3a8bf13f42ee9ba808a188e3870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Fri, 21 Apr 2023 10:38:51 +0200 Subject: [PATCH] publik: add removeprefix and removesuffix filters (#74786) --- .../publik/templatetags/publik.py | 22 ++++++++++++-- tests/test_publik.py | 30 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/publik_django_templatetags/publik/templatetags/publik.py b/publik_django_templatetags/publik/templatetags/publik.py index c46e2dd..9878d7b 100644 --- a/publik_django_templatetags/publik/templatetags/publik.py +++ b/publik_django_templatetags/publik/templatetags/publik.py @@ -22,7 +22,7 @@ from decimal import InvalidOperation as DecimalInvalidOperation from django import template from django.template import defaultfilters -from django.utils.encoding import force_text +from django.utils.encoding import force_str from publik_django_templatetags.publik import utils @@ -62,7 +62,25 @@ def as_list(obj): @register.filter def split(string, separator=' '): - return (force_text(string) or '').split(separator) + return (force_str(string) or '').split(separator) + + +@register.filter +def removeprefix(string, prefix=None): + if not string: + return '' + value = force_str(string) + prefix = force_str(prefix) + return value.removeprefix(prefix) + + +@register.filter +def removesuffix(string, suffix=None): + if not string: + return '' + value = force_str(string) + suffix = force_str(suffix) + return value.removesuffix(suffix) @register.filter diff --git a/tests/test_publik.py b/tests/test_publik.py index 681632e..fbd85ab 100644 --- a/tests/test_publik.py +++ b/tests/test_publik.py @@ -63,6 +63,36 @@ def test_split(): assert t.render(Context({'plop': 42})) == '42 ' +def test_removeprefix(): + t = Template('{{ foo|removeprefix }}') + assert t.render(Context({})) == '' + assert t.render(Context({'foo': None})) == '' + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + t = Template('{{ foo|removeprefix:"" }}') + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + t = Template('{{ foo|removeprefix:"XY" }}') + assert t.render(Context({'foo': 'XYfoo barXY'})) == 'foo barXY' + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + assert t.render(Context({'foo': 'xyfoo barXY'})) == 'xyfoo barXY' + assert t.render(Context({'foo': ' XYfoo barXY'})) == ' XYfoo barXY' + assert t.render(Context({'foo': 'XYXYfoo barXY'})) == 'XYfoo barXY' + + +def test_removesuffix(): + t = Template('{{ foo|removesuffix }}') + assert t.render(Context()) == '' + assert t.render(Context({'foo': None})) == '' + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + t = Template('{{ foo|removesuffix:"" }}') + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + t = Template('{{ foo|removesuffix:"XY" }}') + assert t.render(Context({'foo': 'XYfoo barXY'})) == 'XYfoo bar' + assert t.render(Context({'foo': 'foo bar'})) == 'foo bar' + assert t.render(Context({'foo': 'XYfoo barxy'})) == 'XYfoo barxy' + assert t.render(Context({'foo': 'XYfoo barXY '})) == 'XYfoo barXY ' + assert t.render(Context({'foo': 'XYfoo barXYXY'})) == 'XYfoo barXY' + + def test_first(): t = Template('{{ foo|first }}')