Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active January 1, 2016 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/8095260 to your computer and use it in GitHub Desktop.
Save miraculixx/8095260 to your computer and use it in GitHub Desktop.
django-multiconfig. Enable environment specific configurations and configuration mix-ins for your django project. With any Django project your settings.py file quickly grows beyond managability. A modular approach is needed and that's what this Gist does for you.

#Django Multiple Modular Configuration

Enables modular settings.py configurations and configuration mix-ins for your django project. With any Django project your settings.py file quickly grows beyond managability. A modular approach is needed and that's what this Gist does for you.

##Installation

  1. create package config
  2. add this gist's config_base.py
  3. create a global configuration class EnvSettings_GLOBAL(EnvSettingsBase)
  4. from your settings.py cut/paste all global settings into the new class
  5. add the following two lines in settings.py:
os.environ.setdefault('DJANGO_CONFIGURATION', 'EnvSettings_GLOBAL')
EnvSettingsBase.setup(globals(), os.environ['DJANGO_CONFIGURATION'])

##Usage Whenever you see the need for a specific configuration (such as per development, test and production environment), create a new class:

class EnvSettings_Local(EnvSettings_GLOBAL):
    SOME_SETTING = `value` 

Enable this new configuration using the process' environment, e.g.

export DJANGO_CONFIGURATION=EnvSettings_Local

###Mix-ins To collect all settings applicable to a specific extension or for a specific situation, simply create a mix-in class. The following example collects all settings to enable site-wide HTTPs using the SecureRequiredMiddleware:

 from config.env_global import EnvSettings_GLOBAL
 class Config_SiteHttps(object):
    #A configuration mix in to enable full site encryption
    #Overred the SECURE_REQUIRED_PATHS to set sub-site paths only
    #
    #MAKE SURE YOU LIST THE MIXIN *BEFORE* THE PARENT CLASS, e.g.
    #
    #class EnvSettings_SecureSite(Config_SiteHttps, EnvSettings_GLOBAL):
    #  pass
    #
    # setup the middleware for HTTPs. Secure middle ware must be in the 
    # beginning!
    SSL_MIDDLEWARE_CLASSES = ('middleware.SecureRequiredMiddleware',)
    MIDDLEWARE_CLASSES = SSL_MIDDLEWARE_CLASSES
    MIDDLEWARE_CLASSES += EnvSettings_GLOBAL.MIDDLEWARE_CLASSES
    # set the paths to be secured -- defaults to all requests
    HTTPS_SUPPORT = True
    SECURE_REQUIRED_PATHS = ('*',)
'''
Created on Oct 27, 2013
@author: miraculixx
'''
import inspect
def isuppercase(name):
return name == name.upper() and not name.startswith('_')
class EnvSettingsBase(object):
"""
Setup environment specific settings variables. This will
get all UPPERCASE class variables from the specified class
into the module-level variables in settings.
Use in settings.py:
a) EnvSettingsBase.setup(globals(), "MyEnvSettings_classname")
b) EnvSettingsBase.setup(globals(), os.environ['DJANGO_CONFIGURATION'])
c) EnvSettingsBase.setup(globals(), os.environ['DJANGO_CONFIGURATION'], "config_module_name")
The default config module is "config"
"""
@classmethod
def setup(cls, globals, env_class=None, config_mod="config", ):
try:
mod = __import__(config_mod, env_class)
settings_cls = getattr(mod, env_class)
except Exception as e:
print "ERROR: Could not find %s.%s: %s" % (config_mod, env_class, e)
return
#for m in sys.modules:
# print "%s" % m
# loop through all base classes in reverse class lookup manner
# (from top to bottom) so that overrides are possible
#print "Applying settings from %s.%s" % (config_mod, env_class)
for base_cls in reversed(inspect.getmro(settings_cls)):
for var in base_cls.__dict__:
if isuppercase(var):
#print "Setting %s from %s" % (var, base_cls)
globals[var] = base_cls.__dict__[var]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment