Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed 16938 -- Ensured that the active locale's formats take preceden…

…ce over the default settings even if they would be interpreted as False in a conditional test (e.g. 0 or empty string). Thanks to pikerr for the report and initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17017 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1452cecd154c9a77af85e6a5b09e334797779be4 1 parent 51b8f0a
Julien Phalip jphalip authored
Showing with 41 additions and 2 deletions.
  1. +6 −1 django/utils/formats.py
  2. +35 −1 tests/regressiontests/i18n/tests.py
7 django/utils/formats.py
View
@@ -71,7 +71,12 @@ def get_format(format_type, lang=None, use_l10n=None):
lang = get_language()
cache_key = (format_type, lang)
try:
- return _format_cache[cache_key] or getattr(settings, format_type)
+ cached = _format_cache[cache_key]
+ if cached is not None:
+ return cached
+ else:
+ # Return the general setting by default
+ return getattr(settings, format_type)
except KeyError:
for module in get_format_modules(lang):
try:
36 tests/regressiontests/i18n/tests.py
View
@@ -14,7 +14,8 @@
from django.test.utils import override_settings
from django.utils import translation
from django.utils.formats import (get_format, date_format, time_format,
- localize, localize_input, iter_format_modules, get_format_modules)
+ localize, localize_input, iter_format_modules, get_format_modules,
+ number_format)
from django.utils.importlib import import_module
from django.utils.numberformat import format as nformat
from django.utils.safestring import mark_safe, SafeString, SafeUnicode
@@ -397,6 +398,39 @@ def test_l10n_disabled(self):
self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
+ def test_false_like_locale_formats(self):
+ """
+ Ensure that the active locale's formats take precedence over the
+ default settings even if they would be interpreted as False in a
+ conditional test (e.g. 0 or empty string).
+ Refs #16938.
+ """
+ from django.conf.locale.fr import formats as fr_formats
+
+ # Back up original formats
+ backup_THOUSAND_SEPARATOR = fr_formats.THOUSAND_SEPARATOR
+ backup_FIRST_DAY_OF_WEEK = fr_formats.FIRST_DAY_OF_WEEK
+
+ # Set formats that would get interpreted as False in a conditional test
+ fr_formats.THOUSAND_SEPARATOR = ''
+ fr_formats.FIRST_DAY_OF_WEEK = 0
+
+ with translation.override('fr'):
+ with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True,
+ THOUSAND_SEPARATOR='!'):
+ self.assertEqual('', get_format('THOUSAND_SEPARATOR'))
+ # Even a second time (after the format has been cached)...
+ self.assertEqual('', get_format('THOUSAND_SEPARATOR'))
+
+ with self.settings(USE_L10N=True, FIRST_DAY_OF_WEEK=1):
+ self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
+ # Even a second time (after the format has been cached)...
+ self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
+
+ # Restore original formats
+ fr_formats.THOUSAND_SEPARATOR = backup_THOUSAND_SEPARATOR
+ fr_formats.FIRST_DAY_OF_WEEK = backup_FIRST_DAY_OF_WEEK
+
def test_l10n_enabled(self):
settings.USE_L10N = True
# Catalan locale
Please sign in to comment.
Something went wrong with that request. Please try again.