Переглянути джерело

Fixed #18394 -- Added error for invalid JavaScriptCatalog packages

Thanks Tim Graham for the review.
Claude Paroz 8 роки тому
батько
коміт
23142eea85
3 змінених файлів з 22 додано та 2 видалено
  1. 5 0
      django/views/i18n.py
  2. 4 0
      docs/releases/2.0.txt
  3. 13 2
      tests/view_tests/tests/test_i18n.py

+ 5 - 0
django/views/i18n.py

@@ -232,6 +232,11 @@ class JavaScriptCatalog(View):
     def get_paths(self, packages):
         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]
+        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
         return [os.path.join(app.path, 'locale') for app in app_configs]
 

+ 4 - 0
docs/releases/2.0.txt

@@ -420,6 +420,10 @@ Miscellaneous
 
       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:
 
 Features deprecated in 2.0

+ 13 - 2
tests/view_tests/tests/test_i18n.py

@@ -4,14 +4,15 @@ from os import path
 
 from django.conf import settings
 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.urls import reverse
 from django.utils.translation import (
     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
 
@@ -397,6 +398,16 @@ class I18NViewTests(SimpleTestCase):
                 response = self.client.get('/jsi18n/')
                 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')
 class I18nSeleniumTests(SeleniumTestCase):