Stopped iterating on INSTALLED_APPS.

Used the app cache's get_app_configs() method instead.
This commit is contained in:
Aymeric Augustin 2013-12-19 15:57:23 +01:00
parent d4733b6df0
commit 65cd74be8e
18 changed files with 116 additions and 82 deletions

View File

@ -2,6 +2,7 @@ from collections import OrderedDict
import os import os
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import default_storage, Storage, FileSystemStorage from django.core.files.storage import default_storage, Storage, FileSystemStorage
from django.utils.functional import empty, LazyObject from django.utils.functional import empty, LazyObject
@ -116,10 +117,11 @@ class AppDirectoriesFinder(BaseFinder):
def __init__(self, apps=None, *args, **kwargs): def __init__(self, apps=None, *args, **kwargs):
# The list of apps that are handled # The list of apps that are handled
self.apps = [] self.apps = []
# Mapping of app module paths to storage instances # Mapping of app names to storage instances
self.storages = OrderedDict() self.storages = OrderedDict()
if apps is None: if apps is None:
apps = settings.INSTALLED_APPS app_configs = app_cache.get_app_configs()
apps = [app_config.name for app_config in app_configs]
for app in apps: for app in apps:
app_storage = self.storage_class(app) app_storage = self.storage_class(app)
if os.path.isdir(app_storage.location): if os.path.isdir(app_storage.location):

View File

@ -5,6 +5,7 @@ from optparse import OptionParser, NO_DEFAULT
import os import os
import sys import sys
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError, handle_default_options from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.management.color import color_style from django.core.management.color import color_style
@ -106,13 +107,19 @@ def get_commands():
_commands = dict((name, 'django.core') for name in find_commands(__path__[0])) _commands = dict((name, 'django.core') for name in find_commands(__path__[0]))
# Find the installed apps # Find the installed apps
from django.conf import settings
try: try:
apps = settings.INSTALLED_APPS settings.INSTALLED_APPS
except ImproperlyConfigured: except ImproperlyConfigured:
# Still useful for commands that do not require functional settings, # Still useful for commands that do not require functional
# like startproject or help # settings, like startproject or help.
apps = [] apps = []
else:
# Populate the app cache outside of the try/except block to avoid
# catching ImproperlyConfigured errors that aren't caused by the
# absence of a settings module.
from django.core.apps import app_cache
app_configs = app_cache.get_app_configs()
apps = [app_config.name for app_config in app_configs]
# Find and load the management module for each installed app. # Find and load the management module for each installed app.
for app_name in apps: for app_name in apps:
@ -339,9 +346,10 @@ class ManagementUtility(object):
elif cwords[0] in ('dumpdata', 'sql', 'sqlall', 'sqlclear', elif cwords[0] in ('dumpdata', 'sql', 'sqlall', 'sqlclear',
'sqlcustom', 'sqlindexes', 'sqlsequencereset', 'test'): 'sqlcustom', 'sqlindexes', 'sqlsequencereset', 'test'):
try: try:
from django.conf import settings from django.core.apps import app_cache
app_configs = app_cache.get_app_configs()
# Get the last part of the dotted path as the app name. # Get the last part of the dotted path as the app name.
options += [(a.split('.')[-1], 0) for a in settings.INSTALLED_APPS] options += [(app_config.label, 0) for app_config in app_configs]
except ImportError: except ImportError:
# Fail silently if DJANGO_SETTINGS_MODULE isn't set. The # Fail silently if DJANGO_SETTINGS_MODULE isn't set. The
# user will find out once they execute the command. # user will find out once they execute the command.

View File

@ -2,7 +2,6 @@ import sys
from importlib import import_module from importlib import import_module
from optparse import make_option from optparse import make_option
from django.conf import settings
from django.core.apps import app_cache from django.core.apps import app_cache
from django.db import connections, router, transaction, DEFAULT_DB_ALIAS from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
from django.core.management import call_command from django.core.management import call_command
@ -42,9 +41,9 @@ class Command(NoArgsCommand):
# Import the 'management' module within each installed app, to register # Import the 'management' module within each installed app, to register
# dispatcher events. # dispatcher events.
for app_name in settings.INSTALLED_APPS: for app_config in app_cache.get_app_configs():
try: try:
import_module('.management', app_name) import_module('.management', app_config.name)
except ImportError: except ImportError:
pass pass

View File

@ -6,7 +6,6 @@ from importlib import import_module
import itertools import itertools
import traceback import traceback
from django.conf import settings
from django.core.apps import app_cache from django.core.apps import app_cache
from django.core.management import call_command from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
@ -47,9 +46,9 @@ class Command(BaseCommand):
# Import the 'management' module within each installed app, to register # Import the 'management' module within each installed app, to register
# dispatcher events. # dispatcher events.
for app_name in settings.INSTALLED_APPS: for app_config in app_cache.get_app_configs():
if module_has_submodule(import_module(app_name), "management"): if module_has_submodule(app_config.app_module, "management"):
import_module('.management', app_name) import_module('.management', app_config.name)
# Get the database we're operating from # Get the database we're operating from
db = options.get('database') db = options.get('database')

View File

@ -6,6 +6,7 @@ from importlib import import_module
from inspect import getargspec, getcallargs from inspect import getargspec, getcallargs
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility
ContextPopException) ContextPopException)
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
@ -1302,9 +1303,12 @@ def get_templatetags_modules():
# Populate list once per process. Mutate the local list first, and # Populate list once per process. Mutate the local list first, and
# then assign it to the global name to ensure there are no cases where # then assign it to the global name to ensure there are no cases where
# two threads try to populate it simultaneously. # two threads try to populate it simultaneously.
for app_module in ['django'] + list(settings.INSTALLED_APPS):
templatetags_modules_candidates = ['django.templatetags']
templatetags_modules_candidates += ['%s.templatetags' % app_config.name
for app_config in app_cache.get_app_configs()]
for templatetag_module in templatetags_modules_candidates:
try: try:
templatetag_module = '%s.templatetags' % app_module
import_module(templatetag_module) import_module(templatetag_module)
_templatetags_modules.append(templatetag_module) _templatetags_modules.append(templatetag_module)
except ImportError: except ImportError:

View File

@ -3,12 +3,11 @@ Wrapper for loading templates from "templates" directories in INSTALLED_APPS
packages. packages.
""" """
from importlib import import_module
import os import os
import sys import sys
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.apps import app_cache
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader from django.template.loader import BaseLoader
from django.utils._os import safe_join from django.utils._os import safe_join
@ -18,12 +17,8 @@ from django.utils import six
if six.PY2: if six.PY2:
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
app_template_dirs = [] app_template_dirs = []
for app in settings.INSTALLED_APPS: for app_config in app_cache.get_app_configs():
try: template_dir = os.path.join(app_config.path, 'templates')
mod = import_module(app)
except ImportError as e:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
if os.path.isdir(template_dir): if os.path.isdir(template_dir):
if six.PY2: if six.PY2:
template_dir = template_dir.decode(fs_encoding) template_dir = template_dir.decode(fs_encoding)

View File

@ -7,6 +7,7 @@ except ImportError:
resource_string = None resource_string = None
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader from django.template.loader import BaseLoader
from django.utils import six from django.utils import six
@ -23,12 +24,12 @@ class Loader(BaseLoader):
""" """
if resource_string is not None: if resource_string is not None:
pkg_name = 'templates/' + template_name pkg_name = 'templates/' + template_name
for app in settings.INSTALLED_APPS: for app_config in app_cache.get_app_configs():
try: try:
resource = resource_string(app, pkg_name) resource = resource_string(app_config.name, pkg_name)
except Exception: except Exception:
continue continue
if six.PY2: if six.PY2:
resource = resource.decode(settings.FILE_CHARSET) resource = resource.decode(settings.FILE_CHARSET)
return (resource, 'egg:%s:%s' % (app, pkg_name)) return (resource, 'egg:%s:%s' % (app_config.name, pkg_name))
raise TemplateDoesNotExist(template_name) raise TemplateDoesNotExist(template_name)

View File

@ -37,9 +37,8 @@ import time
import traceback import traceback
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.core.signals import request_finished from django.core.signals import request_finished
from django.utils._os import upath
from importlib import import_module
try: try:
from django.utils.six.moves import _thread as thread from django.utils.six.moves import _thread as thread
except ImportError: except ImportError:
@ -91,10 +90,8 @@ def gen_filenames():
basedirs = [os.path.join(os.path.dirname(os.path.dirname(__file__)), basedirs = [os.path.join(os.path.dirname(os.path.dirname(__file__)),
'conf', 'locale'), 'conf', 'locale'),
'locale'] 'locale']
for appname in reversed(settings.INSTALLED_APPS): for app_config in reversed(list(app_cache.get_app_configs())):
app = import_module(appname) basedirs.append(os.path.join(app_config.path, 'locale'))
basedirs.append(os.path.join(os.path.dirname(upath(app.__file__)),
'locale'))
basedirs.extend(settings.LOCALE_PATHS) basedirs.extend(settings.LOCALE_PATHS)
basedirs = [os.path.abspath(basedir) for basedir in basedirs basedirs = [os.path.abspath(basedir) for basedir in basedirs
if os.path.isdir(basedir)] if os.path.isdir(basedir)]

View File

@ -58,18 +58,17 @@ def autodiscover_modules(*args, **kwargs):
registry. This register_to object must have a _registry instance variable registry. This register_to object must have a _registry instance variable
to access it. to access it.
""" """
from django.conf import settings from django.core.apps import app_cache
register_to = kwargs.get('register_to') register_to = kwargs.get('register_to')
for app in settings.INSTALLED_APPS: for app_config in app_cache.get_app_configs():
mod = import_module(app)
# Attempt to import the app's module. # Attempt to import the app's module.
try: try:
if register_to: if register_to:
before_import_registry = copy.copy(register_to._registry) before_import_registry = copy.copy(register_to._registry)
for module_to_search in args: for module_to_search in args:
import_module('%s.%s' % (app, module_to_search)) import_module('%s.%s' % (app_config.name, module_to_search))
except: except:
# Reset the model registry to the state before the last import as # Reset the model registry to the state before the last import as
# this import will have to reoccur on the next request and this # this import will have to reoccur on the next request and this
@ -81,7 +80,7 @@ def autodiscover_modules(*args, **kwargs):
# Decide whether to bubble up this error. If the app just # Decide whether to bubble up this error. If the app just
# doesn't have an admin module, we can ignore the error # doesn't have an admin module, we can ignore the error
# attempting to import it, otherwise we want it to bubble up. # attempting to import it, otherwise we want it to bubble up.
if module_has_submodule(mod, module_to_search): if module_has_submodule(app_config.app_module, module_to_search):
raise raise

View File

@ -7,10 +7,10 @@ import os
import re import re
import sys import sys
import gettext as gettext_module import gettext as gettext_module
from importlib import import_module
from threading import local from threading import local
import warnings import warnings
from django.core.apps import app_cache
from django.dispatch import receiver from django.dispatch import receiver
from django.test.signals import setting_changed from django.test.signals import setting_changed
from django.utils.encoding import force_str, force_text from django.utils.encoding import force_str, force_text
@ -179,10 +179,8 @@ def translation(language):
res.merge(t) res.merge(t)
return res return res
for appname in reversed(settings.INSTALLED_APPS): for app_config in reversed(list(app_cache.get_app_configs())):
app = import_module(appname) apppath = os.path.join(app_config.path, 'locale')
apppath = os.path.join(os.path.dirname(upath(app.__file__)), 'locale')
if os.path.isdir(apppath): if os.path.isdir(apppath):
res = _merge(apppath) res = _merge(apppath)

View File

@ -5,6 +5,7 @@ import gettext as gettext_module
from django import http from django import http
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.template import Context, Template from django.template import Context, Template
from django.utils.translation import check_for_language, to_locale, get_language from django.utils.translation import check_for_language, to_locale, get_language
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
@ -187,7 +188,10 @@ def render_javascript_catalog(catalog=None, plural=None):
def get_javascript_catalog(locale, domain, packages): def get_javascript_catalog(locale, domain, packages):
default_locale = to_locale(settings.LANGUAGE_CODE) default_locale = to_locale(settings.LANGUAGE_CODE)
packages = [p for p in packages if p == 'django.conf' or p in settings.INSTALLED_APPS] app_configs = app_cache.get_app_configs()
allowable_packages = set(app_config.name for app_config in app_configs)
allowable_packages.add('django.conf')
packages = [p for p in packages if p in allowable_packages]
t = {} t = {}
paths = [] paths = []
en_selected = locale.startswith('en') en_selected = locale.startswith('en')

View File

@ -5,7 +5,7 @@ import os
import sys import sys
import unittest import unittest
from django.conf import settings from django.core.apps import app_cache
from django.core.management import ManagementUtility from django.core.management import ManagementUtility
from django.utils.six import StringIO from django.utils.six import StringIO
@ -84,5 +84,7 @@ class BashCompletionTests(unittest.TestCase):
"Application names will be autocompleted for an AppCommand" "Application names will be autocompleted for an AppCommand"
self._user_input('django-admin.py sqlall a') self._user_input('django-admin.py sqlall a')
output = self._run_autocomplete() output = self._run_autocomplete()
app_labels = [name.split('.')[-1] for name in settings.INSTALLED_APPS] a_labels = sorted(app_config.label
self.assertEqual(output, sorted(label for label in app_labels if label.startswith('a'))) for app_config in app_cache.get_app_configs()
if app_config.label.startswith('a'))
self.assertEqual(output, a_labels)

View File

@ -9,6 +9,7 @@ import pickle
from threading import local from threading import local
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.template import Template, Context from django.template import Template, Context
from django.template.base import TemplateSyntaxError from django.template.base import TemplateSyntaxError
from django.test import TestCase, RequestFactory from django.test import TestCase, RequestFactory
@ -1035,11 +1036,29 @@ class ResolutionOrderI18NTests(TransRealMixin, TestCase):
"translation of '%s'; the actual result is '%s'." % (msgstr, msgid, result))) "translation of '%s'; the actual result is '%s'." % (msgstr, msgid, result)))
@override_settings(INSTALLED_APPS=['i18n.resolution'] + list(settings.INSTALLED_APPS))
class AppResolutionOrderI18NTests(ResolutionOrderI18NTests): class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
def test_app_translation(self): def test_app_translation(self):
self.assertUgettext('Date/time', 'APP') # This test relies on an implementation detail, namely the fact that
# _with_app adds the app at the list. Adjust the test if this changes.
# Original translation.
self.assertUgettext('Date/time', 'Datum/Zeit')
# Different translation.
with app_cache._with_app('i18n.resolution'):
self.flush_caches()
activate('de')
# Doesn't work because it's added later in the list.
self.assertUgettext('Date/time', 'Datum/Zeit')
with app_cache._without_app('admin'):
self.flush_caches()
activate('de')
# Unless the original is removed from the list.
self.assertUgettext('Date/time', 'Datum/Zeit (APP)')
@override_settings(LOCALE_PATHS=extended_locale_paths) @override_settings(LOCALE_PATHS=extended_locale_paths)
@ -1049,8 +1068,7 @@ class LocalePathsResolutionOrderI18NTests(ResolutionOrderI18NTests):
self.assertUgettext('Time', 'LOCALE_PATHS') self.assertUgettext('Time', 'LOCALE_PATHS')
def test_locale_paths_override_app_translation(self): def test_locale_paths_override_app_translation(self):
extended_apps = list(settings.INSTALLED_APPS) + ['i18n.resolution'] with app_cache._with_app('i18n.resolution'):
with self.settings(INSTALLED_APPS=extended_apps):
self.assertUgettext('Time', 'LOCALE_PATHS') self.assertUgettext('Time', 'LOCALE_PATHS')

View File

@ -86,6 +86,9 @@ class StaticLiveServerChecks(LiveServerBase):
class StaticLiveServerView(LiveServerBase): class StaticLiveServerView(LiveServerBase):
# The test is going to access a static file stored in this application.
available_apps = ['staticfiles_tests.apps.test']
def urlopen(self, url): def urlopen(self, url):
return urlopen(self.live_server_url + url) return urlopen(self.live_server_url + url)

View File

@ -20,6 +20,7 @@ except ImportError:
pkg_resources = None pkg_resources = None
from django.core.apps import app_cache
from django.template import TemplateDoesNotExist, Context from django.template import TemplateDoesNotExist, Context
from django.template.loaders.eggs import Loader as EggLoader from django.template.loaders.eggs import Loader as EggLoader
from django.template import loader from django.template import loader
@ -49,7 +50,6 @@ def create_egg(name, resources):
@unittest.skipUnless(pkg_resources, 'setuptools is not installed') @unittest.skipUnless(pkg_resources, 'setuptools is not installed')
@override_settings(INSTALLED_APPS=[])
class EggLoaderTest(TestCase): class EggLoaderTest(TestCase):
def setUp(self): def setUp(self):
# Defined here b/c at module scope we may not have pkg_resources # Defined here b/c at module scope we may not have pkg_resources
@ -78,29 +78,28 @@ class EggLoaderTest(TestCase):
os.path.normcase('templates/x.txt'): StringIO("x"), os.path.normcase('templates/x.txt'): StringIO("x"),
}) })
@override_settings(INSTALLED_APPS=['egg_empty'])
def test_empty(self): def test_empty(self):
"Loading any template on an empty egg should fail" "Loading any template on an empty egg should fail"
with app_cache._with_app('egg_empty'):
egg_loader = EggLoader() egg_loader = EggLoader()
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
@override_settings(INSTALLED_APPS=['egg_1'])
def test_non_existing(self): def test_non_existing(self):
"Template loading fails if the template is not in the egg" "Template loading fails if the template is not in the egg"
with app_cache._with_app('egg_1'):
egg_loader = EggLoader() egg_loader = EggLoader()
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
@override_settings(INSTALLED_APPS=['egg_1'])
def test_existing(self): def test_existing(self):
"A template can be loaded from an egg" "A template can be loaded from an egg"
with app_cache._with_app('egg_1'):
egg_loader = EggLoader() egg_loader = EggLoader()
contents, template_name = egg_loader.load_template_source("y.html") contents, template_name = egg_loader.load_template_source("y.html")
self.assertEqual(contents, "y") self.assertEqual(contents, "y")
self.assertEqual(template_name, "egg:egg_1:templates/y.html") self.assertEqual(template_name, "egg:egg_1:templates/y.html")
@override_settings(INSTALLED_APPS=[])
def test_not_installed(self): def test_not_installed(self):
"Loading an existent template from an egg not included in INSTALLED_APPS should fail" "Loading an existent template from an egg not included in any app should fail"
egg_loader = EggLoader() egg_loader = EggLoader()
self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html") self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")

View File

@ -16,6 +16,7 @@ import unittest
import warnings import warnings
from django import template from django import template
from django.core.apps import app_cache
from django.core import urlresolvers from django.core import urlresolvers
from django.template import (base as template_base, loader, Context, from django.template import (base as template_base, loader, Context,
RequestContext, Template, TemplateSyntaxError) RequestContext, Template, TemplateSyntaxError)
@ -1873,23 +1874,25 @@ class TemplateTagLoading(TestCase):
self.assertTrue('ImportError' in e.args[0]) self.assertTrue('ImportError' in e.args[0])
self.assertTrue('Xtemplate' in e.args[0]) self.assertTrue('Xtemplate' in e.args[0])
@override_settings(INSTALLED_APPS=('tagsegg',))
def test_load_error_egg(self): def test_load_error_egg(self):
ttext = "{% load broken_egg %}" ttext = "{% load broken_egg %}"
egg_name = '%s/tagsegg.egg' % self.egg_dir egg_name = '%s/tagsegg.egg' % self.egg_dir
sys.path.append(egg_name) sys.path.append(egg_name)
self.assertRaises(template.TemplateSyntaxError, template.Template, ttext) with self.assertRaises(template.TemplateSyntaxError):
with app_cache._with_app('tagsegg'):
template.Template(ttext)
try: try:
with app_cache._with_app('tagsegg'):
template.Template(ttext) template.Template(ttext)
except template.TemplateSyntaxError as e: except template.TemplateSyntaxError as e:
self.assertTrue('ImportError' in e.args[0]) self.assertTrue('ImportError' in e.args[0])
self.assertTrue('Xtemplate' in e.args[0]) self.assertTrue('Xtemplate' in e.args[0])
@override_settings(INSTALLED_APPS=('tagsegg',))
def test_load_working_egg(self): def test_load_working_egg(self):
ttext = "{% load working_egg %}" ttext = "{% load working_egg %}"
egg_name = '%s/tagsegg.egg' % self.egg_dir egg_name = '%s/tagsegg.egg' % self.egg_dir
sys.path.append(egg_name) sys.path.append(egg_name)
with app_cache._with_app('tagsegg'):
template.Template(ttext) template.Template(ttext)

View File

@ -5,9 +5,9 @@ import sys
import unittest import unittest
from zipimport import zipimporter from zipimport import zipimporter
from django.core.apps import app_cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test.utils import override_settings
from django.utils import six from django.utils import six
from django.utils.module_loading import autodiscover_modules, import_by_path, module_has_submodule from django.utils.module_loading import autodiscover_modules, import_by_path, module_has_submodule
from django.utils._os import upath from django.utils._os import upath
@ -135,9 +135,14 @@ class ModuleImportTestCase(unittest.TestCase):
'Should have more than the calling frame in the traceback.') 'Should have more than the calling frame in the traceback.')
@override_settings(INSTALLED_APPS=('utils_tests.test_module',))
class AutodiscoverModulesTestCase(SimpleTestCase): class AutodiscoverModulesTestCase(SimpleTestCase):
def setUp(self):
self._with_test_module = app_cache._begin_with_app('utils_tests.test_module')
def tearDown(self):
app_cache._end_with_app(self._with_test_module)
def test_autodiscover_modules_found(self): def test_autodiscover_modules_found(self):
autodiscover_modules('good_module') autodiscover_modules('good_module')

View File

@ -5,6 +5,7 @@ from os import path
import unittest import unittest
from django.conf import settings from django.conf import settings
from django.core.apps import app_cache
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.test import LiveServerTestCase, TestCase from django.test import LiveServerTestCase, TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
@ -115,9 +116,8 @@ class JsI18NTests(TestCase):
available. The Javascript i18n view must return a NON empty language catalog available. The Javascript i18n view must return a NON empty language catalog
with the proper English translations. See #13726 for more details. with the proper English translations. See #13726 for more details.
""" """
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app0'] with app_cache._with_app('view_tests.app0'):
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps): with self.settings(LANGUAGE_CODE='fr'), override('en-us'):
with override('en-us'):
response = self.client.get('/views/jsi18n_english_translation/') response = self.client.get('/views/jsi18n_english_translation/')
self.assertContains(response, javascript_quote('this app0 string is to be translated')) self.assertContains(response, javascript_quote('this app0 string is to be translated'))
@ -144,9 +144,8 @@ class JsI18NTestsMultiPackage(TestCase):
translations of multiple Python packages is requested. See #13388, translations of multiple Python packages is requested. See #13388,
#3594 and #13514 for more details. #3594 and #13514 for more details.
""" """
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app1', 'view_tests.app2'] with app_cache._with_app('view_tests.app1'), app_cache._with_app('view_tests.app2'):
with self.settings(LANGUAGE_CODE='en-us', INSTALLED_APPS=extended_apps): with self.settings(LANGUAGE_CODE='en-us'), override('fr'):
with override('fr'):
response = self.client.get('/views/jsi18n_multi_packages1/') response = self.client.get('/views/jsi18n_multi_packages1/')
self.assertContains(response, javascript_quote('il faut traduire cette chaîne de caractères de app1')) self.assertContains(response, javascript_quote('il faut traduire cette chaîne de caractères de app1'))
@ -155,9 +154,8 @@ class JsI18NTestsMultiPackage(TestCase):
Similar to above but with neither default or requested language being Similar to above but with neither default or requested language being
English. English.
""" """
extended_apps = list(settings.INSTALLED_APPS) + ['view_tests.app3', 'view_tests.app4'] with app_cache._with_app('view_tests.app3'), app_cache._with_app('view_tests.app4'):
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps): with self.settings(LANGUAGE_CODE='fr'), override('es-ar'):
with override('es-ar'):
response = self.client.get('/views/jsi18n_multi_packages2/') response = self.client.get('/views/jsi18n_multi_packages2/')
self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido')) self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido'))