Skip to content

Commit 4674415

Browse files
committed
[soc2010/app-loading] raise ImproperlyConfigured when trying to register models to an app that isnt listed in INSTALLED_APPS
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13571 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 2b7bfac commit 4674415

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

django/core/apps.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,8 @@ def app_cache_ready(self):
173173
def get_apps(self):
174174
"Returns a list of all installed modules that contain models."
175175
self._populate()
176-
177-
# Ensure the returned list is always in the same order (with new apps
178-
# added at the end). This avoids unstable ordering on the admin app
179-
# list page, for example.
180176
return [app.models_module for app in self.app_instances\
181-
if app.models_module]
177+
if hasattr(app, 'models_module')]
182178

183179
def get_app(self, app_label, emptyOK=False):
184180
"""
@@ -266,13 +262,10 @@ def register_models(self, app_label, *models):
266262
Register a set of models as belonging to an app.
267263
"""
268264
app_instance = self.find_app(app_label)
269-
270-
# Create a new App instance if the ModelBase tries to register
271-
# an app that isn't listed in INSTALLED_APPS
272265
if not app_instance:
273-
app_instance = App(app_label)
274-
self.app_instances.append(app_instance)
275-
266+
raise ImproperlyConfigured('Could not find App instance with label "%s". '
267+
'Please check your INSTALLED_APPS setting'
268+
% app_label)
276269
for model in models:
277270
# Store as 'name: model' pair in a dictionary
278271
# in the models list of the App instance

tests/appcachetests/runtests.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
from django.conf import settings
66
from django.utils.datastructures import SortedDict
77
from django.core.exceptions import ImproperlyConfigured
8-
from django.core.apps import MultipleInstancesReturned
8+
from django.core.apps import cache, MultipleInstancesReturned
99

1010
# remove when tests are integrated into the django testsuite
1111
settings.configure()
1212

13-
from django.db.models.loading import cache
1413

1514
class AppCacheTestCase(unittest.TestCase):
1615
"""
@@ -41,17 +40,15 @@ def tearDown(self):
4140

4241
# we cannot copy() the whole cache.__dict__ in the setUp function
4342
# because thread.RLock is un(deep)copyable
44-
cache.app_store = SortedDict()
4543
cache.app_models = SortedDict()
46-
cache.app_errors = {}
44+
cache.app_instances = []
45+
4746
cache.loaded = False
4847
cache.handled = {}
4948
cache.postponed = []
5049
cache.nesting_level = 0
5150
cache.write_lock = threading.RLock()
5251
cache._get_models_cache = {}
53-
54-
cache.app_instances = []
5552

5653
class AppCacheReadyTests(AppCacheTestCase):
5754
"""
@@ -65,22 +62,26 @@ def test_not_initialized(self):
6562

6663
def test_load_app(self):
6764
"""Should return False after executing the load_app function"""
68-
cache.load_app('django.contrib.comments')
65+
cache.load_app('nomodel_app')
6966
self.assertFalse(cache.app_cache_ready())
70-
cache.load_app('django.contrib.comments', can_postpone=True)
67+
cache.load_app('nomodel_app', can_postpone=True)
7168
self.assertFalse(cache.app_cache_ready())
7269

7370
class GetAppsTests(AppCacheTestCase):
7471
"""Tests for the get_apps function"""
7572

7673
def test_get_apps(self):
7774
"""Test that the correct models modules are returned"""
78-
settings.INSTALLED_APPS = ('django.contrib.auth',
75+
settings.INSTALLED_APPS = ('django.contrib.sites',
76+
'django.contrib.contenttypes',
77+
'django.contrib.auth',
7978
'django.contrib.flatpages',)
8079
apps = cache.get_apps()
81-
self.assertEqual(len(apps), 2)
80+
self.assertEqual(len(apps), 4)
8281
self.assertTrue(apps[0], 'django.contrib.auth.models')
8382
self.assertTrue(apps[1], 'django.contrib.flatpages.models')
83+
self.assertTrue(apps[2], 'django.contrib.sites.models')
84+
self.assertTrue(apps[3], 'django.contrib.contenttypes.models')
8485
self.assertTrue(cache.app_cache_ready())
8586

8687
def test_empty_models(self):
@@ -94,7 +95,8 @@ class GetAppTests(AppCacheTestCase):
9495

9596
def test_get_app(self):
9697
"""Test that the correct module is returned"""
97-
settings.INSTALLED_APPS = ('django.contrib.auth',)
98+
settings.INSTALLED_APPS = ('django.contrib.contenttypes',
99+
'django.contrib.auth',)
98100
module = cache.get_app('auth')
99101
self.assertTrue(module, 'django.contrib.auth.models')
100102
self.assertTrue(cache.app_cache_ready())
@@ -141,9 +143,10 @@ class GetModelsTests(AppCacheTestCase):
141143

142144
def test_get_models(self):
143145
"""Test that the correct model classes are returned"""
144-
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
145-
from django.contrib.flatpages.models import Site, FlatPage
146+
settings.INSTALLED_APPS = ('django.contrib.sites',
147+
'django.contrib.flatpages',)
146148
models = cache.get_models()
149+
from django.contrib.flatpages.models import Site, FlatPage
147150
self.assertEqual(len(models), 2)
148151
self.assertEqual(models[0], Site)
149152
self.assertEqual(models[1], FlatPage)
@@ -154,7 +157,11 @@ def test_app_mod(self):
154157
Test that the correct model classes are returned if an
155158
app module is specified
156159
"""
157-
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
160+
settings.INSTALLED_APPS = ('django.contrib.sites',
161+
'django.contrib.flatpages',)
162+
# populate cache
163+
cache.get_app_errors()
164+
158165
from django.contrib.flatpages import models
159166
from django.contrib.flatpages.models import FlatPage
160167
rv = cache.get_models(app_mod=models)
@@ -164,9 +171,10 @@ def test_app_mod(self):
164171

165172
def test_include_auto_created(self):
166173
"""Test that auto created models are included if specified"""
167-
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
168-
from django.contrib.flatpages.models import Site, FlatPage
174+
settings.INSTALLED_APPS = ('django.contrib.sites',
175+
'django.contrib.flatpages',)
169176
models = cache.get_models(include_auto_created=True)
177+
from django.contrib.flatpages.models import Site, FlatPage
170178
self.assertEqual(len(models), 3)
171179
self.assertEqual(models[0], Site)
172180
self.assertEqual(models[1].__name__, 'FlatPage_sites')
@@ -181,9 +189,11 @@ class GetModelTests(AppCacheTestCase):
181189

182190
def test_get_model(self):
183191
"""Test that the correct model is returned"""
184-
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
192+
settings.INSTALLED_APPS = ('django.contrib.sites',
193+
'django.contrib.flatpages',)
194+
rv = cache.get_model('flatpages', 'FlatPage')
185195
from django.contrib.flatpages.models import FlatPage
186-
self.assertEqual(cache.get_model('flatpages', 'FlatPage'), FlatPage)
196+
self.assertEqual(rv, FlatPage)
187197
self.assertTrue(cache.app_cache_ready())
188198

189199
def test_invalid(self):
@@ -278,16 +288,17 @@ def test_register_models(self):
278288
self.assertEqual(len(cache.app_instances), 1)
279289
self.assertEqual(app.models[0].__name__, 'Person')
280290

281-
def test_new_instance(self):
291+
def test_app_not_installed(self):
282292
"""
283-
Test a new app instance is created if one doesn't exist, and the
284-
models are attached to it.
293+
Test that an exception is raised if models are tried to be registered
294+
to an app that isn't listed in INSTALLED_APPS.
285295
"""
286-
from model_app.models import Person
287-
app = cache.app_instances[0]
288-
self.assertEqual(len(cache.app_instances), 1)
289-
self.assertEqual(app.name, 'model_app')
290-
self.assertEqual(app.models[0].__name__, 'Person')
296+
try:
297+
from model_app.models import Person
298+
except ImproperlyConfigured:
299+
pass
300+
else:
301+
self.fail('ImproperlyConfigured not raised')
291302

292303
if __name__ == '__main__':
293304
unittest.main()

0 commit comments

Comments
 (0)