From 9582ba51bd486c5785c0e76ed7f7996119bc9650 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Wed, 25 Nov 2015 12:17:59 +0100 Subject: [PATCH] [1.8.x] Fixed #25812 -- Restored the ability to use custom formats with the date template filter. Backport of 34d88944f46d3e2734488fd0ca3c2c24c15a0264 from master --- AUTHORS | 1 + django/utils/formats.py | 8 +++----- docs/releases/1.8.8.txt | 4 ++++ docs/topics/i18n/formatting.txt | 5 ++++- tests/i18n/other/locale/fr/__init__.py | 0 tests/i18n/other/locale/fr/formats.py | 2 ++ tests/i18n/tests.py | 5 +++++ 7 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 tests/i18n/other/locale/fr/__init__.py create mode 100644 tests/i18n/other/locale/fr/formats.py diff --git a/AUTHORS b/AUTHORS index 63855d76be..06d9882df1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -715,6 +715,7 @@ answer newbie questions, and generally made Django that much better: Wilson Miner wojtek Xia Kai + Yann Fouillat Yann Malet Yasushi Masuda ye7cakf02@sneakemail.com diff --git a/django/utils/formats.py b/django/utils/formats.py index 767871b301..4faf10117b 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -113,8 +113,6 @@ def get_format(format_type, lang=None, use_l10n=None): be localized (or not), overriding the value of settings.USE_L10N. """ format_type = force_str(format_type) - if format_type not in FORMAT_SETTINGS: - return format_type if use_l10n or (use_l10n is None and settings.USE_L10N): if lang is None: lang = get_language() @@ -123,9 +121,6 @@ def get_format(format_type, lang=None, use_l10n=None): 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: @@ -140,6 +135,9 @@ def get_format(format_type, lang=None, use_l10n=None): except AttributeError: pass _format_cache[cache_key] = None + if format_type not in FORMAT_SETTINGS: + return format_type + # Return the general setting by default return getattr(settings, format_type) get_format_lazy = lazy(get_format, six.text_type, list, tuple) diff --git a/docs/releases/1.8.8.txt b/docs/releases/1.8.8.txt index 1a28efe167..94cda25ba2 100644 --- a/docs/releases/1.8.8.txt +++ b/docs/releases/1.8.8.txt @@ -14,3 +14,7 @@ Bugfixes * Corrected ``__len`` query lookup on ``ArrayField`` for empty arrays (:ticket:`25772`). + +* Restored the ability to use custom formats from ``formats.py`` with + ``django.utils.formats.get_format()`` and the ``date`` template filter + (:ticket:`25812`). diff --git a/docs/topics/i18n/formatting.txt b/docs/topics/i18n/formatting.txt index 62e4cc9ede..8a96bd74a9 100644 --- a/docs/topics/i18n/formatting.txt +++ b/docs/topics/i18n/formatting.txt @@ -170,7 +170,10 @@ the package where format files will exist, for instance:: ] Files are not placed directly in this directory, but in a directory named as -the locale, and must be named ``formats.py``. +the locale, and must be named ``formats.py``. Be careful not to put sensitive +information in these files as values inside can be exposed if you pass the +string to ``django.utils.formats.get_format()`` (used by the :tfilter:`date` +template filter). To customize the English formats, a structure like this would be needed:: diff --git a/tests/i18n/other/locale/fr/__init__.py b/tests/i18n/other/locale/fr/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/i18n/other/locale/fr/formats.py b/tests/i18n/other/locale/fr/formats.py new file mode 100644 index 0000000000..d13e245c30 --- /dev/null +++ b/tests/i18n/other/locale/fr/formats.py @@ -0,0 +1,2 @@ +# A user-defined format +CUSTOM_DAY_FORMAT = 'd/m/Y CUSTOM' diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index b06d73179f..29bb2e9cb6 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -930,6 +930,11 @@ class FormattingTests(TestCase): def test_format_arbitrary_settings(self): self.assertEqual(get_format('DEBUG'), 'DEBUG') + def test_get_custom_format(self): + with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'): + with translation.override('fr', deactivate=True): + self.assertEqual('d/m/Y CUSTOM', get_format('CUSTOM_DAY_FORMAT')) + class MiscTests(TestCase):