From 1452cecd154c9a77af85e6a5b09e334797779be4 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 20 Oct 2011 12:14:06 +0000 Subject: [PATCH] Fixed 16938 -- Ensured 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). 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 --- django/utils/formats.py | 7 +++++- tests/regressiontests/i18n/tests.py | 36 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/django/utils/formats.py b/django/utils/formats.py index 3babccb0a0..e283490b17 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -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: diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index bfed0ec882..17395ffb24 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -14,7 +14,8 @@ from django.test import TestCase, RequestFactory 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 @@ class FormattingTests(TestCase): 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