Changes between Version 12 and Version 13 of SplitSettings


Ignore:
Timestamp:
08/10/2010 08:20:55 AM (5 years ago)
Author:
CordiS
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SplitSettings

    v12 v13  
    333333    settings.update(config(**settings))
    334334}}}
     335
     336== Settings inheritance ==
     337
     338I ([http://djangopeople.net/cordis  Sergey Yushckeyev]) have extended proposed solution:
     339
     340env
     341{{{
     342export APP_SETTINGS = 'development.cordis'
     343}}}
     344
     345settings/__init__.py
     346{{{
     347#!python
     348
     349CONFIGURATION = 'common'
     350
     351def deep_update(from_dict, to_dict):
     352    for (key, value) in from_dict.iteritems():
     353        if key in to_dict.keys() and isinstance(to_dict[key], dict) and isinstance(value, dict):
     354            deep_update(value, to_dict[key])
     355        else:
     356            to_dict[key] = value
     357
     358try:
     359    modules = os.environ.get('APP_SETTINGS', '').split('.')
     360    current = __name__
     361    for name in modules:
     362        module  = getattr(__import__(current, globals(), locals(), [name]), name)
     363        current += '.' + name
     364        current_settings = {}
     365        for setting in dir(module):
     366            if setting == setting.upper():
     367                current_settings[setting] = getattr(module, setting)
     368        deep_update(current_settings, locals())
     369except ImportError, e:
     370    print 'Unable to import configuration: %s' % (str(e))
     371}}}
     372
     373settings/development/cordis.py
     374{{{
     375#!python
     376
     377CONFIGURATION = 'cordis'
     378}}}
     379
     380app/models.py
     381{{{
     382#!python
     383
     384from settings import CONFIGURATION
     385print CONFIGURATION # prints 'cordis'
     386}}}
Back to Top