From 21f22f9544b0c6c4390bcd1acd98e6e5201ed83e Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 29 Dec 2013 20:43:10 +0100 Subject: [PATCH] Added Apps.clear_cache(). This avoid leaking implementation details to tests that swap models. --- django/apps/registry.py | 24 ++++++++++++++++-------- django/contrib/auth/tests/test_basic.py | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/django/apps/registry.py b/django/apps/registry.py index 67cca2cfdf..a66e561566 100644 --- a/django/apps/registry.py +++ b/django/apps/registry.py @@ -84,7 +84,7 @@ class Apps(object): app_config = AppConfig.create(entry) self.app_configs[app_config.label] = app_config - self.get_models.cache_clear() + self.clear_cache() self._apps_loaded = True def populate_models(self): @@ -133,7 +133,7 @@ class Apps(object): del self._postponed - self.get_models.cache_clear() + self.clear_cache() self._models_loaded = True @property @@ -248,7 +248,7 @@ class Apps(object): "Conflicting '%s' models in application '%s': %s and %s." % (model_name, app_label, app_models[model_name], model)) app_models[model_name] = model - self.get_models.cache_clear() + self.clear_cache() def has_app(self, app_name): """ @@ -299,14 +299,14 @@ class Apps(object): (label, app_config) for label, app_config in self.app_configs.items() if app_config.name in available) - self.get_models.cache_clear() + self.clear_cache() def unset_available_apps(self): """ Cancels a previous call to set_available_apps(). """ self.app_configs = self.stored_app_configs.pop() - self.get_models.cache_clear() + self.clear_cache() def set_installed_apps(self, installed): """ @@ -327,7 +327,7 @@ class Apps(object): """ self.stored_app_configs.append((self.app_configs, self._apps_loaded, self._models_loaded)) self.app_configs = OrderedDict() - self.get_models.cache_clear() + self.clear_cache() self._apps_loaded = False self.populate_apps(installed) self._models_loaded = False @@ -338,7 +338,15 @@ class Apps(object): Cancels a previous call to set_installed_apps(). """ self.app_configs, self._apps_loaded, self._models_loaded = self.stored_app_configs.pop() - self.get_models.cache_clear() + self.clear_cache() + + def clear_cache(self): + """ + Clears all internal caches, for methods that alter the app registry. + + This is mostly used in tests. + """ + self.clear_cache() ### DEPRECATED METHODS GO BELOW THIS LINE ### @@ -353,7 +361,7 @@ class Apps(object): app_config = AppConfig.create(app_name) app_config.import_models(self.all_models[app_config.label]) self.app_configs[app_config.label] = app_config - self.get_models.cache_clear() + self.clear_cache() return app_config.models_module def app_cache_ready(self): diff --git a/django/contrib/auth/tests/test_basic.py b/django/contrib/auth/tests/test_basic.py index beaa97f285..0bcd2c47c2 100644 --- a/django/contrib/auth/tests/test_basic.py +++ b/django/contrib/auth/tests/test_basic.py @@ -26,7 +26,7 @@ def user_model_swapped(**kwargs): # Reset User manager setattr(User, 'objects', User._default_manager) ensure_default_manager(User) - apps.get_models.cache_clear() + apps.clear_cache() def mock_inputs(inputs):