Fixed #25758 -- Defaulted to current language FORMATs in date/time filters

Thanks Ali Lozano for the report and the initial patch, and Tim Graham for
the review.
This commit is contained in:
Claude Paroz 2016-01-30 14:25:56 +01:00
parent c47364ef0c
commit be9bd3348d
3 changed files with 27 additions and 9 deletions

View File

@ -7,7 +7,6 @@ from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation
from functools import wraps from functools import wraps
from pprint import pformat from pprint import pformat
from django.conf import settings
from django.utils import formats, six from django.utils import formats, six
from django.utils.dateformat import format, time_format from django.utils.dateformat import format, time_format
from django.utils.encoding import force_text, iri_to_uri from django.utils.encoding import force_text, iri_to_uri
@ -727,8 +726,6 @@ def date(value, arg=None):
"""Formats a date according to the given format.""" """Formats a date according to the given format."""
if value in (None, ''): if value in (None, ''):
return '' return ''
if arg is None:
arg = settings.DATE_FORMAT
try: try:
return formats.date_format(value, arg) return formats.date_format(value, arg)
except AttributeError: except AttributeError:
@ -743,8 +740,6 @@ def time(value, arg=None):
"""Formats a time according to the given format.""" """Formats a time according to the given format."""
if value in (None, ''): if value in (None, ''):
return '' return ''
if arg is None:
arg = settings.TIME_FORMAT
try: try:
return formats.time_format(value, arg) return formats.time_format(value, arg)
except AttributeError: except AttributeError:

View File

@ -1,8 +1,8 @@
from datetime import datetime, time from datetime import datetime, time
from django.template.defaultfilters import date from django.template.defaultfilters import date
from django.test import SimpleTestCase from django.test import SimpleTestCase, override_settings
from django.utils import timezone from django.utils import timezone, translation
from ..utils import setup from ..utils import setup
from .timezone_utils import TimezoneTestCase from .timezone_utils import TimezoneTestCase
@ -20,6 +20,17 @@ class DateTests(TimezoneTestCase):
output = self.engine.render_to_string('date02', {'d': datetime(2008, 1, 1)}) output = self.engine.render_to_string('date02', {'d': datetime(2008, 1, 1)})
self.assertEqual(output, 'Jan. 1, 2008') self.assertEqual(output, 'Jan. 1, 2008')
@override_settings(USE_L10N=True)
@setup({'date02_l10n': '{{ d|date }}'})
def test_date02_l10n(self):
"""
Without arg and when USE_L10N is True, the active language's DATE_FORMAT
is used.
"""
with translation.override('fr'):
output = self.engine.render_to_string('date02_l10n', {'d': datetime(2008, 1, 1)})
self.assertEqual(output, '1 janvier 2008')
@setup({'date03': '{{ d|date:"m" }}'}) @setup({'date03': '{{ d|date:"m" }}'})
def test_date03(self): def test_date03(self):
""" """

View File

@ -1,8 +1,8 @@
from datetime import time from datetime import time
from django.template.defaultfilters import time as time_filter from django.template.defaultfilters import time as time_filter
from django.test import SimpleTestCase from django.test import SimpleTestCase, override_settings
from django.utils import timezone from django.utils import timezone, translation
from ..utils import setup from ..utils import setup
from .timezone_utils import TimezoneTestCase from .timezone_utils import TimezoneTestCase
@ -13,6 +13,18 @@ class TimeTests(TimezoneTestCase):
#20693: Timezone support for the time template filter #20693: Timezone support for the time template filter
""" """
@setup({'time00': '{{ dt|time }}'})
def test_time00(self):
output = self.engine.render_to_string('time00', {'dt': time(16, 25)})
self.assertEqual(output, '4:25 p.m.')
@override_settings(USE_L10N=True)
@setup({'time00_l10n': '{{ dt|time }}'})
def test_time00_l10n(self):
with translation.override('fr'):
output = self.engine.render_to_string('time00_l10n', {'dt': time(16, 25)})
self.assertEqual(output, '16:25')
@setup({'time01': '{{ dt|time:"e:O:T:Z" }}'}) @setup({'time01': '{{ dt|time:"e:O:T:Z" }}'})
def test_time01(self): def test_time01(self):
output = self.engine.render_to_string('time01', {'dt': self.now_tz_i}) output = self.engine.render_to_string('time01', {'dt': self.now_tz_i})