2013-12-18 18:28:21 +08:00
|
|
|
from importlib import import_module
|
2014-01-28 04:28:53 +08:00
|
|
|
import os
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-12-27 02:05:45 +08:00
|
|
|
from django.utils.module_loading import module_has_submodule
|
2013-12-13 06:33:09 +08:00
|
|
|
from django.utils._os import upath
|
|
|
|
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2013-12-18 18:28:21 +08:00
|
|
|
MODELS_MODULE_NAME = 'models'
|
|
|
|
|
|
|
|
|
2013-12-12 18:28:04 +08:00
|
|
|
class AppConfig(object):
|
|
|
|
"""
|
|
|
|
Class representing a Django application and its configuration.
|
|
|
|
"""
|
|
|
|
|
2013-12-27 01:40:28 +08:00
|
|
|
def __init__(self, app_name, app_module):
|
2013-12-13 06:33:09 +08:00
|
|
|
# Full Python path to the application eg. 'django.contrib.admin'.
|
2013-12-18 18:28:21 +08:00
|
|
|
self.name = app_name
|
2013-12-13 06:33:09 +08:00
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
# Root module for the application eg. <module 'django.contrib.admin'
|
|
|
|
# from 'django/contrib/admin/__init__.pyc'>.
|
2013-12-27 01:40:28 +08:00
|
|
|
self.module = app_module
|
2013-12-20 21:03:14 +08:00
|
|
|
|
|
|
|
# The following attributes could be defined at the class level in a
|
|
|
|
# subclass, hence the test-and-set pattern.
|
|
|
|
|
2013-12-13 04:34:39 +08:00
|
|
|
# Last component of the Python path to the application eg. 'admin'.
|
2013-12-13 06:33:09 +08:00
|
|
|
# This value must be unique across a Django project.
|
2013-12-20 21:03:14 +08:00
|
|
|
if not hasattr(self, 'label'):
|
|
|
|
self.label = app_name.rpartition(".")[2]
|
2013-12-13 06:33:09 +08:00
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
# Human-readable name for the application eg. "Admin".
|
|
|
|
if not hasattr(self, 'verbose_name'):
|
|
|
|
self.verbose_name = self.label.title()
|
|
|
|
|
|
|
|
# Filesystem path to the application directory eg.
|
2014-01-28 04:28:53 +08:00
|
|
|
# u'/usr/lib/python2.7/dist-packages/django/contrib/admin'. Unicode on
|
|
|
|
# Python 2 and a str on Python 3.
|
2013-12-20 21:03:14 +08:00
|
|
|
if not hasattr(self, 'path'):
|
2014-01-28 04:28:53 +08:00
|
|
|
self.path = self._path_from_module(app_module)
|
2013-12-13 04:34:39 +08:00
|
|
|
|
|
|
|
# Module containing models eg. <module 'django.contrib.admin.models'
|
2013-12-18 18:28:21 +08:00
|
|
|
# from 'django/contrib/admin/models.pyc'>. Set by import_models().
|
|
|
|
# None if the application doesn't have a models module.
|
|
|
|
self.models_module = None
|
2013-12-12 18:28:04 +08:00
|
|
|
|
2013-12-18 18:28:21 +08:00
|
|
|
# Mapping of lower case model names to model classes. Initally set to
|
|
|
|
# None to prevent accidental access before import_models() runs.
|
|
|
|
self.models = None
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2013-12-12 18:28:04 +08:00
|
|
|
def __repr__(self):
|
2013-12-29 03:13:08 +08:00
|
|
|
return '<%s: %s>' % (self.__class__.__name__, self.label)
|
2013-12-18 18:28:21 +08:00
|
|
|
|
2014-01-28 04:28:53 +08:00
|
|
|
def _path_from_module(self, module):
|
|
|
|
"""Attempt to determine app's filesystem path from its module."""
|
|
|
|
# See #21874 for extended discussion of the behavior of this method in
|
|
|
|
# various cases.
|
|
|
|
# Convert paths to list because Python 3.3 _NamespacePath does not
|
|
|
|
# support indexing.
|
|
|
|
paths = list(getattr(module, '__path__', []))
|
|
|
|
if len(paths) != 1:
|
|
|
|
filename = getattr(module, '__file__', None)
|
|
|
|
if filename is not None:
|
|
|
|
paths = [os.path.dirname(filename)]
|
|
|
|
if len(paths) > 1:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The app module %r has multiple filesystem locations (%r); "
|
|
|
|
"you must configure this app with an AppConfig subclass "
|
|
|
|
"with a 'path' class attribute." % (module, paths))
|
|
|
|
elif not paths:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The app module %r has no filesystem location, "
|
|
|
|
"you must configure this app with an AppConfig subclass "
|
|
|
|
"with a 'path' class attribute." % (module,))
|
|
|
|
return upath(paths[0])
|
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
@classmethod
|
|
|
|
def create(cls, entry):
|
|
|
|
"""
|
|
|
|
Factory that creates an app config from an entry in INSTALLED_APPS.
|
|
|
|
"""
|
|
|
|
try:
|
2014-01-25 05:43:00 +08:00
|
|
|
# If import_module succeeds, entry is a path to an app module,
|
|
|
|
# which may specify an app config class with default_app_config.
|
2013-12-20 21:03:14 +08:00
|
|
|
# Otherwise, entry is a path to an app config class or an error.
|
2013-12-27 01:40:28 +08:00
|
|
|
module = import_module(entry)
|
2013-12-20 21:03:14 +08:00
|
|
|
|
|
|
|
except ImportError:
|
2013-12-27 02:05:45 +08:00
|
|
|
mod_path, _, cls_name = entry.rpartition('.')
|
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
# Raise the original exception when entry cannot be a path to an
|
2013-12-27 02:05:45 +08:00
|
|
|
# app config class.
|
|
|
|
if not mod_path:
|
2013-12-20 21:03:14 +08:00
|
|
|
raise
|
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
else:
|
2013-12-20 21:03:14 +08:00
|
|
|
try:
|
2014-01-25 05:43:00 +08:00
|
|
|
# If this works, the app module specifies an app config class.
|
|
|
|
entry = module.default_app_config
|
2013-12-20 21:03:14 +08:00
|
|
|
except AttributeError:
|
2014-01-25 05:43:00 +08:00
|
|
|
# Otherwise, it simply uses the default app config class.
|
|
|
|
return cls(entry, module)
|
|
|
|
else:
|
|
|
|
mod_path, _, cls_name = entry.rpartition('.')
|
|
|
|
|
|
|
|
# If we're reaching this point, we must load the app config class
|
|
|
|
# located at <mod_path>.<cls_name>.
|
2013-12-20 21:03:14 +08:00
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
# Avoid django.utils.module_loading.import_by_path because it
|
|
|
|
# masks errors -- it reraises ImportError as ImproperlyConfigured.
|
|
|
|
mod = import_module(mod_path)
|
|
|
|
try:
|
|
|
|
cls = getattr(mod, cls_name)
|
|
|
|
except AttributeError:
|
|
|
|
# Emulate the error that "from <mod_path> import <cls_name>"
|
|
|
|
# would raise when <mod_path> exists but not <cls_name>, with
|
|
|
|
# more context (Python just says "cannot import name ...").
|
|
|
|
raise ImportError(
|
|
|
|
"cannot import name '%s' from '%s'" % (cls_name, mod_path))
|
|
|
|
|
|
|
|
# Check for obvious errors. (This check prevents duck typing, but
|
|
|
|
# it could be removed if it became a problem in practice.)
|
|
|
|
if not issubclass(cls, AppConfig):
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"'%s' isn't a subclass of AppConfig." % entry)
|
|
|
|
|
|
|
|
# Obtain app name here rather than in AppClass.__init__ to keep
|
|
|
|
# all error checking for entries in INSTALLED_APPS in one place.
|
|
|
|
try:
|
|
|
|
app_name = cls.name
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"'%s' must supply a name attribute." % entry)
|
2013-12-27 01:40:28 +08:00
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
# Ensure app_name points to a valid module.
|
|
|
|
app_module = import_module(app_name)
|
|
|
|
|
|
|
|
# Entry is a path to an app config class.
|
|
|
|
return cls(app_name, app_module)
|
2013-12-20 21:03:14 +08:00
|
|
|
|
2013-12-28 21:41:11 +08:00
|
|
|
def get_model(self, model_name):
|
|
|
|
"""
|
|
|
|
Returns the model with the given case-insensitive model_name.
|
|
|
|
|
|
|
|
Raises LookupError if no model exists with this name.
|
|
|
|
"""
|
|
|
|
if self.models is None:
|
|
|
|
raise LookupError(
|
|
|
|
"App '%s' doesn't have any models." % self.label)
|
|
|
|
try:
|
|
|
|
return self.models[model_name.lower()]
|
|
|
|
except KeyError:
|
|
|
|
raise LookupError(
|
|
|
|
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
|
|
|
|
|
2013-12-30 03:26:13 +08:00
|
|
|
def get_models(self, include_auto_created=False,
|
|
|
|
include_deferred=False, include_swapped=False):
|
|
|
|
"""
|
|
|
|
Returns an iterable of models.
|
|
|
|
|
|
|
|
By default, the following models aren't included:
|
|
|
|
|
|
|
|
- auto-created models for many-to-many relations without
|
|
|
|
an explicit intermediate table,
|
|
|
|
- models created to satisfy deferred attribute queries,
|
|
|
|
- models that have been swapped out.
|
|
|
|
|
|
|
|
Set the corresponding keyword argument to True to include such models.
|
|
|
|
Keyword arguments aren't documented; they're a private API.
|
|
|
|
"""
|
|
|
|
for model in self.models.values():
|
|
|
|
if model._deferred and not include_deferred:
|
|
|
|
continue
|
|
|
|
if model._meta.auto_created and not include_auto_created:
|
|
|
|
continue
|
|
|
|
if model._meta.swapped and not include_swapped:
|
|
|
|
continue
|
|
|
|
yield model
|
|
|
|
|
2013-12-18 18:28:21 +08:00
|
|
|
def import_models(self, all_models):
|
2013-12-26 21:12:30 +08:00
|
|
|
# Dictionary of models for this app, primarily maintained in the
|
|
|
|
# 'all_models' attribute of the Apps this AppConfig is attached to.
|
|
|
|
# Injected as a parameter because it gets populated when models are
|
2013-12-31 06:15:29 +08:00
|
|
|
# imported, which might happen before populate() imports models.
|
2013-12-18 18:28:21 +08:00
|
|
|
self.models = all_models
|
|
|
|
|
2013-12-27 01:40:28 +08:00
|
|
|
if module_has_submodule(self.module, MODELS_MODULE_NAME):
|
2013-12-18 18:28:21 +08:00
|
|
|
models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
|
|
|
|
self.models_module = import_module(models_module_name)
|
2013-12-30 19:49:53 +08:00
|
|
|
|
2014-01-01 00:55:12 +08:00
|
|
|
def ready(self):
|
2013-12-30 19:49:53 +08:00
|
|
|
"""
|
2014-01-01 00:55:12 +08:00
|
|
|
Override this method in subclasses to run code when Django starts.
|
2013-12-30 19:49:53 +08:00
|
|
|
"""
|