1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Added get_app_config() to look up app configs by label.

Refactored get_app() to rely on that method.

get_app() starts by calling _populate(), which goes through
INSTALLED_APPS and, for each app, imports the app module and attempts to
import the models module. At this point, no further imports are
necessary to return the models module for a  given app. Therefore, the
implementation of get_app() can be simplified and the safeguards for
race conditions can be removed.

Besides, the emptyOK parameter isn't used anywhere in Django. It was
introduced in d6c95e93 but not actually used nor documented, and it has
just been carried around since then. Since it's an obscure private API,
it's acceptable to stop supporting it without a deprecation path. This
branch aims at providing first-class support for applications without a
models module eventually.

For backwards-compatibility, get_app() still raises ImproperlyConfigured
when an app isn't found, even though LookupError is technically more
correct. I haven't gone as far as to preserve the exact error messages.
I've adjusted a few tests instead.
This commit is contained in:
Aymeric Augustin 2013-12-13 12:02:54 +01:00
parent c5eac3a2f6
commit a4cb140004
3 changed files with 34 additions and 25 deletions

View File

@ -153,6 +153,26 @@ class BaseAppCache(object):
""" """
return self.loaded return self.loaded
def get_app_config(self, app_label, only_installed=True):
"""
Returns the application configuration for the given app_label.
Raises LookupError if no application exists with this app_label.
Raises UnavailableApp when set_available_apps() disables the
application with this app_label.
If only_installed is True (default), only applications explicitly
listed in INSTALLED_APPS are considered.
"""
self._populate()
app_config = self.app_configs.get(app_label)
if app_config is None or (only_installed and not app_config.installed):
raise LookupError("No app with label %r." % app_label)
if self.available_apps is not None and app_config.label not in self.available_apps:
raise UnavailableApp("App with label %r isn't available." % app_label)
return app_config
def get_apps(self): def get_apps(self):
""" """
Returns a list of all installed modules that contain models. Returns a list of all installed modules that contain models.
@ -197,29 +217,18 @@ class BaseAppCache(object):
app_paths.append(self._get_app_path(app)) app_paths.append(self._get_app_path(app))
return app_paths return app_paths
def get_app(self, app_label, emptyOK=False): def get_app(self, app_label):
""" """
Returns the module containing the models for the given app_label. Returns the module containing the models for the given app_label.
Returns None if the app has no models in it and emptyOK is True.
Raises UnavailableApp when set_available_apps() in in effect and Raises UnavailableApp when set_available_apps() in in effect and
doesn't include app_label. doesn't include app_label.
""" """
self._populate()
imp.acquire_lock()
try: try:
for app_name in settings.INSTALLED_APPS: return self.get_app_config(app_label).models_module
if app_label == app_name.split('.')[-1]: except LookupError as exc:
mod = self.load_app(app_name, False) # Change the exception type for backwards compatibility.
if mod is None and not emptyOK: raise ImproperlyConfigured(*exc.args)
raise ImproperlyConfigured("App with label %s is missing a models.py module." % app_label)
if self.available_apps is not None and app_label not in self.available_apps:
raise UnavailableApp("App with label %s isn't available." % app_label)
return mod
raise ImproperlyConfigured("App with label %s could not be found" % app_label)
finally:
imp.release_lock()
def get_models(self, app_mod=None, def get_models(self, app_mod=None,
include_auto_created=False, include_deferred=False, include_auto_created=False, include_deferred=False,

View File

@ -379,14 +379,14 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=test_project.settings', 'admin_scripts'] args = ['sqlall', '--settings=test_project.settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_environment(self): def test_builtin_with_environment(self):
"minimal: django-admin builtin commands fail if settings are provided in the environment" "minimal: django-admin builtin commands fail if settings are provided in the environment"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'test_project.settings') out, err = self.run_django_admin(args, 'test_project.settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_bad_settings(self): def test_builtin_with_bad_settings(self):
"minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist" "minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
@ -815,21 +815,21 @@ class ManageMinimalSettings(AdminScriptTestCase):
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"minimal: manage.py builtin commands fail if settings are provided as argument" "minimal: manage.py builtin commands fail if settings are provided as argument"
args = ['sqlall', '--settings=test_project.settings', 'admin_scripts'] args = ['sqlall', '--settings=test_project.settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_environment(self): def test_builtin_with_environment(self):
"minimal: manage.py builtin commands fail if settings are provided in the environment" "minimal: manage.py builtin commands fail if settings are provided in the environment"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args, 'test_project.settings') out, err = self.run_manage(args, 'test_project.settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_bad_settings(self): def test_builtin_with_bad_settings(self):
"minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist" "minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
@ -964,7 +964,7 @@ class ManageMultipleSettings(AdminScriptTestCase):
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found.') self.assertOutput(err, "No app with label 'admin_scripts'.")
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"multiple: manage.py builtin commands succeed if settings are provided as argument" "multiple: manage.py builtin commands succeed if settings are provided as argument"
@ -1442,13 +1442,13 @@ class CommandTypes(AdminScriptTestCase):
"User AppCommands can execute when a single app name is provided" "User AppCommands can execute when a single app name is provided"
args = ['app_command', 'NOT_AN_APP'] args = ['app_command', 'NOT_AN_APP']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertOutput(err, "App with label NOT_AN_APP could not be found") self.assertOutput(err, "No app with label 'NOT_AN_APP'.")
def test_app_command_some_invalid_appnames(self): def test_app_command_some_invalid_appnames(self):
"User AppCommands can execute when some of the provided app names are invalid" "User AppCommands can execute when some of the provided app names are invalid"
args = ['app_command', 'auth', 'NOT_AN_APP'] args = ['app_command', 'auth', 'NOT_AN_APP']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertOutput(err, "App with label NOT_AN_APP could not be found") self.assertOutput(err, "No app with label 'NOT_AN_APP'.")
def test_label_command(self): def test_label_command(self):
"User LabelCommands can execute when a label is provided" "User LabelCommands can execute when a label is provided"

View File

@ -32,5 +32,5 @@ class NoModelTests(TestCase):
@override_settings(INSTALLED_APPS=("empty.no_models",)) @override_settings(INSTALLED_APPS=("empty.no_models",))
def test_no_models(self): def test_no_models(self):
with six.assertRaisesRegex(self, ImproperlyConfigured, with six.assertRaisesRegex(self, ImproperlyConfigured,
'App with label no_models is missing a models.py module.'): "No app with label 'no_models'."):
app_cache.get_app('no_models') app_cache.get_app('no_models')