Skip to content

Instantly share code, notes, and snippets.

@bubenkoff
Created June 3, 2014 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bubenkoff/005ea57b63251dafe81f to your computer and use it in GitHub Desktop.
Save bubenkoff/005ea57b63251dafe81f to your computer and use it in GitHub Desktop.
Utility script which switches the django settings module. Usage: setup_django_settings('myproject.settings')
import os
import sys
from django.utils.importlib import import_module
from django.core.urlresolvers import clear_url_caches
from django.template import context, base, loader
from django.utils import translation
from django.utils.translation import trans_real
def setup_django_settings(test_settings):
"""Override the enviroment variable and call the _setup method of the settings object to reload them."""
os.environ['DJANGO_SETTINGS_MODULE'] = test_settings
from django.conf import settings as django_settings
if test_settings:
# reload settings
reload_settings(django_settings)
else:
# just settings cleanup, no reload
django_settings._wrapped = None
def reload_settings(settings):
"""Special routine to reload django settings, including:
urlconf module, context processor, templatetags settings, database settings.
This also includes re-setting up sqlalchemy database settings. Environment chosen is always TestEnv."""
# resetup django settings
settings._setup()
# check if there's settings to reload
if hasattr(settings, 'ROOT_URLCONF'):
if settings.ROOT_URLCONF in sys.modules:
reload(sys.modules[settings.ROOT_URLCONF])
import_module(settings.ROOT_URLCONF)
settings.LANGUAGE_CODE = 'en' # all tests should be run with English by default
# Make the ConnectionHandler use the new settings, otherwise the ConnectionHandler will have old configuraton.
from django.db.utils import ConnectionHandler
import django.db
from django.db.utils import load_backend
import django.db.transaction
import django.db.models
import django.db.models.sql.query
import django.core.management.commands.syncdb
import django.db.models.sql.compiler
import django.db.backends
import django.db.backends.mysql.base
import django.core.management.commands.loaddata
# all modules which imported django.db.connections should be changed to get new ConnectionHanlder
django.db.models.sql.compiler.connections = django.db.models.connections = \
django.core.management.commands.loaddata.connections = \
django.db.backends.connections = django.db.backends.mysql.base.connections = \
django.core.management.commands.syncdb.connections = django.db.transaction.connections = \
django.db.connections = django.db.models.base.connections = django.db.models.sql.query.connections = \
ConnectionHandler(settings.DATABASES)
# default django connection and backend should be also changed
django.db.connection = django.db.connections[django.db.DEFAULT_DB_ALIAS]
django.db.backend = load_backend(django.db.connection.settings_dict['ENGINE'])
# reporting database settings
from paylogic.privates import reporting_readslave
backend = load_backend('django.db.backends.' + reporting_readslave.REPORTING_ENGINE)
reporting_readslave.READ_CONNECTION = 'readslave'
django.db.connections._connections[reporting_readslave.READ_CONNECTION] = backend.DatabaseWrapper({
'HOST': reporting_readslave.REPORTING_HOST,
'NAME': reporting_readslave.REPORTING_NAME,
'PASSWORD': reporting_readslave.REPORTING_PASSWORD,
'OPTIONS': '',
'PORT': reporting_readslave.REPORTING_PORT,
'USER': reporting_readslave.REPORTING_USER,
'TIME_ZONE': settings.TIME_ZONE,
})
import django.core.cache
django.core.cache.cache = django.core.cache.get_cache(django.core.cache.DEFAULT_CACHE_ALIAS)
# clear django urls cache
clear_url_caches()
# clear django contextprocessors cache
context._standard_context_processors = None
# clear django templatetags cache
base.templatetags_modules = None
# reload translation files
reload(translation)
reload(trans_real)
# clear django template loaders cache
loader.template_source_loaders = None
from django.template.loaders import app_directories
reload(app_directories)
@hbruno
Copy link

hbruno commented Aug 11, 2017

Hi dude!,
Do you have an update of this script but for django 1.11 ?

I need to change my default DATABASE live :D.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment