diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-14 11:11:52 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-17 10:17:45 +0100 |
| commit | 69039becde74308d6218c72c1e410cd0049fd2bd (patch) | |
| tree | 4c7c669ad326fb376ecda6bf8532d3000a293518 /django | |
| parent | 2732edc5f2305cf454d5df43c7d85a68c639493e (diff) | |
Deprecated get_app().
Diffstat (limited to 'django')
| -rw-r--r-- | django/apps/cache.py | 29 | ||||
| -rw-r--r-- | django/contrib/admindocs/views.py | 17 | ||||
| -rw-r--r-- | django/contrib/auth/tests/test_management.py | 6 | ||||
| -rw-r--r-- | django/core/management/base.py | 5 | ||||
| -rw-r--r-- | django/core/management/commands/dumpdata.py | 13 | ||||
| -rw-r--r-- | django/core/management/commands/makemigrations.py | 5 | ||||
| -rw-r--r-- | django/db/migrations/questioner.py | 7 | ||||
| -rw-r--r-- | django/test/simple.py | 4 |
8 files changed, 40 insertions, 46 deletions
diff --git a/django/apps/cache.py b/django/apps/cache.py index c0648a9015..76e580eb40 100644 --- a/django/apps/cache.py +++ b/django/apps/cache.py @@ -189,19 +189,6 @@ class BaseAppCache(object): raise UnavailableApp("App with label %r isn't available." % app_label) return app_config - def get_app(self, app_label): - """ - Returns the module containing the models for the given app_label. - - Raises UnavailableApp when set_available_apps() in in effect and - doesn't include app_label. - """ - try: - return self.get_app_config(app_label).models_module - except LookupError as exc: - # Change the exception type for backwards compatibility. - raise ImproperlyConfigured(*exc.args) - def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False, only_installed=True, include_swapped=False): @@ -332,6 +319,22 @@ class BaseAppCache(object): ### DEPRECATED METHODS GO BELOW THIS LINE ### + def get_app(self, app_label): + """ + Returns the module containing the models for the given app_label. + + Raises UnavailableApp when set_available_apps() in in effect and + doesn't include app_label. + """ + warnings.warn( + "get_app_config(app_label).models_module supersedes get_app(app_label).", + PendingDeprecationWarning, stacklevel=2) + try: + return self.get_app_config(app_label).models_module + except LookupError as exc: + # Change the exception type for backwards compatibility. + raise ImproperlyConfigured(*exc.args) + def get_apps(self): """ Returns a list of all installed modules that contain models. diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py index e1edf0aad8..c52e0f0d7f 100644 --- a/django/contrib/admindocs/views.py +++ b/django/contrib/admindocs/views.py @@ -9,7 +9,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.admin.views.decorators import staff_member_required from django.db import models -from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist +from django.core.exceptions import ViewDoesNotExist from django.http import Http404 from django.core import urlresolvers from django.contrib.admindocs import utils @@ -194,17 +194,12 @@ class ModelDetailView(BaseAdminDocsView): def get_context_data(self, **kwargs): # Get the model class. try: - app_mod = app_cache.get_app(self.kwargs['app_label']) - except ImproperlyConfigured: - raise Http404(_("App %r not found") % self.kwargs['app_label']) - model = None - for m in app_cache.get_models(app_mod): - if m._meta.model_name == self.kwargs['model_name']: - model = m - break + app_cache.get_app_config(self.kwargs['app_label']) + except LookupError: + raise Http404(_("App %(app_label)r not found") % self.kwargs) + model = app_cache.get_model(self.kwargs['app_label'], self.kwargs['model_name']) if model is None: - raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % { - 'model_name': self.kwargs['model_name'], 'app_label': self.kwargs['app_label']}) + raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs) opts = model._meta diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index 43ad15dcab..c005e74442 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -184,21 +184,21 @@ class CustomUserModelValidationTestCase(TestCase): def test_required_fields_is_list(self): "REQUIRED_FIELDS should be a list." new_io = StringIO() - get_validation_errors(new_io, app_cache.get_app('auth')) + get_validation_errors(new_io, app_cache.get_app_config('auth').models_module) self.assertIn("The REQUIRED_FIELDS must be a list or tuple.", new_io.getvalue()) @override_settings(AUTH_USER_MODEL='auth.CustomUserBadRequiredFields') def test_username_not_in_required_fields(self): "USERNAME_FIELD should not appear in REQUIRED_FIELDS." new_io = StringIO() - get_validation_errors(new_io, app_cache.get_app('auth')) + get_validation_errors(new_io, app_cache.get_app_config('auth').models_module) self.assertIn("The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.", new_io.getvalue()) @override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername') def test_username_non_unique(self): "A non-unique USERNAME_FIELD should raise a model validation error." new_io = StringIO() - get_validation_errors(new_io, app_cache.get_app('auth')) + get_validation_errors(new_io, app_cache.get_app_config('auth').models_module) self.assertIn("The USERNAME_FIELD must be unique. Add unique=True to the field parameters.", new_io.getvalue()) diff --git a/django/core/management/base.py b/django/core/management/base.py index d749bc2e8e..c7d9c939e6 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -11,7 +11,6 @@ import sys from optparse import make_option, OptionParser import django -from django.core.exceptions import ImproperlyConfigured from django.core.management.color import color_style, no_style from django.utils.encoding import force_str from django.utils.six import StringIO @@ -346,8 +345,8 @@ class AppCommand(BaseCommand): if not app_labels: raise CommandError('Enter at least one appname.') try: - app_list = [app_cache.get_app(app_label) for app_label in app_labels] - except (ImproperlyConfigured, ImportError) as e: + app_list = [app_cache.get_app_config(app_label).models_module for app_label in app_labels] + except (LookupError, ImportError) as e: raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e) output = [] for app in app_list: diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index ebef8ba75c..af8da18322 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -3,7 +3,6 @@ import warnings from collections import OrderedDict from optparse import make_option -from django.core.exceptions import ImproperlyConfigured from django.core.management.base import BaseCommand, CommandError from django.core import serializers from django.db import router, DEFAULT_DB_ALIAS @@ -70,9 +69,9 @@ class Command(BaseCommand): excluded_models.add(model_obj) else: try: - app_obj = app_cache.get_app(exclude) + app_obj = app_cache.get_app_config(exclude).models_module excluded_apps.add(app_obj) - except ImproperlyConfigured: + except LookupError: raise CommandError('Unknown app in excludes: %s' % exclude) if len(app_labels) == 0: @@ -89,8 +88,8 @@ class Command(BaseCommand): try: app_label, model_label = label.split('.') try: - app = app_cache.get_app(app_label) - except ImproperlyConfigured: + app = app_cache.get_app_config(app_label).models_module + except LookupError: raise CommandError("Unknown application: %s" % app_label) if app in excluded_apps: continue @@ -109,8 +108,8 @@ class Command(BaseCommand): # This is just an app - no model qualifier app_label = label try: - app = app_cache.get_app(app_label) - except ImproperlyConfigured: + app = app_cache.get_app_config(app_label).models_module + except LookupError: raise CommandError("Unknown application: %s" % app_label) if app in excluded_apps: continue diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index cd891c404d..aaf0270840 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -5,7 +5,6 @@ from optparse import make_option from django.apps import app_cache from django.core.management.base import BaseCommand, CommandError -from django.core.exceptions import ImproperlyConfigured from django.db import connections, DEFAULT_DB_ALIAS, migrations from django.db.migrations.loader import MigrationLoader from django.db.migrations.autodetector import MigrationAutodetector @@ -38,8 +37,8 @@ class Command(BaseCommand): bad_app_labels = set() for app_label in app_labels: try: - app_cache.get_app(app_label) - except ImproperlyConfigured: + app_cache.get_app_config(app_label) + except LookupError: bad_app_labels.add(app_label) if bad_app_labels: for app_label in bad_app_labels: diff --git a/django/db/migrations/questioner.py b/django/db/migrations/questioner.py index d9446f8d88..4a860dcf7e 100644 --- a/django/db/migrations/questioner.py +++ b/django/db/migrations/questioner.py @@ -5,7 +5,6 @@ import sys from django.apps import app_cache from django.utils import datetime_safe from django.utils.six.moves import input -from django.core.exceptions import ImproperlyConfigured class MigrationQuestioner(object): @@ -29,10 +28,10 @@ class MigrationQuestioner(object): # Apps from the new app template will have these; the python # file check will ensure we skip South ones. try: - models_module = app_cache.get_app(app_label) - except ImproperlyConfigured: # It's a fake app + app_config = app_cache.get_app_config(app_label) + except LookupError: # It's a fake app. return self.defaults.get("ask_initial", False) - migrations_import_path = "%s.migrations" % models_module.__package__ + migrations_import_path = "%s.migrations" % app_config.name try: migrations_module = importlib.import_module(migrations_import_path) except ImportError: diff --git a/django/test/simple.py b/django/test/simple.py index d00b95f008..8ea1d6ac53 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -179,7 +179,7 @@ def build_test(label): # # First, look for TestCase instances with a name that matches # - app_module = app_cache.get_app(parts[0]) + app_module = app_cache.get_app_config(parts[0]).models_module test_module = get_tests(app_module) TestClass = getattr(app_module, parts[1], None) @@ -241,7 +241,7 @@ class DjangoTestSuiteRunner(runner.DiscoverRunner): if '.' in label: suite.addTest(build_test(label)) else: - app = app_cache.get_app(label) + app = app_cache.get_app_config(label).models_module suite.addTest(build_suite(app)) else: for app_config in app_cache.get_app_configs(): |
