def enable(self): # Keep this code at the beginning to leave the settings unchanged # in case it raises an exception because INSTALLED_APPS is invalid. if 'INSTALLED_APPS' in self.options: try: apps.set_installed_apps(self.options['INSTALLED_APPS']) except Exception: apps.unset_installed_apps() raise override = UserSettingsHolder(settings._wrapped) for key, new_value in self.options.items(): setattr(override, key, new_value) # 保存原有的配置 self.wrapped = settings._wrapped settings._wrapped = override for key, new_value in self.options.items(): setting_changed.send( sender=settings._wrapped.__class__, setting=key, value=new_value, enter=True)
def disable(self): if "INSTALLED_APPS" in self.options: apps.unset_installed_apps() settings._wrapped = self.wrapped del self.wrapped for key in self.options: new_value = getattr(settings, key, None) setting_changed.send(sender=settings._wrapped.__class__, setting=key, value=new_value, enter=False)
def _call_test_func(self, test_fn): apps = None need_to_call_unset = False if django.get_version() >= '1.7': from django.apps import apps if not apps.is_installed('djangular.config.angularseed_template'): apps.set_installed_apps(tuple([ 'djangular.config.angularseed_template'])) need_to_call_unset = True try: test_fn(self) finally: if apps and need_to_call_unset: apps.unset_installed_apps()
def optimize_apps_for_test_labels(test_labels): test_map = AppAndTestMap() for label in test_labels: if "." in label: test_map.add_test(label.split(".")[0], build_test(label)) else: test_map.add_app(label) _real_installed_apps = settings.INSTALLED_APPS needed_apps = test_map.get_needed_installed_apps() print "overriding settings.INSTALLED_APPS to {}".format(",".join(test_map.get_needed_installed_apps())) settings.INSTALLED_APPS = tuple(needed_apps) apps.set_installed_apps(settings.INSTALLED_APPS) try: yield finally: settings.INSTALLED_APPS = _real_installed_apps apps.unset_installed_apps()
def enable(self): # Keep this code at the beginning to leave the settings unchanged # in case it raises an exception because INSTALLED_APPS is invalid. if 'INSTALLED_APPS' in self.options: try: apps.set_installed_apps(self.options['INSTALLED_APPS']) except Exception: apps.unset_installed_apps() raise override = UserSettingsHolder(settings._wrapped) for key, new_value in self.options.items(): setattr(override, key, new_value) self.wrapped = settings._wrapped settings._wrapped = override for key, new_value in self.options.items(): setting_changed.send(sender=settings._wrapped.__class__, setting=key, value=new_value, enter=True)
def disable(self): if 'INSTALLED_APPS' in self.options: apps.unset_installed_apps() settings._wrapped = self.wrapped del self.wrapped responses = [] for key in self.options: new_value = getattr(settings, key, None) responses_for_setting = setting_changed.send_robust( sender=settings._wrapped.__class__, setting=key, value=new_value, enter=False, ) responses.extend(responses_for_setting) if self.enable_exception is not None: exc = self.enable_exception self.enable_exception = None raise exc for _, response in responses: if isinstance(response, Exception): raise response
def optimize_apps_for_test_labels(test_labels): # TODO make this work on Django 1.8 (django.test.simple has been removed) from django.test.simple import build_test test_map = AppAndTestMap() for label in test_labels: if isinstance(label, tuple): test_map.add_test(*label) elif '.' in label: test_map.add_test(label.split('.')[0], build_test(label)) else: test_map.add_app(label) _real_installed_apps = settings.INSTALLED_APPS needed_apps = test_map.get_needed_installed_apps() print 'overriding settings.INSTALLED_APPS to {}'.format(','.join( test_map.get_needed_installed_apps())) settings.INSTALLED_APPS = tuple(needed_apps) apps.set_installed_apps(settings.INSTALLED_APPS) try: yield finally: settings.INSTALLED_APPS = _real_installed_apps apps.unset_installed_apps()
def tearDown(self): super(TestModelsLoaderMixin, self).tearDown() call_command('flush', verbosity=0, interactive=False) settings.INSTALLED_APPS = self._models_loader_old_settings # If we added a fake 'models' module to sys.modules, remove it. models_mod = self._tests_loader_models_mod if models_mod: try: del sys.modules[models_mod.__name__] except KeyError: pass if apps: apps.unset_installed_apps() cache.clear_cache() else: if models_mod: del cache.app_store[models_mod] cache._get_models_cache.clear()
def tearDown(self): super(TestModelsLoaderMixin, self).tearDown() call_command('flush', verbosity=0, interactive=False) settings.INSTALLED_APPS = self._models_loader_old_settings # If we added a fake 'models' module to sys.modules, remove it. models_mod = self._tests_loader_models_mod if models_mod: try: del sys.modules[models_mod.__name__] except KeyError: pass if apps: apps.unset_installed_apps() apps.all_models[self.tests_app].clear() else: if models_mod: del app_cache.app_store[models_mod] app_cache._get_models_cache.clear()
def optimize_apps_for_test_labels(test_labels): # TODO make this work on Django 1.8 (django.test.simple has been removed) from django.test.simple import build_test test_map = AppAndTestMap() for label in test_labels: if isinstance(label, tuple): test_map.add_test(*label) elif '.' in label: test_map.add_test(label.split('.')[0], build_test(label)) else: test_map.add_app(label) _real_installed_apps = settings.INSTALLED_APPS needed_apps = test_map.get_needed_installed_apps() print 'overriding settings.INSTALLED_APPS to {}'.format( ','.join(test_map.get_needed_installed_apps()) ) settings.INSTALLED_APPS = tuple(needed_apps) apps.set_installed_apps(settings.INSTALLED_APPS) try: yield finally: settings.INSTALLED_APPS = _real_installed_apps apps.unset_installed_apps()
def tearDown(self): try: from django.apps import apps apps.unset_installed_apps() except ImportError: self._ctx.__exit__(None, None, None)
import logging
def tearDown(self): apps.unset_installed_apps() super().tearDown()