Commit 1452cecd authored by Julien Phalip's avatar Julien Phalip
Browse files

Fixed 16938 -- Ensured that the active locale's formats take precedence over...

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
parent 51b8f0a2
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -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:
+35 −1
Original line number Diff line number Diff line
@@ -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