Refs #33864 -- Removed length_is template filter per deprecation timeline.

This commit is contained in:
Mariusz Felisiak 2023-09-12 21:34:40 +02:00
parent 2abf417c81
commit 14ef92fa9e
4 changed files with 2 additions and 164 deletions

View File

@ -2,7 +2,6 @@
import random as random_module
import re
import types
import warnings
from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation, getcontext
from functools import wraps
from inspect import unwrap
@ -12,7 +11,6 @@ from urllib.parse import quote
from django.utils import formats
from django.utils.dateformat import format, time_format
from django.utils.deprecation import RemovedInDjango51Warning
from django.utils.encoding import iri_to_uri
from django.utils.html import avoid_wrapping, conditional_escape, escape, escapejs
from django.utils.html import json_script as _json_script
@ -622,20 +620,6 @@ def length(value):
return 0
@register.filter(is_safe=False)
def length_is(value, arg):
"""Return a boolean of whether the value's length is the argument."""
warnings.warn(
"The length_is template filter is deprecated in favor of the length template "
"filter and the == operator within an {% if %} tag.",
RemovedInDjango51Warning,
)
try:
return len(value) == int(arg)
except (ValueError, TypeError):
return ""
@register.filter(is_safe=True)
def random(value):
"""Return a random item from the list."""

View File

@ -2161,24 +2161,6 @@ If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
The filter returns ``0`` for an undefined variable.
.. templatefilter:: length_is
``length_is``
-------------
.. deprecated:: 4.2
Returns ``True`` if the value's length is the argument, or ``False`` otherwise.
For example:
.. code-block:: html+django
{{ value|length_is:"4" }}
If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
``True``.
.. templatefilter:: linebreaks
``linebreaks``

View File

@ -254,3 +254,5 @@ to remove usage of these features.
* The ``BaseUserManager.make_random_password()`` method is removed.
* The model's ``Meta.index_together`` option is removed.
* The ``length_is`` template filter is removed.

View File

@ -1,130 +0,0 @@
from django.template.defaultfilters import length_is
from django.test import SimpleTestCase, ignore_warnings
from django.utils.deprecation import RemovedInDjango51Warning
from ..utils import setup
@ignore_warnings(category=RemovedInDjango51Warning)
class LengthIsTests(SimpleTestCase):
@setup({"length_is01": '{% if some_list|length_is:"4" %}Four{% endif %}'})
def test_length_is01(self):
output = self.engine.render_to_string(
"length_is01", {"some_list": ["4", None, True, {}]}
)
self.assertEqual(output, "Four")
@setup(
{
"length_is02": (
'{% if some_list|length_is:"4" %}Four{% else %}Not Four{% endif %}'
)
}
)
def test_length_is02(self):
output = self.engine.render_to_string(
"length_is02", {"some_list": ["4", None, True, {}, 17]}
)
self.assertEqual(output, "Not Four")
@setup({"length_is03": '{% if mystring|length_is:"4" %}Four{% endif %}'})
def test_length_is03(self):
output = self.engine.render_to_string("length_is03", {"mystring": "word"})
self.assertEqual(output, "Four")
@setup(
{
"length_is04": (
'{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}'
)
}
)
def test_length_is04(self):
output = self.engine.render_to_string("length_is04", {"mystring": "Python"})
self.assertEqual(output, "Not Four")
@setup(
{
"length_is05": (
'{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}'
)
}
)
def test_length_is05(self):
output = self.engine.render_to_string("length_is05", {"mystring": ""})
self.assertEqual(output, "Not Four")
@setup(
{
"length_is06": (
"{% with var|length as my_length %}{{ my_length }}{% endwith %}"
)
}
)
def test_length_is06(self):
output = self.engine.render_to_string("length_is06", {"var": "django"})
self.assertEqual(output, "6")
# Boolean return value from length_is should not be coerced to a string
@setup(
{
"length_is07": (
'{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}'
)
}
)
def test_length_is07(self):
output = self.engine.render_to_string("length_is07", {})
self.assertEqual(output, "Length not 0")
@setup(
{
"length_is08": (
'{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}'
)
}
)
def test_length_is08(self):
output = self.engine.render_to_string("length_is08", {})
self.assertEqual(output, "Length is 1")
# Invalid uses that should fail silently.
@setup({"length_is09": '{{ var|length_is:"fish" }}'})
def test_length_is09(self):
output = self.engine.render_to_string("length_is09", {"var": "django"})
self.assertEqual(output, "")
@setup({"length_is10": '{{ int|length_is:"1" }}'})
def test_length_is10(self):
output = self.engine.render_to_string("length_is10", {"int": 7})
self.assertEqual(output, "")
@setup({"length_is11": '{{ none|length_is:"1" }}'})
def test_length_is11(self):
output = self.engine.render_to_string("length_is11", {"none": None})
self.assertEqual(output, "")
@ignore_warnings(category=RemovedInDjango51Warning)
class FunctionTests(SimpleTestCase):
def test_empty_list(self):
self.assertIs(length_is([], 0), True)
self.assertIs(length_is([], 1), False)
def test_string(self):
self.assertIs(length_is("a", 1), True)
self.assertIs(length_is("a", 10), False)
class DeprecationTests(SimpleTestCase):
@setup(
{"length_is_warning": "{{ string|length_is:3 }}"},
test_once=True,
)
def test_length_is_warning(self):
msg = (
"The length_is template filter is deprecated in favor of the length "
"template filter and the == operator within an {% if %} tag."
)
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
self.engine.render_to_string("length_is_warning", {"string": "good"})