Removed BaseAppCache.app_store.

It was only storing redundant information. This is part of the effort to
allow applications without a models module.
This commit is contained in:
Aymeric Augustin 2013-12-11 22:19:19 +01:00
parent 860c2c8bc5
commit 9217b89da3
9 changed files with 20 additions and 50 deletions

View File

@ -39,11 +39,8 @@ def _initialize():
[shared] state of the app cache. [shared] state of the app cache.
""" """
return dict( return dict(
# Keys of app_store are the model modules for each application.
app_store=ModelDict(),
# Mapping of installed app_labels to model modules for that app. # Mapping of installed app_labels to model modules for that app.
app_labels={}, app_labels=OrderedDict(),
# Mapping of app_labels to a dictionary of model names to model code. # Mapping of app_labels to a dictionary of model names to model code.
# May contain apps that are not installed. # May contain apps that are not installed.
@ -154,9 +151,9 @@ class BaseAppCache(object):
raise raise
self.nesting_level -= 1 self.nesting_level -= 1
if models not in self.app_store: label = self._label_for(models)
self.app_store[models] = len(self.app_store) if label not in self.app_labels:
self.app_labels[self._label_for(models)] = models self.app_labels[label] = models
return models return models
def app_cache_ready(self): def app_cache_ready(self):
@ -174,17 +171,13 @@ class BaseAppCache(object):
""" """
self._populate() self._populate()
apps = self.app_store.items() # app_labels is an OrderedDict, which ensures that the returned list
# is always in the same order (with new apps added at the end). This
# avoids unstable ordering on the admin app list page, for example.
apps = self.app_labels.items()
if self.available_apps is not None: if self.available_apps is not None:
apps = [elt for elt in apps apps = [app for app in apps if app[0] in self.available_apps]
if self._label_for(elt[0]) in self.available_apps] return [app[1] for app in apps]
# Ensure the returned list is always in the same order (with new apps
# added at the end). This avoids unstable ordering on the admin app
# list page, for example.
apps = sorted(apps, key=lambda elt: elt[1])
return [elt[0] for elt in apps]
def _get_app_package(self, app): def _get_app_package(self, app):
return '.'.join(app.__name__.split('.')[:-1]) return '.'.join(app.__name__.split('.')[:-1])
@ -282,8 +275,9 @@ class BaseAppCache(object):
pass pass
self._populate() self._populate()
if app_mod: if app_mod:
if app_mod in self.app_store: app_label = self._label_for(app_mod)
app_list = [self.app_models.get(self._label_for(app_mod), ModelDict())] if app_label in self.app_labels:
app_list = [self.app_models.get(app_label, ModelDict())]
else: else:
app_list = [] app_list = []
else: else:

View File

@ -20,12 +20,10 @@ class EggLoadingTest(TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
self.old_app_models = copy.deepcopy(cache.app_models) self.old_app_models = copy.deepcopy(cache.app_models)
self.old_app_store = copy.deepcopy(cache.app_store)
def tearDown(self): def tearDown(self):
sys.path = self.old_path sys.path = self.old_path
cache.app_models = self.old_app_models cache.app_models = self.old_app_models
cache.app_store = self.old_app_store
def test_egg1(self): def test_egg1(self):
"""Models module can be loaded from an app in an egg""" """Models module can be loaded from an app in an egg"""

View File

@ -23,11 +23,9 @@ class InvalidModelTestCase(unittest.TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
self.old_app_models = copy.deepcopy(cache.app_models) self.old_app_models = copy.deepcopy(cache.app_models)
self.old_app_store = copy.deepcopy(cache.app_store)
def tearDown(self): def tearDown(self):
cache.app_models = self.old_app_models cache.app_models = self.old_app_models
cache.app_store = self.old_app_store
cache._get_models_cache = {} cache._get_models_cache = {}
sys.stdout = self.old_stdout sys.stdout = self.old_stdout

View File

@ -115,7 +115,6 @@ class ManagersRegressionTests(TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
old_app_models = copy.deepcopy(cache.app_models) old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
class SwappableModel(models.Model): class SwappableModel(models.Model):
class Meta: class Meta:
@ -131,7 +130,6 @@ class ManagersRegressionTests(TestCase):
finally: finally:
cache.app_models = old_app_models cache.app_models = old_app_models
cache.app_store = old_app_store
@override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent') @override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
def test_custom_swappable_manager(self): def test_custom_swappable_manager(self):
@ -140,7 +138,6 @@ class ManagersRegressionTests(TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
old_app_models = copy.deepcopy(cache.app_models) old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
class SwappableModel(models.Model): class SwappableModel(models.Model):
@ -160,7 +157,6 @@ class ManagersRegressionTests(TestCase):
finally: finally:
cache.app_models = old_app_models cache.app_models = old_app_models
cache.app_store = old_app_store
@override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent') @override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
def test_explicit_swappable_manager(self): def test_explicit_swappable_manager(self):
@ -169,7 +165,6 @@ class ManagersRegressionTests(TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
old_app_models = copy.deepcopy(cache.app_models) old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
class SwappableModel(models.Model): class SwappableModel(models.Model):
@ -189,7 +184,6 @@ class ManagersRegressionTests(TestCase):
finally: finally:
cache.app_models = old_app_models cache.app_models = old_app_models
cache.app_store = old_app_store
def test_regress_3871(self): def test_regress_3871(self):
related = RelatedModel.objects.create() related = RelatedModel.objects.create()

View File

@ -133,11 +133,9 @@ class MakeMigrationsTests(MigrationTestBase):
self.migration_dir = os.path.join(self.test_dir, 'migrations_%d' % self.creation_counter) self.migration_dir = os.path.join(self.test_dir, 'migrations_%d' % self.creation_counter)
self.migration_pkg = "migrations.migrations_%d" % self.creation_counter self.migration_pkg = "migrations.migrations_%d" % self.creation_counter
self._old_app_models = copy.deepcopy(cache.app_models) self._old_app_models = copy.deepcopy(cache.app_models)
self._old_app_store = copy.deepcopy(cache.app_store)
def tearDown(self): def tearDown(self):
cache.app_models = self._old_app_models cache.app_models = self._old_app_models
cache.app_store = self._old_app_store
cache._get_models_cache = {} cache._get_models_cache = {}
os.chdir(self.test_dir) os.chdir(self.test_dir)

View File

@ -2,7 +2,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import copy
import datetime import datetime
import os import os
@ -116,8 +115,6 @@ class WriterTests(TestCase):
self.assertIn("Migration", result) self.assertIn("Migration", result)
def test_migration_path(self): def test_migration_path(self):
_old_app_store = copy.deepcopy(cache.app_store)
test_apps = [ test_apps = [
'migrations.migrations_test_apps.normal', 'migrations.migrations_test_apps.normal',
'migrations.migrations_test_apps.with_package_model', 'migrations.migrations_test_apps.with_package_model',
@ -125,7 +122,6 @@ class WriterTests(TestCase):
base_dir = os.path.dirname(os.path.dirname(__file__)) base_dir = os.path.dirname(os.path.dirname(__file__))
try:
with override_settings(INSTALLED_APPS=test_apps): with override_settings(INSTALLED_APPS=test_apps):
for app in test_apps: for app in test_apps:
cache.load_app(app) cache.load_app(app)
@ -133,5 +129,3 @@ class WriterTests(TestCase):
expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py'])) expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
writer = MigrationWriter(migration) writer = MigrationWriter(migration)
self.assertEqual(writer.path, expected_path) self.assertEqual(writer.path, expected_path)
finally:
cache.app_store = _old_app_store

View File

@ -32,8 +32,6 @@ class ProxyModelInheritanceTests(TransactionTestCase):
def tearDown(self): def tearDown(self):
sys.path = self.old_sys_path sys.path = self.old_sys_path
del cache.app_store[cache.app_labels['app1']]
del cache.app_store[cache.app_labels['app2']]
del cache.app_labels['app1'] del cache.app_labels['app1']
del cache.app_labels['app2'] del cache.app_labels['app2']
del cache.app_models['app1'] del cache.app_models['app1']

View File

@ -160,7 +160,6 @@ class ProxyModelTests(TestCase):
# need to be removed in order to prevent bad interactions # need to be removed in order to prevent bad interactions
# with the flush operation in other tests. # with the flush operation in other tests.
old_app_models = copy.deepcopy(cache.app_models) old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
class SwappableModel(models.Model): class SwappableModel(models.Model):
@ -178,7 +177,6 @@ class ProxyModelTests(TestCase):
proxy = True proxy = True
finally: finally:
cache.app_models = old_app_models cache.app_models = old_app_models
cache.app_store = old_app_store
def test_myperson_manager(self): def test_myperson_manager(self):
Person.objects.create(name="fred") Person.objects.create(name="fred")

View File

@ -29,7 +29,6 @@ class TablespacesTests(TestCase):
# The unmanaged models need to be removed after the test in order to # The unmanaged models need to be removed after the test in order to
# prevent bad interactions with the flush operation in other tests. # prevent bad interactions with the flush operation in other tests.
self.old_app_models = copy.deepcopy(cache.app_models) self.old_app_models = copy.deepcopy(cache.app_models)
self.old_app_store = copy.deepcopy(cache.app_store)
for model in Article, Authors, Reviewers, Scientist: for model in Article, Authors, Reviewers, Scientist:
model._meta.managed = True model._meta.managed = True
@ -39,7 +38,6 @@ class TablespacesTests(TestCase):
model._meta.managed = False model._meta.managed = False
cache.app_models = self.old_app_models cache.app_models = self.old_app_models
cache.app_store = self.old_app_store
cache._get_models_cache = {} cache._get_models_cache = {}
def assertNumContains(self, haystack, needle, count): def assertNumContains(self, haystack, needle, count):