public
Last active

Django settings file as loader for server specific settings files

  • Download Gist
settings.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#
# 1. Imports base settings from `/serversettings/base.py`
# 2. Imports settings based on server name, unless:
# a. The environmental setting `SERVER_TYPE` has been set in the `django.wsgi` script
# b. The name of the server corresponds to a name in the `DEVELOPMENT_MACHINES` tuple
#
import os
import platform
import sys
import warnings
 
from django.utils.importlib import import_module
 
DEVELOPMENT_MACHINES = ('christian-mac',)
TESTING_MACHINES = ('alpha','beta',)
PRODUCTION_MACHINES = ('echo',)
 
def get_server_name():
server_type = os.environ.get('SERVER_TYPE','')
server_name = platform.node().split('.')[0]
if server_type:
return server_type
if server_name in DEVELOPMENT_MACHINES:
return 'local'
if server_name in TESTING_MACHINES:
return 'testing'
if server_name in PRODUCTION_MACHINES:
return 'production'
else:
return server_name
def override_settings(dottedpath):
sys.path.append(os.sep.join(os.path.abspath(os.path.dirname(__file__)).split(os.sep)[:-1]))
try:
_module = import_module(dottedpath)
except ImportError, e:
warnings.warn("Failed to import %s" % dottedpath)
warnings.warn("Exact error was: %s" % e)
else:
_thismodule = sys.modules[__name__]
for _keyword in dir(_module):
if _keyword.isupper() and not _keyword.startswith('__'):
setattr(_thismodule, _keyword, getattr(_module, _keyword))
 
override_settings('mysite.serversettings.base')
override_settings('mysite.serversettings.%s' % get_server_name())

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.