2013-12-18 18:19:56 +08:00
|
|
|
from collections import defaultdict, OrderedDict
|
2013-12-12 04:44:27 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2013-12-14 02:28:42 +08:00
|
|
|
import warnings
|
2013-12-12 04:44:27 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-12-24 17:08:04 +08:00
|
|
|
from django.utils import lru_cache
|
2013-12-18 21:49:29 +08:00
|
|
|
from django.utils.module_loading import import_lock
|
2013-12-12 04:44:27 +08:00
|
|
|
from django.utils._os import upath
|
|
|
|
|
2013-12-18 21:49:29 +08:00
|
|
|
from .base import AppConfig
|
2013-12-12 04:44:27 +08:00
|
|
|
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
class Apps(object):
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-24 19:25:17 +08:00
|
|
|
A registry that stores the configuration of installed applications.
|
|
|
|
|
|
|
|
It also keeps track of models eg. to provide reverse-relations.
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-18 00:47:19 +08:00
|
|
|
|
2013-12-30 23:03:06 +08:00
|
|
|
def __init__(self, installed_apps=()):
|
|
|
|
# installed_apps is set to None when creating the master registry
|
|
|
|
# because it cannot be populated at that point. Other registries must
|
|
|
|
# provide a list of installed apps and are populated immediately.
|
|
|
|
if installed_apps is None and hasattr(sys.modules[__name__], 'apps'):
|
|
|
|
raise RuntimeError("You must supply an installed_apps argument.")
|
2013-12-18 00:47:19 +08:00
|
|
|
|
2013-12-26 21:12:30 +08:00
|
|
|
# Mapping of app labels => model names => model classes. Every time a
|
|
|
|
# model is imported, ModelBase.__new__ calls apps.register_model which
|
|
|
|
# creates an entry in all_models. All imported models are registered,
|
|
|
|
# regardless of whether they're defined in an installed application
|
|
|
|
# and whether the registry has been populated. Since it isn't possible
|
|
|
|
# to reimport a module safely (it could reexecute initialization code)
|
|
|
|
# all_models is never overridden or reset.
|
2013-12-18 18:19:56 +08:00
|
|
|
self.all_models = defaultdict(OrderedDict)
|
|
|
|
|
2013-12-12 18:28:04 +08:00
|
|
|
# Mapping of labels to AppConfig instances for installed apps.
|
2013-12-18 05:24:25 +08:00
|
|
|
self.app_configs = OrderedDict()
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-23 07:10:53 +08:00
|
|
|
# Stack of app_configs. Used to store the current state in
|
|
|
|
# set_available_apps and set_installed_apps.
|
|
|
|
self.stored_app_configs = []
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-29 03:13:08 +08:00
|
|
|
# Internal flags used when populating the registry.
|
|
|
|
self._apps_loaded = False
|
|
|
|
self._models_loaded = False
|
2013-12-18 18:28:21 +08:00
|
|
|
|
2013-12-18 23:49:42 +08:00
|
|
|
# Pending lookups for lazy relations.
|
|
|
|
self._pending_lookups = {}
|
|
|
|
|
2013-12-30 23:03:06 +08:00
|
|
|
# Populate apps and models, unless it's the master registry.
|
|
|
|
if installed_apps is not None:
|
|
|
|
self.populate_apps(installed_apps)
|
|
|
|
self.populate_models()
|
|
|
|
|
2013-12-24 07:07:54 +08:00
|
|
|
def populate_apps(self, installed_apps=None):
|
2013-12-18 18:28:21 +08:00
|
|
|
"""
|
|
|
|
Populate app-related information.
|
|
|
|
|
|
|
|
This method imports each application module.
|
|
|
|
|
|
|
|
It is thread safe and idempotent, but not reentrant.
|
|
|
|
"""
|
|
|
|
if self._apps_loaded:
|
|
|
|
return
|
|
|
|
# Since populate_apps() may be a side effect of imports, and since
|
|
|
|
# it will itself import modules, an ABBA deadlock between threads
|
|
|
|
# would be possible if we didn't take the import lock. See #18251.
|
|
|
|
with import_lock():
|
|
|
|
if self._apps_loaded:
|
|
|
|
return
|
|
|
|
|
|
|
|
# app_config should be pristine, otherwise the code below won't
|
|
|
|
# guarantee that the order matches the order in INSTALLED_APPS.
|
|
|
|
if self.app_configs:
|
|
|
|
raise RuntimeError("populate_apps() isn't reentrant")
|
|
|
|
|
|
|
|
# Application modules aren't expected to import anything, and
|
|
|
|
# especially not other application modules, even indirectly.
|
|
|
|
# Therefore we simply import them sequentially.
|
2013-12-24 07:07:54 +08:00
|
|
|
if installed_apps is None:
|
|
|
|
installed_apps = settings.INSTALLED_APPS
|
2013-12-29 03:13:08 +08:00
|
|
|
for entry in installed_apps:
|
|
|
|
if isinstance(entry, AppConfig):
|
|
|
|
app_config = entry
|
|
|
|
else:
|
|
|
|
app_config = AppConfig.create(entry)
|
2013-12-18 18:28:21 +08:00
|
|
|
self.app_configs[app_config.label] = app_config
|
|
|
|
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-18 18:28:21 +08:00
|
|
|
self._apps_loaded = True
|
|
|
|
|
|
|
|
def populate_models(self):
|
|
|
|
"""
|
|
|
|
Populate model-related information.
|
|
|
|
|
|
|
|
This method imports each models module.
|
|
|
|
|
|
|
|
It is thread safe, idempotent and reentrant.
|
|
|
|
"""
|
|
|
|
if self._models_loaded:
|
|
|
|
return
|
|
|
|
# Since populate_models() may be a side effect of imports, and since
|
|
|
|
# it will itself import modules, an ABBA deadlock between threads
|
|
|
|
# would be possible if we didn't take the import lock. See #18251.
|
|
|
|
with import_lock():
|
|
|
|
if self._models_loaded:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.populate_apps()
|
|
|
|
|
|
|
|
# Models modules are likely to import other models modules, for
|
|
|
|
# example to reference related objects. As a consequence:
|
|
|
|
# - we deal with import loops by postponing affected modules.
|
|
|
|
# - we provide reentrancy by making import_models() idempotent.
|
|
|
|
|
|
|
|
outermost = not hasattr(self, '_postponed')
|
|
|
|
if outermost:
|
|
|
|
self._postponed = []
|
|
|
|
|
|
|
|
for app_config in self.app_configs.values():
|
|
|
|
|
2013-12-24 05:21:23 +08:00
|
|
|
if app_config.models is not None:
|
|
|
|
continue
|
|
|
|
|
2013-12-18 18:28:21 +08:00
|
|
|
try:
|
|
|
|
all_models = self.all_models[app_config.label]
|
|
|
|
app_config.import_models(all_models)
|
|
|
|
except ImportError:
|
|
|
|
self._postponed.append(app_config)
|
|
|
|
|
|
|
|
if outermost:
|
2013-12-31 00:00:50 +08:00
|
|
|
try:
|
|
|
|
for app_config in self._postponed:
|
|
|
|
all_models = self.all_models[app_config.label]
|
|
|
|
app_config.import_models(all_models)
|
|
|
|
finally:
|
|
|
|
del self._postponed
|
2013-12-18 18:28:21 +08:00
|
|
|
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-18 18:28:21 +08:00
|
|
|
self._models_loaded = True
|
|
|
|
|
2013-12-30 19:49:53 +08:00
|
|
|
for app_config in self.get_app_configs():
|
|
|
|
app_config.setup()
|
|
|
|
|
2013-12-26 22:04:58 +08:00
|
|
|
@property
|
2013-12-24 19:25:17 +08:00
|
|
|
def ready(self):
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-26 22:04:58 +08:00
|
|
|
Whether the registry is fully populated.
|
2013-12-12 04:44:27 +08:00
|
|
|
|
|
|
|
Useful for code that wants to cache the results of get_models() for
|
|
|
|
themselves once it is safe to do so.
|
|
|
|
"""
|
2013-12-18 21:49:29 +08:00
|
|
|
return self._models_loaded # implies self._apps_loaded.
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-18 20:10:23 +08:00
|
|
|
def get_app_configs(self, only_with_models_module=False):
|
2013-12-14 03:22:21 +08:00
|
|
|
"""
|
2013-12-19 17:26:18 +08:00
|
|
|
Imports applications and returns an iterable of app configs.
|
2013-12-14 03:22:21 +08:00
|
|
|
|
2013-12-19 17:26:18 +08:00
|
|
|
If only_with_models_module in True (non-default), imports models and
|
|
|
|
considers only applications containing a models module.
|
2013-12-14 03:22:21 +08:00
|
|
|
"""
|
2013-12-19 17:26:18 +08:00
|
|
|
if only_with_models_module:
|
|
|
|
self.populate_models()
|
|
|
|
else:
|
|
|
|
self.populate_apps()
|
|
|
|
|
2013-12-14 03:22:21 +08:00
|
|
|
for app_config in self.app_configs.values():
|
2013-12-15 01:51:58 +08:00
|
|
|
if only_with_models_module and app_config.models_module is None:
|
|
|
|
continue
|
2013-12-14 03:22:21 +08:00
|
|
|
yield app_config
|
|
|
|
|
2013-12-18 20:10:23 +08:00
|
|
|
def get_app_config(self, app_label, only_with_models_module=False):
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
"""
|
2013-12-19 17:26:18 +08:00
|
|
|
Imports applications and returns an app config for the given label.
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
|
2013-12-19 17:26:18 +08:00
|
|
|
Raises LookupError if no application exists with this label.
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
|
2013-12-19 17:26:18 +08:00
|
|
|
If only_with_models_module in True (non-default), imports models and
|
|
|
|
considers only applications containing a models module.
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
"""
|
2013-12-19 17:26:18 +08:00
|
|
|
if only_with_models_module:
|
|
|
|
self.populate_models()
|
|
|
|
else:
|
|
|
|
self.populate_apps()
|
|
|
|
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
app_config = self.app_configs.get(app_label)
|
2013-12-15 01:51:58 +08:00
|
|
|
if app_config is None:
|
2013-12-28 06:17:59 +08:00
|
|
|
raise LookupError("No installed app with label '%s'." % app_label)
|
2013-12-15 01:51:58 +08:00
|
|
|
if only_with_models_module and app_config.models_module is None:
|
2013-12-28 21:41:11 +08:00
|
|
|
raise LookupError("App '%s' doesn't have a models module." % app_label)
|
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.
2013-12-13 19:02:54 +08:00
|
|
|
return app_config
|
|
|
|
|
2013-12-24 17:08:04 +08:00
|
|
|
# This method is performance-critical at least for Django's test suite.
|
|
|
|
@lru_cache.lru_cache(maxsize=None)
|
2013-12-29 01:27:33 +08:00
|
|
|
def get_models(self, app_mod=None, include_auto_created=False,
|
|
|
|
include_deferred=False, include_swapped=False):
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-30 04:47:55 +08:00
|
|
|
Returns a list of all installed models.
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-30 04:47:55 +08:00
|
|
|
By default, the following models aren't included:
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-30 04:47:55 +08:00
|
|
|
- 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.
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-30 04:47:55 +08:00
|
|
|
Set the corresponding keyword argument to True to include such models.
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-18 21:49:29 +08:00
|
|
|
self.populate_models()
|
2013-12-30 04:47:55 +08:00
|
|
|
|
2013-12-12 04:44:27 +08:00
|
|
|
if app_mod:
|
2013-12-30 04:47:55 +08:00
|
|
|
warnings.warn(
|
|
|
|
"The app_mod argument of get_models is deprecated.",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-12-16 03:46:46 +08:00
|
|
|
app_label = app_mod.__name__.split('.')[-2]
|
2013-12-29 01:27:33 +08:00
|
|
|
try:
|
2013-12-30 04:47:55 +08:00
|
|
|
return list(self.get_app_config(app_label).get_models(
|
|
|
|
include_auto_created, include_deferred, include_swapped))
|
|
|
|
except LookupError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
result = []
|
|
|
|
for app_config in self.app_configs.values():
|
|
|
|
result.extend(list(app_config.get_models(
|
|
|
|
include_auto_created, include_deferred, include_swapped)))
|
|
|
|
return result
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-28 21:41:11 +08:00
|
|
|
def get_model(self, app_label, model_name):
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-28 21:41:11 +08:00
|
|
|
Returns the model matching the given app_label and model_name.
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-28 21:41:11 +08:00
|
|
|
model_name is case-insensitive.
|
|
|
|
|
2013-12-28 21:55:54 +08:00
|
|
|
Raises LookupError if no application exists with this label, or no
|
|
|
|
model exists with this name in the application.
|
2013-12-12 04:44:27 +08:00
|
|
|
"""
|
2013-12-18 21:49:29 +08:00
|
|
|
self.populate_models()
|
2013-12-28 21:55:54 +08:00
|
|
|
return self.get_app_config(app_label).get_model(model_name.lower())
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-14 04:29:30 +08:00
|
|
|
def register_model(self, app_label, model):
|
2013-12-18 18:19:56 +08:00
|
|
|
# Since this method is called when models are imported, it cannot
|
|
|
|
# perform imports because of the risk of import loops. It mustn't
|
|
|
|
# call get_app_config().
|
2013-12-14 04:29:30 +08:00
|
|
|
model_name = model._meta.model_name
|
Simplified the implementation of register_model.
register_model is called exactly once in the entire Django code base, at the
bottom of ModelBase.__new__:
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
ModelBase.__new__ exits prematurely 120 lines earlier (sigh) if a model with
the same name is already registered:
if new_class._meta.apps.get_registered_model(new_class._meta.app_label, name):
return
(This isn't the exact code, but it's equivalent.)
apps.register_model and apps.get_registered_model are essentially a setter and
a getter for apps.all_models, and apps.register_model is the only setter. As a
consequence, new_class._meta.apps.all_models cannot change in-between.
Considering that name == new_class.__name__, we can conclude that
register_model(app_label, model) is always called with such arguments that
get_registered_model(app_label, model.__name__) returns None.
Considering that model._meta.model_name == model.__name__.lower(), and looking
at the implementation of register_model and get_registered_model, this proves
that self.all_models[app_label] doesn't contain model._meta.model_name in
register_model, allowing us to simplify the implementation.
2013-12-28 06:03:03 +08:00
|
|
|
app_models = self.all_models[app_label]
|
|
|
|
# Defensive check for extra safety.
|
|
|
|
if model_name in app_models:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Conflicting '%s' models in application '%s': %s and %s." %
|
|
|
|
(model_name, app_label, app_models[model_name], model))
|
|
|
|
app_models[model_name] = model
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-20 04:57:09 +08:00
|
|
|
def has_app(self, app_name):
|
|
|
|
"""
|
2013-12-24 19:25:17 +08:00
|
|
|
Checks whether an application with this name exists in the registry.
|
2013-12-23 00:07:18 +08:00
|
|
|
|
|
|
|
app_name is the full name of the app eg. 'django.contrib.admin'.
|
2013-12-20 04:57:09 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
It's safe to call this method at import time, even while the registry
|
2013-12-24 22:40:12 +08:00
|
|
|
is being populated. It returns False for apps that aren't loaded yet.
|
2013-12-20 04:57:09 +08:00
|
|
|
"""
|
|
|
|
app_config = self.app_configs.get(app_name.rpartition(".")[2])
|
2013-12-23 00:11:47 +08:00
|
|
|
return app_config is not None and app_config.name == app_name
|
2013-12-20 04:57:09 +08:00
|
|
|
|
2013-12-23 00:07:52 +08:00
|
|
|
def get_registered_model(self, app_label, model_name):
|
2013-12-18 18:19:56 +08:00
|
|
|
"""
|
2013-12-28 21:41:11 +08:00
|
|
|
Similar to get_model(), but doesn't require that an app exists with
|
|
|
|
the given app_label.
|
2013-12-18 18:19:56 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
It's safe to call this method at import time, even while the registry
|
2013-12-28 21:55:54 +08:00
|
|
|
is being populated.
|
2013-12-18 18:19:56 +08:00
|
|
|
"""
|
2013-12-28 21:55:54 +08:00
|
|
|
model = self.all_models[app_label].get(model_name.lower())
|
|
|
|
if model is None:
|
|
|
|
raise LookupError(
|
|
|
|
"Model '%s.%s' not registered." % (app_label, model_name))
|
|
|
|
return model
|
2013-12-18 18:19:56 +08:00
|
|
|
|
2013-12-12 04:44:27 +08:00
|
|
|
def set_available_apps(self, available):
|
2013-12-20 17:39:12 +08:00
|
|
|
"""
|
|
|
|
Restricts the set of installed apps used by get_app_config[s].
|
|
|
|
|
|
|
|
available must be an iterable of application names.
|
|
|
|
|
2013-12-24 07:07:54 +08:00
|
|
|
set_available_apps() must be balanced with unset_available_apps().
|
|
|
|
|
2013-12-20 17:39:12 +08:00
|
|
|
Primarily used for performance optimization in TransactionTestCase.
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
This method is safe is the sense that it doesn't trigger any imports.
|
2013-12-20 17:39:12 +08:00
|
|
|
"""
|
2013-12-14 03:51:21 +08:00
|
|
|
available = set(available)
|
2013-12-20 17:39:12 +08:00
|
|
|
installed = set(app_config.name for app_config in self.get_app_configs())
|
2013-12-14 03:51:21 +08:00
|
|
|
if not available.issubset(installed):
|
2013-12-12 04:44:27 +08:00
|
|
|
raise ValueError("Available apps isn't a subset of installed "
|
2013-12-14 03:51:21 +08:00
|
|
|
"apps, extra apps: %s" % ", ".join(available - installed))
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
self.stored_app_configs.append(self.app_configs)
|
|
|
|
self.app_configs = OrderedDict(
|
|
|
|
(label, app_config)
|
|
|
|
for label, app_config in self.app_configs.items()
|
|
|
|
if app_config.name in available)
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-12 04:44:27 +08:00
|
|
|
|
|
|
|
def unset_available_apps(self):
|
2013-12-20 17:39:12 +08:00
|
|
|
"""
|
|
|
|
Cancels a previous call to set_available_apps().
|
|
|
|
"""
|
2013-12-23 07:10:53 +08:00
|
|
|
self.app_configs = self.stored_app_configs.pop()
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
def set_installed_apps(self, installed):
|
|
|
|
"""
|
2013-12-24 07:07:54 +08:00
|
|
|
Enables a different set of installed apps for get_app_config[s].
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
installed must be an iterable in the same format as INSTALLED_APPS.
|
|
|
|
|
2013-12-24 07:07:54 +08:00
|
|
|
set_installed_apps() must be balanced with unset_installed_apps(),
|
|
|
|
even if it exits with an exception.
|
|
|
|
|
2013-12-23 07:10:53 +08:00
|
|
|
Primarily used as a receiver of the setting_changed signal in tests.
|
|
|
|
|
|
|
|
This method may trigger new imports, which may add new models to the
|
|
|
|
registry of all imported models. They will stay in the registry even
|
|
|
|
after unset_installed_apps(). Since it isn't possible to replay
|
|
|
|
imports safely (eg. that could lead to registering listeners twice),
|
|
|
|
models are registered when they're imported and never removed.
|
|
|
|
"""
|
2013-12-27 01:37:04 +08:00
|
|
|
self.stored_app_configs.append((self.app_configs, self._apps_loaded, self._models_loaded))
|
2013-12-23 07:10:53 +08:00
|
|
|
self.app_configs = OrderedDict()
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-24 07:07:54 +08:00
|
|
|
self._apps_loaded = False
|
|
|
|
self.populate_apps(installed)
|
|
|
|
self._models_loaded = False
|
|
|
|
self.populate_models()
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
def unset_installed_apps(self):
|
|
|
|
"""
|
|
|
|
Cancels a previous call to set_installed_apps().
|
|
|
|
"""
|
2013-12-27 01:37:04 +08:00
|
|
|
self.app_configs, self._apps_loaded, self._models_loaded = self.stored_app_configs.pop()
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
|
|
|
|
|
|
|
def clear_cache(self):
|
|
|
|
"""
|
|
|
|
Clears all internal caches, for methods that alter the app registry.
|
|
|
|
|
|
|
|
This is mostly used in tests.
|
|
|
|
"""
|
2013-12-30 03:57:03 +08:00
|
|
|
self.get_models.cache_clear()
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-14 02:28:42 +08:00
|
|
|
### DEPRECATED METHODS GO BELOW THIS LINE ###
|
|
|
|
|
2013-12-19 00:56:11 +08:00
|
|
|
def load_app(self, app_name):
|
|
|
|
"""
|
|
|
|
Loads the app with the provided fully qualified name, and returns the
|
|
|
|
model module.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"load_app(app_name) is deprecated.",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-12-20 21:03:14 +08:00
|
|
|
app_config = AppConfig.create(app_name)
|
2013-12-19 00:56:11 +08:00
|
|
|
app_config.import_models(self.all_models[app_config.label])
|
|
|
|
self.app_configs[app_config.label] = app_config
|
2013-12-30 03:43:10 +08:00
|
|
|
self.clear_cache()
|
2013-12-19 00:56:11 +08:00
|
|
|
return app_config.models_module
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def app_cache_ready(self):
|
|
|
|
warnings.warn(
|
2013-12-26 22:04:58 +08:00
|
|
|
"app_cache_ready() is deprecated in favor of the ready property.",
|
2013-12-24 19:25:17 +08:00
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-12-26 22:04:58 +08:00
|
|
|
return self.ready
|
2013-12-24 19:25:17 +08:00
|
|
|
|
2013-12-14 18:11:52 +08:00
|
|
|
def get_app(self, app_label):
|
|
|
|
"""
|
|
|
|
Returns the module containing the models for the given app_label.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"get_app_config(app_label).models_module supersedes get_app(app_label).",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
try:
|
2013-12-19 17:26:18 +08:00
|
|
|
return self.get_app_config(
|
|
|
|
app_label, only_with_models_module=True).models_module
|
2013-12-14 18:11:52 +08:00
|
|
|
except LookupError as exc:
|
|
|
|
# Change the exception type for backwards compatibility.
|
|
|
|
raise ImproperlyConfigured(*exc.args)
|
|
|
|
|
2013-12-14 17:08:44 +08:00
|
|
|
def get_apps(self):
|
|
|
|
"""
|
|
|
|
Returns a list of all installed modules that contain models.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"[a.models_module for a in get_app_configs()] supersedes get_apps().",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-12-19 17:26:18 +08:00
|
|
|
app_configs = self.get_app_configs(only_with_models_module=True)
|
|
|
|
return [app_config.models_module for app_config in app_configs]
|
2013-12-14 17:08:44 +08:00
|
|
|
|
2013-12-14 02:28:42 +08:00
|
|
|
def _get_app_package(self, app):
|
|
|
|
return '.'.join(app.__name__.split('.')[:-1])
|
|
|
|
|
|
|
|
def get_app_package(self, app_label):
|
|
|
|
warnings.warn(
|
|
|
|
"get_app_config(label).name supersedes get_app_package(label).",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
return self._get_app_package(self.get_app(app_label))
|
|
|
|
|
|
|
|
def _get_app_path(self, app):
|
|
|
|
if hasattr(app, '__path__'): # models/__init__.py package
|
|
|
|
app_path = app.__path__[0]
|
|
|
|
else: # models.py module
|
|
|
|
app_path = app.__file__
|
|
|
|
return os.path.dirname(upath(app_path))
|
|
|
|
|
|
|
|
def get_app_path(self, app_label):
|
|
|
|
warnings.warn(
|
|
|
|
"get_app_config(label).path supersedes get_app_path(label).",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
return self._get_app_path(self.get_app(app_label))
|
|
|
|
|
|
|
|
def get_app_paths(self):
|
|
|
|
"""
|
|
|
|
Returns a list of paths to all installed apps.
|
|
|
|
|
|
|
|
Useful for discovering files at conventional locations inside apps
|
|
|
|
(static files, templates, etc.)
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"[a.path for a in get_app_configs()] supersedes get_app_paths().",
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
|
2013-12-18 21:49:29 +08:00
|
|
|
self.populate_models()
|
2013-12-14 02:28:42 +08:00
|
|
|
|
|
|
|
app_paths = []
|
|
|
|
for app in self.get_apps():
|
|
|
|
app_paths.append(self._get_app_path(app))
|
|
|
|
return app_paths
|
|
|
|
|
2013-12-14 04:29:30 +08:00
|
|
|
def register_models(self, app_label, *models):
|
|
|
|
"""
|
|
|
|
Register a set of models as belonging to an app.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
2013-12-19 00:56:11 +08:00
|
|
|
"register_models(app_label, *models) is deprecated.",
|
2013-12-14 04:29:30 +08:00
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
|
|
|
for model in models:
|
|
|
|
self.register_model(app_label, model)
|
|
|
|
|
2013-12-12 04:44:27 +08:00
|
|
|
|
2013-12-30 23:03:06 +08:00
|
|
|
apps = Apps(installed_apps=None)
|