mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Added unlocalize effect down to date/time filters
Co-authored-by: Natalia Bidart <124304+nessita@users.noreply.github.com>
This commit is contained in:
parent
57f9982312
commit
90862f4a82
@ -173,7 +173,7 @@ def apnumber(value):
|
|||||||
# Perform the comparison in the default time zone when USE_TZ = True
|
# Perform the comparison in the default time zone when USE_TZ = True
|
||||||
# (unless a specific time zone has been applied with the |timezone filter).
|
# (unless a specific time zone has been applied with the |timezone filter).
|
||||||
@register.filter(expects_localtime=True)
|
@register.filter(expects_localtime=True)
|
||||||
def naturalday(value, arg=None):
|
def naturalday(value, arg=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
For date values that are tomorrow, today or yesterday compared to
|
For date values that are tomorrow, today or yesterday compared to
|
||||||
present day return representing string. Otherwise, return a string
|
present day return representing string. Otherwise, return a string
|
||||||
|
@ -737,12 +737,14 @@ class FilterExpression:
|
|||||||
arg_vals.append(mark_safe(arg))
|
arg_vals.append(mark_safe(arg))
|
||||||
else:
|
else:
|
||||||
arg_vals.append(arg.resolve(context))
|
arg_vals.append(arg.resolve(context))
|
||||||
|
kwargs = {}
|
||||||
if getattr(func, "expects_localtime", False):
|
if getattr(func, "expects_localtime", False):
|
||||||
obj = template_localtime(obj, context.use_tz)
|
obj = template_localtime(obj, context.use_tz)
|
||||||
|
kwargs["use_l10n"] = context.use_l10n
|
||||||
if getattr(func, "needs_autoescape", False):
|
if getattr(func, "needs_autoescape", False):
|
||||||
new_obj = func(obj, autoescape=context.autoescape, *arg_vals)
|
new_obj = func(obj, autoescape=context.autoescape, *arg_vals, **kwargs)
|
||||||
else:
|
else:
|
||||||
new_obj = func(obj, *arg_vals)
|
new_obj = func(obj, *arg_vals, **kwargs)
|
||||||
if getattr(func, "is_safe", False) and isinstance(obj, SafeData):
|
if getattr(func, "is_safe", False) and isinstance(obj, SafeData):
|
||||||
obj = mark_safe(new_obj)
|
obj = mark_safe(new_obj)
|
||||||
else:
|
else:
|
||||||
|
@ -763,12 +763,12 @@ def get_digit(value, arg):
|
|||||||
|
|
||||||
|
|
||||||
@register.filter(expects_localtime=True, is_safe=False)
|
@register.filter(expects_localtime=True, is_safe=False)
|
||||||
def date(value, arg=None):
|
def date(value, arg=None, use_l10n=None):
|
||||||
"""Format a date according to the given format."""
|
"""Format a date according to the given format."""
|
||||||
if value in (None, ""):
|
if value in (None, ""):
|
||||||
return ""
|
return ""
|
||||||
try:
|
try:
|
||||||
return formats.date_format(value, arg)
|
return formats.date_format(value, arg, use_l10n=use_l10n)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
try:
|
||||||
return format(value, arg)
|
return format(value, arg)
|
||||||
@ -777,12 +777,12 @@ def date(value, arg=None):
|
|||||||
|
|
||||||
|
|
||||||
@register.filter(expects_localtime=True, is_safe=False)
|
@register.filter(expects_localtime=True, is_safe=False)
|
||||||
def time(value, arg=None):
|
def time(value, arg=None, use_l10n=None):
|
||||||
"""Format a time according to the given format."""
|
"""Format a time according to the given format."""
|
||||||
if value in (None, ""):
|
if value in (None, ""):
|
||||||
return ""
|
return ""
|
||||||
try:
|
try:
|
||||||
return formats.time_format(value, arg)
|
return formats.time_format(value, arg, use_l10n=use_l10n)
|
||||||
except (AttributeError, TypeError):
|
except (AttributeError, TypeError):
|
||||||
try:
|
try:
|
||||||
return time_format(value, arg)
|
return time_format(value, arg)
|
||||||
|
@ -1353,20 +1353,49 @@ class FormattingTests(SimpleTestCase):
|
|||||||
|
|
||||||
def test_unlocalize_honor_date_settings(self):
|
def test_unlocalize_honor_date_settings(self):
|
||||||
filter_template = Template(
|
filter_template = Template(
|
||||||
"{% load l10n %}Localized: {{ date }}. Unlocalized: {{ date|unlocalize }}."
|
"{% load l10n %}Localized: {{ my_value }}. "
|
||||||
|
"Unlocalized: {{ my_value|unlocalize }}."
|
||||||
)
|
)
|
||||||
tag_template = Template(
|
tag_template = Template(
|
||||||
"{% load l10n %}Localized: {{ date }}. {% localize off %}Unlocalized: "
|
"{% load l10n %}Localized: {{ my_value }}. {% localize off %}"
|
||||||
"{{ date }}{% endlocalize %}."
|
"Unlocalized: {{ my_value }}{% endlocalize %}."
|
||||||
)
|
)
|
||||||
context = Context({"date": datetime.date(2024, 12, 15)})
|
filter_inside_unlocalize = Template(
|
||||||
|
"{% load l10n %}Localized: {{ my_value|date }}. {% localize off %}"
|
||||||
|
"Unlocalized: {{ my_value|date:'DATE_FORMAT' }}{% endlocalize %}."
|
||||||
|
)
|
||||||
|
context = Context({"my_value": datetime.date(2024, 12, 15)})
|
||||||
expected_result = "Localized: 15. Dezember 2024. Unlocalized: 15-12-2024."
|
expected_result = "Localized: 15. Dezember 2024. Unlocalized: 15-12-2024."
|
||||||
with (
|
for case in (filter_template, tag_template, filter_inside_unlocalize):
|
||||||
translation.override("de", deactivate=True),
|
with (
|
||||||
self.settings(DATE_FORMAT="j-m-Y"),
|
self.subTest(case=str(case)),
|
||||||
):
|
translation.override("de", deactivate=True),
|
||||||
self.assertEqual(filter_template.render(context), expected_result)
|
self.settings(DATE_FORMAT="j-m-Y"),
|
||||||
self.assertEqual(tag_template.render(context), expected_result)
|
):
|
||||||
|
self.assertEqual(case.render(context), expected_result)
|
||||||
|
|
||||||
|
def test_unlocalize_honor_time_settings(self):
|
||||||
|
filter_template = Template(
|
||||||
|
"{% load l10n %}Localized: {{ my_value }}. "
|
||||||
|
"Unlocalized: {{ my_value|unlocalize }}."
|
||||||
|
)
|
||||||
|
tag_template = Template(
|
||||||
|
"{% load l10n %}Localized: {{ my_value }}. {% localize off %}"
|
||||||
|
"Unlocalized: {{ my_value }}{% endlocalize %}."
|
||||||
|
)
|
||||||
|
filter_inside_unlocalize = Template(
|
||||||
|
"{% load l10n %}Localized: {{ my_value|time }}. {% localize off %}"
|
||||||
|
"Unlocalized: {{ my_value|time }}{% endlocalize %}."
|
||||||
|
)
|
||||||
|
context = Context({"my_value": datetime.time(1, 2, 3)})
|
||||||
|
expected_result = "Localized: 01:02. Unlocalized: 01h 02m."
|
||||||
|
for case in (filter_template, tag_template, filter_inside_unlocalize):
|
||||||
|
with (
|
||||||
|
self.subTest(case=str(case)),
|
||||||
|
translation.override("de", deactivate=True),
|
||||||
|
self.settings(TIME_FORMAT="H\\h i\\m"),
|
||||||
|
):
|
||||||
|
self.assertEqual(case.render(context), expected_result)
|
||||||
|
|
||||||
def test_localized_off_numbers(self):
|
def test_localized_off_numbers(self):
|
||||||
"""A string representation is returned for unlocalized numbers."""
|
"""A string representation is returned for unlocalized numbers."""
|
||||||
|
Loading…
Reference in New Issue
Block a user