8000 [soc2010/app-loading] register models als unbound if the cache is not… · ddriddle/django@3da601b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3da601b

Browse files
committed
[soc2010/app-loading] register models als unbound if the cache is not initialized
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13576 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 81dc507 commit 3da601b

File tree

3 files changed

+101
-61
lines changed

3 files changed

+101
-61
lines changed

django/core/apps.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AppCache(object):
4646
installed_apps = [],
4747

4848
# Mapping of app_labels to a dictionary of model names to model code.
49-
app_models = SortedDict(),
49+
unbound_models = {},
5050

5151
# -- Everything below here is only used when populating the cache --
5252
loaded = False,
@@ -79,7 +79,19 @@ def _populate(self):
7979
if not self.nesting_level:
8080
for app_name in self.postponed:
8181
self.load_app(app_name)
82+
# since the cache is still unseeded at this point
83+
# all models have been stored as unbound models
84+
# we need to assign the models to the app instances
85+
for app_name, models in self.unbound_models.iteritems():
86+
app_instance = self.find_app(app_name)
87+
if not app_instance:
88+
raise ImproperlyConfigured(
89+
'Could not find an app instance for "%s"'
90+
% app_label)
91+
for model in models.itervalues():
92+
app_instance.models.append(model)
8293
self.loaded = True
94+
self.unbound_models = {}
8395
finally:
8496
self.write_lock.release()
8597

@@ -159,15 +171,6 @@ def find_app(self, name):
159171
if app.name == name:
160172
return app
161173

162-
def create_app(self, name):
163-
"""create an app instance"""
164-
name = name.split('.')[-1]
165-
app = self.find_app(name)
166-
if not app:
167-
app = App(name)
168-
self.app_instances.append(app)
169-
return app
170-
171174
def app_cache_ready(self):
172175
"""
173176
Returns true if the model cache is fully populated.
@@ -259,25 +262,34 @@ def get_model(self, app_label, model_name, seed_cache=True):
259262
if seed_cache:
260263
self._populate()
261264
app = self.find_app(app_label)
262-
if app:
265+
if self.app_cache_ready() and not app:
266+
return
267+
if cache.app_cache_ready():
263268
for model in app.models:
264269
if model_name.lower() == model._meta.object_name.lower():
265270
return model
271+
else:
272+
return self.unbound_models.get(app_label, {}).get(
273+
model_name.lower())
266274

267275
def register_models(self, app_label, *models):
268276
"""
269277
Register a set of models as belonging to an app.
270278
"""
271279
app_instance = self.find_app(app_label)
272-
if not app_instance:
273-
raise ImproperlyConfigured('Could not find App instance with label "%s". '
274-
'Please check your INSTALLED_APPS setting'
275-
% app_label)
280+
if self.app_cache_ready() and not app_instance:
281+
raise ImproperlyConfigured(
282+
'Could not find an app instance with the label "%s". '
283+
'Please check your INSTALLED_APPS setting' % app_label)
284+
276285
for model in models:
277-
# Store as 'name: model' pair in a dictionary
278-
# in the models list of the App instance
279286
model_name = model._meta.object_name.lower()
280-
model_dict = self.app_models.setdefault(app_label, SortedDict())
287+
if self.app_cache_ready():
288+
model_dict = dict([(model._meta.object_name.lower(), model)
289+
for model in app_instance.models])
290+
else:
291+
model_dict = self.unbound_models.setdefault(app_label, {})
292+
281293
if model_name in model_dict:
282294
# The same model may be imported via different paths (e.g.
283295
# appname.models and project.appname.models). We use the source
@@ -289,8 +301,10 @@ def register_models(self, app_label, *models):
289301
# comparing.
290302
if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
291303
continue
292-
model_dict[model_name] = model
293-
app_instance.models.append(model)
304+
if self.app_cache_ready():
305+
app_instance.models.append(model)
306+
else:
307+
model_dict[model_name] = model
294308
self._get_models_cache.clear()
295309

296310
cache = AppCache()

tests/appcachetests/runtests.py

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ def tearDown(self):
3232
# To detect which model modules have been imported, we go through
3333
# all loaded model classes and remove their respective module
3434
# from sys.modules
35-
for app in cache.app_models.itervalues():
35+
for app in cache.unbound_models.itervalues():
3636
for name in app.itervalues():
3737
module = name.__module__
3838
if module in sys.modules:
3939
del sys.modules[module]
4040

41+
for app in cache.app_instances:
42+
for model in app.models:
43+
module = model.__module__
44+
if module in sys.modules:
45+
del sys.modules[module]
46+
4147
# we cannot copy() the whole cache.__dict__ in the setUp function
4248
# because thread.RLock is un(deep)copyable
43-
cache.app_models = SortedDict()
49+
cache.unbound_models = {}
4450
cache.app_instances = []
4551
cache.installed_apps = []
4652

@@ -178,34 +184,47 @@ def test_include_auto_created(self):
178184
from django.contrib.flatpages.models import Site, FlatPage
179185
self.assertEqual(len(models), 3)
180186
self.assertEqual(models[0], Site)
181-
self.assertEqual(models[1].__name__, 'FlatPage_sites')
182-
self.assertEqual(models[2], FlatPage)
187+
self.assertEqual(models[1], FlatPage)
188+
self.assertEqual(models[2].__name__, 'FlatPage_sites')
183189
self.assertTrue(cache.app_cache_ready())
184190

185-
def test_include_deferred(self):
186-
"""TODO!"""
187-
188191
class GetModelTests(AppCacheTestCase):
189192
"""Tests for the get_model function"""
190193

191-
def test_get_model(self):
192-
"""Test that the correct model is returned"""
193-
settings.INSTALLED_APPS = ('django.contrib.sites',
194-
'django.contrib.flatpages',)
195-
rv = cache.get_model('flatpages', 'FlatPage')
196-
from django.contrib.flatpages.models import FlatPage
197-
self.assertEqual(rv, FlatPage)
194+
def test_seeded(self):
195+
"""
196+
Test that the correct model is returned when the cache is seeded
197+
"""
198+
settings.INSTALLED_APPS = ('model_app',)
199+
rv = cache.get_model('model_app', 'Person')
200+
self.assertEqual(rv.__name__, 'Person')
198201
self.assertTrue(cache.app_cache_ready())
199202

200-
def test_invalid(self):
201-
"""Test that None is returned if an app/model does not exist"""
202-
self.assertEqual(cache.get_model('foo', 'bar'), None)
203+
def test_seeded_invalid(self):
204+
"""
205+
Test that None is returned if a model was not registered
206+
with the seeded cache
207+
"""
208+
rv = cache.get_model('model_app', 'Person')
209+
self.assertEqual(rv, None)
203210
self.assertTrue(cache.app_cache_ready())
204211

205-
def test_without_seeding(self):
206-
"""Test that None is returned if the cache is not seeded"""
207-
settings.INSTALLED_APPS = ('django.contrib.flatpages',)
208-
rv = cache.get_model('flatpages', 'FlatPage', seed_cache=False)
212+
def test_unseeded(self):
213+
"""
214+
Test that the correct model is returned when the cache is
215+
unseeded (but the model was registered using register_models)
216+
"""
217+
from model_app.models import Person
218+
rv = cache.get_model('model_app', 'Person', seed_cache=False)
219+
self.assertEqual(rv.__name__, 'Person')
220+
self.assertFalse(cache.app_cache_ready())
221+
222+
def test_unseeded_invalid(self):
223+
"""
224+
Test that None is returned if a model was not registered
225+
with the unseeded cache
226+
"""
227+
rv = cache.get_model('model_app', 'Person', seed_cache=False)
209228
self.assertEqual(rv, None)
210229
self.assertFalse(cache.app_cache_ready())
211230

@@ -285,30 +304,37 @@ def test_installed_apps(self):
285304
class RegisterModelsTests(AppCacheTestCase):
286305
"""Tests for the register_models function"""
287306

288-
def test_register_models(self):
307+
def test_seeded_cache(self):
289308
"""
290-
Test that register_models attaches the models to an existing
291-
app instance
309+
Test that the models are attached to the correct app instance
310+
in a seeded cache
292311
"""
293-
# We don't need to call the register_models method. Importing the
294-
# models.py file will suffice. This is done in the load_app function
295-
# The ModelBase will call the register_models method
296-
cache.load_app('model_app')
297-
app = cache.app_instances[0]
298-
self.assertEqual(len(cache.app_instances), 1)
299-
self.assertEqual(app.models[0].__name__, 'Person')
312+
settings.INSTALLED_APPS = ('model_app',)
313+
cache.get_app_errors()
314+
self.assertTrue(cache.app_cache_ready())
315+
app_models = cache.app_instances[0].models
316+
self.assertEqual(len(app_models), 1)
317+
self.assertEqual(app_models[0].__name__, 'Person')
300318

301-
def test_app_not_installed(self):
319+
def test_seeded_cache_invalid_app(self):
302320
"""
303-
Test that an exception is raised if models are tried to be registered
304-
to an app that isn't listed in INSTALLED_APPS.
321+
Test that an exception is raised if the cache is seeded and models
322+
are tried to be attached to an app instance that doesn't exist
305323
"""
306-
try:
307-
from model_app.models import Person
308-
except ImproperlyConfigured:
309-
pass
310-
else:
311-
self.fail('ImproperlyConfigured not raised')
324+
settings.INSTALLED_APPS = ('model_app',)
325+
cache.get_app_errors()
326+
self.assertTrue(cache.app_cache_ready())
327+
from model_app.models import Person
328+
self.assertRaises(ImproperlyConfigured, cache.register_models,
329+
'model_app_NONEXISTENT', *(Person,))
330+
331+
def test_unseeded_cache(self):
332+
"""
333+
Test that models can be registered with an unseeded cache
334+
"""
335+
from model_app.models import Person
336+
self.assertFalse(cache.app_cache_ready())
337+
self.assertEquals(cache.unbound_models['model_app']['person'], Person)
312338

313339
if __name__ == '__main__':
314340
unittest.main()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models
22

3-
class User(models.Model):
3+
class Person(models.Model):
44
first_name = models.CharField(max_length=30)
55
last_name = models.CharField(max_length=30)

0 commit comments

Comments
 (0)
0