def enable(self): # Save previous value of djangocms_versioning_enabled and then set to False self._prev_versioning_enabled = self.navigation_config.djangocms_versioning_enabled self.navigation_config.djangocms_versioning_enabled = False # Make sure NAVIGATION_VERSIONING_ENABLED setting is False settings_override = UserSettingsHolder(settings._wrapped) settings_override.NAVIGATION_VERSIONING_ENABLED = False self.wrapped = settings._wrapped settings._wrapped = settings_override setting_changed.send( sender=settings._wrapped.__class__, setting='NAVIGATION_VERSIONING_ENABLED', value=False, enter=True) super().enable()
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(): try: setting_changed.send( sender=settings._wrapped.__class__, setting=key, value=new_value, enter=True, ) except Exception as exc: self.enable_exception = exc self.disable()
def enable(self): override = UserSettingsHolder(settings._wrapped) for key, new_value in self.options.items(): setattr(override, key, new_value) settings._wrapped = override for key, new_value in self.options.items(): setting_changed.send(sender=settings._wrapped.__class__, setting=key, value=new_value)
def private_settings(request, tmpdir): import django.conf from django.conf import UserSettingsHolder old = django.conf.settings._wrapped django.conf.settings._wrapped = UserSettingsHolder(old) def finalizer(): django.conf.settings._wrapped = old request.addfinalizer(finalizer) return django.conf.settings
def enable(self): override = UserSettingsHolder(copy.copy(settings._wrapped)) for key, new_value in self.options.iteritems(): if new_value is SETTING_DELETED: try: delattr(override.default_settings, key) except AttributeError: pass else: setattr(override, key, new_value) settings._wrapped = override
def setUp(self): super(BaseTestMixin, self).setUp() global ansible_version self.object_ctr = 0 # Save sys.path before tests. self._sys_path = [x for x in sys.path] # Save os.environ before tests. self._environ = dict(os.environ.items()) # Capture current directory to change back after each test. self._cwd = os.getcwd() # Capture list of temp files/directories created during tests. self._temp_paths = [] self._current_auth = None self._user_passwords = {} self.ansible_version = ansible_version self.assertNotEqual(self.ansible_version, 'unknown') # Wrap settings so we can redefine them within each test. self._wrapped = settings._wrapped settings._wrapped = UserSettingsHolder(settings._wrapped) # Set all AUTH_LDAP_* settings to defaults to avoid using LDAP for # tests unless expicitly configured. for name, value in LDAPSettings.defaults.items(): if name == 'SERVER_URI': value = '' setattr(settings, 'AUTH_LDAP_%s' % name, value) # Pass test database settings in environment for use by any management # commands that run from tests. for opt in ('ENGINE', 'NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'): os.environ['AWX_TEST_DATABASE_%s' % opt] = settings.DATABASES['default'][opt] # Set flag so that task chain works with unit tests. settings.CELERY_UNIT_TEST = True settings.SYSTEM_UUID = '00000000-0000-0000-0000-000000000000' settings.BROKER_URL = 'redis://localhost:55672/' settings.CALLBACK_QUEUE = 'callback_tasks_unit' # Disable socket notifications for unit tests. settings.SOCKETIO_NOTIFICATION_PORT = None # Make temp job status directory for unit tests. job_status_dir = tempfile.mkdtemp() self._temp_paths.append(job_status_dir) settings.JOBOUTPUT_ROOT = os.path.abspath(job_status_dir) settings.CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unittests' } } cache.clear() self._start_time = time.time()
def settings(self, **options): """ A context manager that temporarily sets a setting and reverts back to the original value when exiting the context. """ old_wrapped = settings._wrapped override = UserSettingsHolder(settings._wrapped) try: for key, new_value in options.items(): setattr(override, key, new_value) settings._wrapped = override yield finally: settings._wrapped = old_wrapped
def setUp(self): self._old_cwd = os.getcwd() self.project_dir = path.abspath(path.join(TESTS_DIR, 'django_example')) self.tmpdir = tempfile.mkdtemp() os.chdir(self.project_dir) sys.path.append(self.project_dir) # allow changing settings self.old_settings = settings._wrapped os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings' settings.INSTALLED_APPS # load settings on first access settings.DATABASES['default']['NAME'] = path.join( self.tmpdir, 'db.sqlilte3') # this goes around the "settings already loaded" issue. self.override = UserSettingsHolder(settings._wrapped) settings._wrapped = self.override
def enable(self): override = UserSettingsHolder(settings._wrapped) for key, new_value in self.options.items(): setattr(override, key, new_value) settings._wrapped = override
def __setattr__(self, name, value): UserSettingsHolder.__setattr__(self, name, value)
has_django14 = DJANGO_VERSION >= (1,4) # import and configure empty django settings if has_django: from django.conf import settings, LazySettings if not isinstance(settings, LazySettings): # this probably means django globals have been configured already, # which we don't want, since test cases reset and manipulate settings. raise RuntimeError("expected django.conf.settings to be LazySettings: %r" % (settings,)) # else configure a blank settings instance for the unittests if has_django0: if settings._target is None: from django.conf import UserSettingsHolder, global_settings settings._target = UserSettingsHolder(global_settings) elif not settings.configured: settings.configure() #============================================================================= # support funcs #============================================================================= # flag for update_settings() to remove specified key entirely UNSET = object() def update_settings(**kwds): """helper to update django settings from kwds""" for k,v in iteritems(kwds): if v is UNSET: if hasattr(settings, k):
def __setattr__(self, name, value): UserSettingsHolder.__setattr__(self, name, value) setting_changed.send(sender=name, setting=name, value=value)
def __setattr__(self, name, value): UserSettingsHolder.__setattr__(self, name, value) setting_changed.send(sender=self.__class__, setting=name, value=value)
import os from django.conf import settings, UserSettingsHolder settings = UserSettingsHolder(settings) def get(key, default): return getattr(settings, 'CMS_%s' % key, default) # Settings limits for Pages attachments settings.ATTACHMENT_TYPES = get('ATTACHMENT_TYPES', ('doc', 'docx', 'xls', 'xslx', 'pdf', 'rtf', 'zip',)) settings.ATTACHMENT_SIZE = get('ATTACHMENT_SIZE', 1024 * 1024 * 10) # In bites # Type of detecting regions settings.REGION_FINDER = get('REGION_FINDER', 'cms.regions.finder.URLFinder') # URL Builders for the link tag settings.URL_BUILDERS = get('URL_BUILDERS', ('cms.url_builders.RegionURLBuilder',)) settings.LOGIN_URL = get('LOGIN_URL', '/manage/login/') settings.LOGIN_REDIRECT_URL = get('LOGIN_REDIRECT_URL', 'cms:home') settings.ELEMENTS_ON_PAGE = get('ELEMENTS_ON_PAGE', 20) # Person thumbnail size settings.PERSON_THUMB_SIZE = get('PERSON_THUMB_SIZE', 55)
def __enter__(self): from django.conf import UserSettingsHolder override = UserSettingsHolder(settings._wrapped) setattr(override, 'ALLOW_FORM_PROCESSING_QUERIES', True) self.wrapped = settings._wrapped settings._wrapped = override