From da36d03fe69c35e2f289590c8c44c4c0d1281f24 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 13 Dec 2013 20:22:21 +0100 Subject: [PATCH] Added get_app_configs() to iterate on app_config instances. Refactored get_apps() to rely on that method. This commit is fully backwards-compatible. --- django/apps/cache.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/django/apps/cache.py b/django/apps/cache.py index 893b1af2b4..034971c624 100644 --- a/django/apps/cache.py +++ b/django/apps/cache.py @@ -153,6 +153,21 @@ class BaseAppCache(object): """ return self.loaded + def get_app_configs(self, only_installed=True): + """ + Return an iterable of application configurations. + + If only_installed is True (default), only applications explicitly + listed in INSTALLED_APPS are considered. + """ + self._populate() + for app_config in self.app_configs.values(): + if only_installed and not app_config.installed: + continue + if self.available_apps is not None and app_config.label not in self.available_apps: + continue + yield app_config + def get_app_config(self, app_label, only_installed=True): """ Returns the application configuration for the given app_label. @@ -177,15 +192,7 @@ class BaseAppCache(object): """ Returns a list of all installed modules that contain models. """ - self._populate() - - # app_configs is an OrderedDict, which ensures that the returned list - # is always in the same order (with new apps added at the end). This - # avoids unstable ordering on the admin app list page, for example. - apps = [app for app in self.app_configs.items() if app[1].installed] - if self.available_apps is not None: - apps = [app for app in apps if app[0] in self.available_apps] - return [app[1].models_module for app in apps] + return [app_config.models_module for app_config in self.get_app_configs()] def _get_app_package(self, app): return '.'.join(app.__name__.split('.')[:-1])