Fixed #12472 - Correctly show TimeFields values when using locale-aware formatting. Thanks to Ramiro Morales for providing the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12050 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0e88350893
commit
20fbba7f90
|
@ -158,7 +158,7 @@ class EasyInstanceField(object):
|
||||||
if isinstance(self.field, models.DateTimeField):
|
if isinstance(self.field, models.DateTimeField):
|
||||||
objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT'))
|
objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT'))
|
||||||
elif isinstance(self.field, models.TimeField):
|
elif isinstance(self.field, models.TimeField):
|
||||||
objs = capfirst(formats.date_format(self.raw_value, 'TIME_FORMAT'))
|
objs = capfirst(formats.time_format(self.raw_value, 'TIME_FORMAT'))
|
||||||
else:
|
else:
|
||||||
objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT'))
|
objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT'))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -15,10 +15,10 @@ except ImportError:
|
||||||
|
|
||||||
from django.template import Variable, Library
|
from django.template import Variable, Library
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.utils import formats
|
||||||
from django.utils.translation import ugettext, ungettext
|
from django.utils.translation import ugettext, ungettext
|
||||||
from django.utils.encoding import force_unicode, iri_to_uri
|
from django.utils.encoding import force_unicode, iri_to_uri
|
||||||
from django.utils.safestring import mark_safe, SafeData
|
from django.utils.safestring import mark_safe, SafeData
|
||||||
from django.utils.formats import date_format, number_format
|
|
||||||
|
|
||||||
register = Library()
|
register = Library()
|
||||||
|
|
||||||
|
@ -167,14 +167,14 @@ def floatformat(text, arg=-1):
|
||||||
return input_val
|
return input_val
|
||||||
|
|
||||||
if not m and p < 0:
|
if not m and p < 0:
|
||||||
return mark_safe(number_format(u'%d' % (int(d)), 0))
|
return mark_safe(formats.number_format(u'%d' % (int(d)), 0))
|
||||||
|
|
||||||
if p == 0:
|
if p == 0:
|
||||||
exp = Decimal(1)
|
exp = Decimal(1)
|
||||||
else:
|
else:
|
||||||
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
|
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
|
||||||
try:
|
try:
|
||||||
return mark_safe(number_format(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)), abs(p)))
|
return mark_safe(formats.number_format(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)), abs(p)))
|
||||||
except InvalidOperation:
|
except InvalidOperation:
|
||||||
return input_val
|
return input_val
|
||||||
floatformat.is_safe = True
|
floatformat.is_safe = True
|
||||||
|
@ -686,7 +686,7 @@ def date(value, arg=None):
|
||||||
if arg is None:
|
if arg is None:
|
||||||
arg = settings.DATE_FORMAT
|
arg = settings.DATE_FORMAT
|
||||||
try:
|
try:
|
||||||
return date_format(value, arg)
|
return formats.date_format(value, arg)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
try:
|
||||||
return format(value, arg)
|
return format(value, arg)
|
||||||
|
@ -696,16 +696,16 @@ date.is_safe = False
|
||||||
|
|
||||||
def time(value, arg=None):
|
def time(value, arg=None):
|
||||||
"""Formats a time according to the given format."""
|
"""Formats a time according to the given format."""
|
||||||
from django.utils.dateformat import time_format
|
from django.utils import dateformat
|
||||||
if value in (None, u''):
|
if value in (None, u''):
|
||||||
return u''
|
return u''
|
||||||
if arg is None:
|
if arg is None:
|
||||||
arg = settings.TIME_FORMAT
|
arg = settings.TIME_FORMAT
|
||||||
try:
|
try:
|
||||||
return date_format(value, arg)
|
return formats.time_format(value, arg)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
try:
|
||||||
return time_format(value, arg)
|
return dateformat.time_format(value, arg)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return ''
|
return ''
|
||||||
time.is_safe = False
|
time.is_safe = False
|
||||||
|
|
|
@ -55,6 +55,12 @@ def date_format(value, format=None):
|
||||||
"""
|
"""
|
||||||
return dateformat.format(value, get_format(format or 'DATE_FORMAT'))
|
return dateformat.format(value, get_format(format or 'DATE_FORMAT'))
|
||||||
|
|
||||||
|
def time_format(value, format=None):
|
||||||
|
"""
|
||||||
|
Formats a datetime.time object using a localizable format
|
||||||
|
"""
|
||||||
|
return dateformat.time_format(value, get_format(format or 'TIME_FORMAT'))
|
||||||
|
|
||||||
def number_format(value, decimal_pos=None):
|
def number_format(value, decimal_pos=None):
|
||||||
"""
|
"""
|
||||||
Formats a numeric value using localization settings
|
Formats a numeric value using localization settings
|
||||||
|
@ -84,7 +90,7 @@ def localize(value):
|
||||||
elif isinstance(value, datetime.date):
|
elif isinstance(value, datetime.date):
|
||||||
return date_format(value)
|
return date_format(value)
|
||||||
elif isinstance(value, datetime.time):
|
elif isinstance(value, datetime.time):
|
||||||
return date_format(value, 'TIME_FORMAT')
|
return time_format(value, 'TIME_FORMAT')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def localize_input(value, default=None):
|
def localize_input(value, default=None):
|
||||||
|
|
|
@ -4,7 +4,7 @@ import datetime
|
||||||
|
|
||||||
from django.template import Template, Context
|
from django.template import Template, Context
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.formats import get_format, date_format, number_format, localize, localize_input
|
from django.utils.formats import get_format, date_format, time_format, number_format, localize, localize_input
|
||||||
from django.utils.numberformat import format
|
from django.utils.numberformat import format
|
||||||
from django.test import TestCase, client
|
from django.test import TestCase, client
|
||||||
from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy
|
from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy
|
||||||
|
@ -91,8 +91,10 @@ class FormattingTests(TestCase):
|
||||||
self.f = 99999.999
|
self.f = 99999.999
|
||||||
self.d = datetime.date(2009, 12, 31)
|
self.d = datetime.date(2009, 12, 31)
|
||||||
self.dt = datetime.datetime(2009, 12, 31, 20, 50)
|
self.dt = datetime.datetime(2009, 12, 31, 20, 50)
|
||||||
|
self.t = datetime.time(10, 15, 48)
|
||||||
self.ctxt = Context({
|
self.ctxt = Context({
|
||||||
'n': self.n,
|
'n': self.n,
|
||||||
|
't': self.t,
|
||||||
'd': self.d,
|
'd': self.d,
|
||||||
'dt': self.dt,
|
'dt': self.dt,
|
||||||
'f': self.f
|
'f': self.f
|
||||||
|
@ -103,10 +105,10 @@ class FormattingTests(TestCase):
|
||||||
settings.USE_I18N = self._use_i18n
|
settings.USE_I18N = self._use_i18n
|
||||||
settings.USE_L10N = self._use_l10n
|
settings.USE_L10N = self._use_l10n
|
||||||
settings.USE_THOUSAND_SEPARATOR = self._use_thousand_separator
|
settings.USE_THOUSAND_SEPARATOR = self._use_thousand_separator
|
||||||
|
|
||||||
def test_locale_independent(self):
|
def test_locale_independent(self):
|
||||||
"""
|
"""
|
||||||
Localization of dates and numbers
|
Localization of numbers
|
||||||
"""
|
"""
|
||||||
settings.USE_L10N = True
|
settings.USE_L10N = True
|
||||||
settings.USE_THOUSAND_SEPARATOR = False
|
settings.USE_THOUSAND_SEPARATOR = False
|
||||||
|
@ -130,6 +132,7 @@ class FormattingTests(TestCase):
|
||||||
self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
|
self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
|
||||||
self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
|
self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
|
||||||
self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
|
self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
|
||||||
|
self.assertEqual(u'10:15 a.m.', time_format(self.t))
|
||||||
self.assertEqual(u'des. 31, 2009', date_format(self.d))
|
self.assertEqual(u'des. 31, 2009', date_format(self.d))
|
||||||
self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
||||||
self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
||||||
|
@ -144,6 +147,7 @@ class FormattingTests(TestCase):
|
||||||
self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
|
self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
|
||||||
self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
||||||
self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
|
self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
|
||||||
|
self.assertEqual(u'10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
|
||||||
self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
||||||
self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
|
self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
|
||||||
|
|
||||||
|
@ -184,6 +188,7 @@ class FormattingTests(TestCase):
|
||||||
self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
|
self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
|
||||||
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
|
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
|
||||||
self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
|
self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
|
||||||
|
self.assertEqual(u'10:15:48', time_format(self.t))
|
||||||
self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
|
self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
|
||||||
self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
||||||
self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
||||||
|
@ -210,6 +215,7 @@ class FormattingTests(TestCase):
|
||||||
self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
|
self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
|
||||||
self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
||||||
self.assertEqual(u'100000,0', Template('{{ f|floatformat }}').render(self.ctxt))
|
self.assertEqual(u'100000,0', Template('{{ f|floatformat }}').render(self.ctxt))
|
||||||
|
self.assertEqual(u'10:15:48', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
|
||||||
self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
||||||
self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
|
self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue