From 2732edc5f2305cf454d5df43c7d85a68c639493e Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 14 Dec 2013 10:08:44 +0100 Subject: [PATCH] Deprecated get_apps(). --- django/apps/cache.py | 15 ++++++---- django/contrib/contenttypes/management.py | 4 +-- django/core/management/commands/dumpdata.py | 4 ++- django/core/management/commands/flush.py | 4 +-- django/core/management/commands/migrate.py | 7 +++-- django/core/management/sql.py | 32 ++++++++++++--------- django/db/backends/__init__.py | 13 ++++----- django/db/migrations/loader.py | 15 +++++----- django/test/simple.py | 4 +-- tests/runtests.py | 2 +- 10 files changed, 54 insertions(+), 46 deletions(-) diff --git a/django/apps/cache.py b/django/apps/cache.py index 9d762a58ec4..c0648a9015a 100644 --- a/django/apps/cache.py +++ b/django/apps/cache.py @@ -189,12 +189,6 @@ class BaseAppCache(object): raise UnavailableApp("App with label %r isn't available." % app_label) return app_config - def get_apps(self): - """ - Returns a list of all installed modules that contain models. - """ - return [app_config.models_module for app_config in self.get_app_configs()] - def get_app(self, app_label): """ Returns the module containing the models for the given app_label. @@ -338,6 +332,15 @@ class BaseAppCache(object): ### DEPRECATED METHODS GO BELOW THIS LINE ### + 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) + 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]) diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 477464c37e1..dfe5bb3c65b 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -86,8 +86,8 @@ If you're unsure, answer 'no'. def update_all_contenttypes(verbosity=2, **kwargs): - for app in app_cache.get_apps(): - update_contenttypes(app, None, verbosity, **kwargs) + for app_config in app_cache.get_app_configs(): + update_contenttypes(app_config.models_module, None, verbosity, **kwargs) signals.post_migrate.connect(update_contenttypes) diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 4f87b040033..ebef8ba75ce 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -78,7 +78,9 @@ class Command(BaseCommand): if len(app_labels) == 0: if primary_keys: raise CommandError("You can only use --pks option with one model") - app_list = OrderedDict((app, None) for app in app_cache.get_apps() if app not in excluded_apps) + app_list = OrderedDict((app_config.models_module, None) + for app_config in app_cache.get_app_configs() + if app_config.models_module not in excluded_apps) else: if len(app_labels) > 1 and primary_keys: raise CommandError("You can only use --pks option with one model") diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index f4b221a32c8..0701bf82a9b 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -94,6 +94,6 @@ Are you sure you want to do this? # Emit the post migrate signal. This allows individual applications to # respond as if the database had been migrated from scratch. all_models = [] - for app in app_cache.get_apps(): - all_models.extend(router.get_migratable_models(app, database, include_auto_created=True)) + for app_config in app_cache.get_app_configs(): + all_models.extend(router.get_migratable_models(app_config.models_module, database, include_auto_created=True)) emit_post_migrate_signal(set(all_models), verbosity, interactive, database) diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 39a2d9a7849..ce5ffae4a5a 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -180,9 +180,10 @@ class Command(BaseCommand): # Build the manifest of apps and models that are to be synchronized all_models = [ - (app.__name__.split('.')[-2], - router.get_migratable_models(app, connection.alias, include_auto_created=True)) - for app in app_cache.get_apps() if app.__name__.split('.')[-2] in apps + (app_config.label, + router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True)) + for app_config in app_cache.get_app_configs() + if app_config.label in apps ] def model_installed(model): diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 36ee5760e1e..909faf01171 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -207,23 +207,27 @@ def custom_sql_for_model(model, style, connection): def emit_pre_migrate_signal(create_models, verbosity, interactive, db): # Emit the pre_migrate signal for every application. - for app in app_cache.get_apps(): - app_name = app.__name__.split('.')[-2] + for app_config in app_cache.get_app_configs(): if verbosity >= 2: - print("Running pre-migrate handlers for application %s" % app_name) - models.signals.pre_migrate.send(sender=app, app=app, - create_models=create_models, - verbosity=verbosity, - interactive=interactive, - db=db) + print("Running pre-migrate handlers for application %s" % app_config.label) + models.signals.pre_migrate.send( + sender=app_config.models_module, + app=app_config.models_module, + create_models=create_models, + verbosity=verbosity, + interactive=interactive, + db=db) def emit_post_migrate_signal(created_models, verbosity, interactive, db): # Emit the post_migrate signal for every application. - for app in app_cache.get_apps(): - app_name = app.__name__.split('.')[-2] + for app_config in app_cache.get_app_configs(): if verbosity >= 2: - print("Running post-migrate handlers for application %s" % app_name) - models.signals.post_migrate.send(sender=app, app=app, - created_models=created_models, verbosity=verbosity, - interactive=interactive, db=db) + print("Running post-migrate handlers for application %s" % app_config.label) + models.signals.post_migrate.send( + sender=app_config.models_module, + app=app_config.models_module, + created_models=created_models, + verbosity=verbosity, + interactive=interactive, + db=db) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index edbca0e5572..6e5d03ff96f 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -1271,8 +1271,8 @@ class BaseDatabaseIntrospection(object): from django.apps import app_cache from django.db import router tables = set() - for app in app_cache.get_apps(): - for model in router.get_migratable_models(app, self.connection.alias): + for app_config in app_cache.get_app_configs(): + for model in router.get_migratable_models(app_config.models_module, self.connection.alias): if not model._meta.managed: continue tables.add(model._meta.db_table) @@ -1292,8 +1292,8 @@ class BaseDatabaseIntrospection(object): from django.apps import app_cache from django.db import router all_models = [] - for app in app_cache.get_apps(): - all_models.extend(router.get_migratable_models(app, self.connection.alias)) + for app_config in app_cache.get_app_configs(): + all_models.extend(router.get_migratable_models(app_config.models_module, self.connection.alias)) tables = list(map(self.table_name_converter, tables)) return set([ m for m in all_models @@ -1305,11 +1305,10 @@ class BaseDatabaseIntrospection(object): from django.apps import app_cache from django.db import models, router - apps = app_cache.get_apps() sequence_list = [] - for app in apps: - for model in router.get_migratable_models(app, self.connection.alias): + for app_config in app_cache.get_app_configs(): + for model in router.get_migratable_models(app_config.models_module, self.connection.alias): if not model._meta.managed: continue if model._meta.swapped: diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index 859e57f3525..af311fdf029 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -55,10 +55,9 @@ class MigrationLoader(object): self.disk_migrations = {} self.unmigrated_apps = set() self.migrated_apps = set() - for app in app_cache.get_apps(): + for app_config in app_cache.get_app_configs(): # Get the migrations module directory - app_label = app.__name__.split(".")[-2] - module_name = self.migrations_module(app_label) + module_name = self.migrations_module(app_config.label) was_loaded = module_name in sys.modules try: module = import_module(module_name) @@ -66,7 +65,7 @@ class MigrationLoader(object): # I hate doing this, but I don't want to squash other import errors. # Might be better to try a directory check directly. if "No module named" in str(e) and "migrations" in str(e): - self.unmigrated_apps.add(app_label) + self.unmigrated_apps.add(app_config.label) continue raise else: @@ -79,7 +78,7 @@ class MigrationLoader(object): # Force a reload if it's already loaded (tests need this) if was_loaded: six.moves.reload_module(module) - self.migrated_apps.add(app_label) + self.migrated_apps.add(app_config.label) directory = os.path.dirname(module.__file__) # Scan for .py[c|o] files migration_names = set() @@ -100,14 +99,14 @@ class MigrationLoader(object): break raise if not hasattr(migration_module, "Migration"): - raise BadMigrationError("Migration %s in app %s has no Migration class" % (migration_name, app_label)) + raise BadMigrationError("Migration %s in app %s has no Migration class" % (migration_name, app_config.label)) # Ignore South-style migrations if hasattr(migration_module.Migration, "forwards"): south_style_migrations = True break - self.disk_migrations[app_label, migration_name] = migration_module.Migration(migration_name, app_label) + self.disk_migrations[app_config.label, migration_name] = migration_module.Migration(migration_name, app_config.label) if south_style_migrations: - self.unmigrated_apps.add(app_label) + self.unmigrated_apps.add(app_config.label) def get_migration(self, app_label, name_prefix): "Gets the migration exactly named, or raises KeyError" diff --git a/django/test/simple.py b/django/test/simple.py index 10f29061b3d..d00b95f0087 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -244,8 +244,8 @@ class DjangoTestSuiteRunner(runner.DiscoverRunner): app = app_cache.get_app(label) suite.addTest(build_suite(app)) else: - for app in app_cache.get_apps(): - suite.addTest(build_suite(app)) + for app_config in app_cache.get_app_configs(): + suite.addTest(build_suite(app_config.models_module)) if extra_tests: for test in extra_tests: diff --git a/tests/runtests.py b/tests/runtests.py index c294664c310..bec2d22feae 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -81,7 +81,7 @@ def get_test_modules(): def get_installed(): from django.apps import app_cache - return [app.__name__.rsplit('.', 1)[0] for app in app_cache.get_apps()] + return [app_config.name for app_config in app_cache.get_app_configs()] def setup(verbosity, test_labels):