Made the AppConfig API marginally more consistent.

Eliminated the app_ prefix that was more confusing than useful.
This commit is contained in:
Aymeric Augustin 2013-12-26 18:40:28 +01:00
parent fec5330c79
commit ce1bc2c94b
6 changed files with 15 additions and 12 deletions

View File

@ -13,13 +13,13 @@ class AppConfig(object):
Class representing a Django application and its configuration. Class representing a Django application and its configuration.
""" """
def __init__(self, app_name): def __init__(self, app_name, app_module):
# Full Python path to the application eg. 'django.contrib.admin'. # Full Python path to the application eg. 'django.contrib.admin'.
self.name = app_name self.name = app_name
# Root module for the application eg. <module 'django.contrib.admin' # Root module for the application eg. <module 'django.contrib.admin'
# from 'django/contrib/admin/__init__.pyc'>. # from 'django/contrib/admin/__init__.pyc'>.
self.app_module = import_module(app_name) self.module = app_module
# The following attributes could be defined at the class level in a # The following attributes could be defined at the class level in a
# subclass, hence the test-and-set pattern. # subclass, hence the test-and-set pattern.
@ -39,7 +39,7 @@ class AppConfig(object):
# egg. Otherwise it's a unicode on Python 2 and a str on Python 3. # egg. Otherwise it's a unicode on Python 2 and a str on Python 3.
if not hasattr(self, 'path'): if not hasattr(self, 'path'):
try: try:
self.path = upath(self.app_module.__path__[0]) self.path = upath(app_module.__path__[0])
except AttributeError: except AttributeError:
self.path = None self.path = None
@ -63,7 +63,7 @@ class AppConfig(object):
try: try:
# If import_module succeeds, entry is a path to an app module. # If import_module succeeds, entry is a path to an app module.
# Otherwise, entry is a path to an app config class or an error. # Otherwise, entry is a path to an app config class or an error.
import_module(entry) module = import_module(entry)
except ImportError: except ImportError:
# Raise the original exception when entry cannot be a path to an # Raise the original exception when entry cannot be a path to an
@ -88,12 +88,15 @@ class AppConfig(object):
raise ImproperlyConfigured( raise ImproperlyConfigured(
"%r must supply a name attribute." % entry) "%r must supply a name attribute." % entry)
# Ensure app_names points to a valid module.
app_module = import_module(app_name)
# Entry is a path to an app config class. # Entry is a path to an app config class.
return cls(app_name) return cls(app_name, app_module)
else: else:
# Entry is a path to an app module. # Entry is a path to an app module.
return cls(entry) return cls(entry, module)
def import_models(self, all_models): def import_models(self, all_models):
# Dictionary of models for this app, primarily maintained in the # Dictionary of models for this app, primarily maintained in the
@ -102,6 +105,6 @@ class AppConfig(object):
# imported, which may happen before populate_models() runs. # imported, which may happen before populate_models() runs.
self.models = all_models self.models = all_models
if module_has_submodule(self.app_module, MODELS_MODULE_NAME): if module_has_submodule(self.module, MODELS_MODULE_NAME):
models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME) models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
self.models_module = import_module(models_module_name) self.models_module = import_module(models_module_name)

View File

@ -20,7 +20,7 @@ def get_comment_app():
except LookupError: except LookupError:
raise ImproperlyConfigured("The COMMENTS_APP (%r) " raise ImproperlyConfigured("The COMMENTS_APP (%r) "
"must be in INSTALLED_APPS" % settings.COMMENTS_APP) "must be in INSTALLED_APPS" % settings.COMMENTS_APP)
return app_config.app_module return app_config.module
def get_comment_app_name(): def get_comment_app_name():
""" """

View File

@ -47,7 +47,7 @@ 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_config in apps.get_app_configs(): for app_config in apps.get_app_configs():
if module_has_submodule(app_config.app_module, "management"): if module_has_submodule(app_config.module, "management"):
import_module('.management', app_config.name) import_module('.management', app_config.name)
# Get the database we're operating from # Get the database we're operating from

View File

@ -102,7 +102,7 @@ def get_tests(app_config):
except ImportError: except ImportError:
# Couldn't import tests.py. Was it due to a missing file, or # Couldn't import tests.py. Was it due to a missing file, or
# due to an import error in a tests.py that actually exists? # due to an import error in a tests.py that actually exists?
if not module_has_submodule(app_config.app_module, TEST_MODULE): if not module_has_submodule(app_config.module, TEST_MODULE):
test_module = None test_module = None
else: else:
# The module exists, so there must be an import error in the test # The module exists, so there must be an import error in the test

View File

@ -80,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(app_config.app_module, module_to_search): if module_has_submodule(app_config.module, module_to_search):
raise raise

View File

@ -141,7 +141,7 @@ Read-only attributes
It may be ``None`` if the application isn't stored in a directory, for It may be ``None`` if the application isn't stored in a directory, for
instance if it's loaded from an egg. instance if it's loaded from an egg.
.. attribute:: AppConfig.app_module .. attribute:: AppConfig.module
Root module for the application, e.g. ``<module 'django.contrib.admin' from Root module for the application, e.g. ``<module 'django.contrib.admin' from
'django/contrib/admin/__init__.pyc'>``. 'django/contrib/admin/__init__.pyc'>``.