Fixed #18394 -- Added error for invalid JavaScriptCatalog packages

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2017-04-02 20:14:39 +02:00
parent 41e02ab368
commit 23142eea85
3 changed files with 22 additions and 2 deletions

View File

@ -232,6 +232,11 @@ class JavaScriptCatalog(View):
def get_paths(self, packages): def get_paths(self, packages):
allowable_packages = {app_config.name: app_config for app_config in apps.get_app_configs()} allowable_packages = {app_config.name: app_config for app_config in apps.get_app_configs()}
app_configs = [allowable_packages[p] for p in packages if p in allowable_packages] app_configs = [allowable_packages[p] for p in packages if p in allowable_packages]
if len(app_configs) < len(packages):
excluded = [p for p in packages if p not in allowable_packages]
raise ValueError(
'Invalid package(s) provided to JavaScriptCatalog: %s' % ','.join(excluded)
)
# paths of requested packages # paths of requested packages
return [os.path.join(app.path, 'locale') for app in app_configs] return [os.path.join(app.path, 'locale') for app in app_configs]

View File

@ -420,6 +420,10 @@ Miscellaneous
Book.objects.iterator(chunk_size=100) Book.objects.iterator(chunk_size=100)
* Providing unknown package names in the ``packages`` argument of the
:class:`~django.views.i18n.JavaScriptCatalog` view now raises ``ValueError``
instead of passing silently.
.. _deprecated-features-2.0: .. _deprecated-features-2.0:
Features deprecated in 2.0 Features deprecated in 2.0

View File

@ -4,14 +4,15 @@ from os import path
from django.conf import settings from django.conf import settings
from django.test import ( from django.test import (
SimpleTestCase, TestCase, modify_settings, override_settings, RequestFactory, SimpleTestCase, TestCase, modify_settings,
override_settings,
) )
from django.test.selenium import SeleniumTestCase from django.test.selenium import SeleniumTestCase
from django.urls import reverse from django.urls import reverse
from django.utils.translation import ( from django.utils.translation import (
LANGUAGE_SESSION_KEY, get_language, override, LANGUAGE_SESSION_KEY, get_language, override,
) )
from django.views.i18n import get_formats from django.views.i18n import JavaScriptCatalog, get_formats
from ..urls import locale_dir from ..urls import locale_dir
@ -397,6 +398,16 @@ class I18NViewTests(SimpleTestCase):
response = self.client.get('/jsi18n/') response = self.client.get('/jsi18n/')
self.assertContains(response, 'este texto de app3 debe ser traducido') self.assertContains(response, 'este texto de app3 debe ser traducido')
def test_i18n_unknown_package_error(self):
view = JavaScriptCatalog.as_view()
request = RequestFactory().get('/')
msg = 'Invalid package(s) provided to JavaScriptCatalog: unknown_package'
with self.assertRaisesMessage(ValueError, msg):
view(request, packages='unknown_package')
msg += ',unknown_package2'
with self.assertRaisesMessage(ValueError, msg):
view(request, packages='unknown_package+unknown_package2')
@override_settings(ROOT_URLCONF='view_tests.urls') @override_settings(ROOT_URLCONF='view_tests.urls')
class I18nSeleniumTests(SeleniumTestCase): class I18nSeleniumTests(SeleniumTestCase):