Refs #27795 -- Stopped converting integer format settings to str in JS/JSON i18n views

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2017-04-27 10:50:48 +02:00
parent e3a88c7797
commit d842ada305
5 changed files with 30 additions and 13 deletions

View File

@ -8,7 +8,6 @@ from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.template import Context, Engine
from django.urls import translate_url
from django.utils.encoding import force_text
from django.utils.formats import get_format
from django.utils.http import is_safe_url
from django.utils.translation import (
@ -68,16 +67,7 @@ def get_formats():
'THOUSAND_SEPARATOR', 'NUMBER_GROUPING',
'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
)
result = {}
for attr in FORMAT_SETTINGS:
result[attr] = get_format(attr)
formats = {}
for k, v in result.items():
if isinstance(v, (int, str)):
formats[k] = force_text(v)
elif isinstance(v, (tuple, list)):
formats[k] = [force_text(value) for value in v]
return formats
return {attr: get_format(attr) for attr in FORMAT_SETTINGS}
js_catalog_template = r"""

View File

@ -326,6 +326,9 @@ Miscellaneous
require them. Third-party apps that use these methods may want to adopt a
similar approach.
* The ``FIRST_DAY_OF_WEEK`` and ``NUMBER_GROUPING`` format settings are now
kept as integers in JavaScript and JSON i18n view outputs.
.. _deprecated-features-2.0:
Features deprecated in 2.0

View File

@ -53,9 +53,9 @@
"%m/%d/%y"
],
"DECIMAL_SEPARATOR": ".",
"FIRST_DAY_OF_WEEK": "0",
"FIRST_DAY_OF_WEEK": 0,
"MONTH_DAY_FORMAT": "F j",
"NUMBER_GROUPING": "3",
"NUMBER_GROUPING": 3,
"SHORT_DATETIME_FORMAT": "m/d/Y P",
"SHORT_DATE_FORMAT": "m/d/Y",
"THOUSAND_SEPARATOR": ",",

View File

@ -4,6 +4,14 @@
</head>
<body>
<p id="formats">
<script type="text/javascript">
document.write("DATE_INPUT_FORMATS is an " + typeof get_format("DATE_INPUT_FORMATS") + "; ");
document.write("DECIMAL_SEPARATOR is a " + typeof get_format("DECIMAL_SEPARATOR") + "; ");
document.write("FIRST_DAY_OF_WEEK is a " + typeof get_format("FIRST_DAY_OF_WEEK") + ";");
</script>
</p>
<p id="gettext">
<script type="text/javascript">
document.write(gettext("Remove"));

View File

@ -11,6 +11,7 @@ from django.urls import reverse
from django.utils.translation import (
LANGUAGE_SESSION_KEY, get_language, override,
)
from django.views.i18n import get_formats
from ..urls import locale_dir
@ -183,6 +184,14 @@ class SetLanguageTests(TestCase):
@override_settings(ROOT_URLCONF='view_tests.urls')
class I18NViewTests(SimpleTestCase):
"""Test django.views.i18n views other than set_language."""
@override_settings(LANGUAGE_CODE='de')
def test_get_formats(self):
formats = get_formats()
# Test 3 possible types in get_formats: integer, string, and list.
self.assertEqual(formats['FIRST_DAY_OF_WEEK'], 0)
self.assertEqual(formats['DECIMAL_SEPARATOR'], '.')
self.assertEqual(formats['TIME_INPUT_FORMATS'], ['%H:%M:%S', '%H:%M:%S.%f', '%H:%M'])
def test_jsi18n(self):
"""The javascript_catalog can be deployed with language settings"""
for lang_code in ['es', 'fr', 'ru']:
@ -214,6 +223,8 @@ class I18NViewTests(SimpleTestCase):
data = json.loads(response.content.decode())
self.assertIn('catalog', data)
self.assertIn('formats', data)
self.assertEqual(data['formats']['TIME_INPUT_FORMATS'], ['%H:%M:%S', '%H:%M:%S.%f', '%H:%M'])
self.assertEqual(data['formats']['FIRST_DAY_OF_WEEK'], 0)
self.assertIn('plural', data)
self.assertEqual(data['catalog']['month name\x04May'], 'Mai')
self.assertIn('DATETIME_FORMAT', data['formats'])
@ -398,6 +409,11 @@ class I18nSeleniumTests(SeleniumTestCase):
self.assertEqual(elem.text, "1 Resultat")
elem = self.selenium.find_element_by_id("npgettext_plur")
self.assertEqual(elem.text, "455 Resultate")
elem = self.selenium.find_element_by_id("formats")
self.assertEqual(
elem.text,
"DATE_INPUT_FORMATS is an object; DECIMAL_SEPARATOR is a string; FIRST_DAY_OF_WEEK is a number;"
)
@modify_settings(INSTALLED_APPS={'append': ['view_tests.app1', 'view_tests.app2']})
@override_settings(LANGUAGE_CODE='fr')