1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Deprecated get_app().

This commit is contained in:
Aymeric Augustin
2013-12-14 11:11:52 +01:00
parent 2732edc5f2
commit 69039becde
13 changed files with 55 additions and 62 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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())

View File

@@ -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:

View File

@@ -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

View File

@@ -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:

View File

@@ -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:

View File

@@ -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():