Skip to main content

Utility for defining external django configs (for end user)

Project description

(Works with django>=1.9, and possibly lower versions, but it wasn’t tested, use at your own discretion)

This is a simple library which was build out of personal necessity.

As good as django is as a framework, it does not provide any ‘out of the box’ plugin for extending settings.py with end user config files. Just to make sure - I do not treat python files as settings - giving end user ability to run arbitrary code in the application context is a great way to bring disaster to whole system.

On the other hand settings.py contains a lot of code and settings which are not really up for configuration (middleware, installed apps, this is clearly for the developer).

For a couple of smaller and larger projects now I had to manually write some code which was responsible for reading external config files (usually .ini style) and inserting them into settings.py, in some cases also some code for generating stub configs was required.

So at some point I decided - “Why shouldn’t I make this into a reusable app?” - and this is how this library was made.

The main purpose of this library is to let you specify end user config in a declarative way:

config = django_configure.define({
    'Common': {
        'secret': django_configure.type.String('Secret for the application', default=generate_secret()),
        'static_root': django_configure.type.Path('Static root path (static files will be copied here)',
                                               default='/var/lib/myapp/static/'),
        'static_url': django_configure.type.Path('Url to static files', default='/static/'),
        'media_root': django_configure.type.Path('Media root path (media files will be stored here)',
                                              default='/var/lib/myapp/media/'),
        'media_url': django_configure.type.Path('Url to media files', '/media/'),
        'debug': django_configure.type.Boolean(help='if true debug mode will be enabled for the application, do not switch this in production', default=False)
    },
    'Database': {
        'url': django_configure.type.Database('Url to access database (including credentials)',
                                           default='sqlite:////var/lib/myapp/myapp.sqlite')
    }
}, env_prefix='MYAPP')

Afterwards you can assign values to settings variables as follows:

SECRET_KEY=config.get('Common.secret')

As the fields in the config file are type aware they do a good job at converting string to booleans, or numbers:

DEBUG=config.get('Common.debug')

For convenience sake function for generating default config:

config = django_configure.default('myapp', env_prefix='MYAPP')

Above code will generate config object with following fields (secret will be auto generated):

{
    'Common': {
        'secret': django_configure.type.String('Secret for the application', default=generate_secret()),
        'static_root': django_configure.type.Path('Static root path (static files will be copied here)',
                                               default='/var/lib/' + app_name + '/static/'),
        'static_url': django_configure.type.Path('Url to static files', default='/static/'),
        'media_root': django_configure.type.Path('Media root path (media files will be stored here)',
                                              default='/var/lib/' + app_name + '/media/'),
        'media_url': django_configure.type.Path('Url to media files', '/media/'),
        'debug': django_configure.type.Boolean(help='if true debug mode will be enabled for the application, do not switch this in production', default=False)
    },
    'Database': {
        'url': django_configure.type.Database('Url to access database (including credentials)',
                                           default='sqlite:////var/lib/'+app_name+'/'+app_name+'.sqlite')
    }
}

You can redefine / append parts of this via config.append(additional_field):

config = django_configure.default('myapp', env_prefix='MYAPP')
config.append({'Common': {'media_root': '/my/secret/storage'}})

Config specified as follows can be read from environmental variable (env_prefix+CONFIG, in the case of above code it will be MYAPP_CONFIG). Config path can also be specified in config definition via default_path variable, but it’s not recomended, as it hardcodes path into settings.py which is preciusly what we want to avoid.

After defining config in settings.py there is a possibility to generate template config and wsgi file by running manage.py command:

python manage.py createconfig <config_path> <wsgi_path>

Path to the config file will be hardcoded into wsgi file, so be mindful (You can always manually edit wsgi file later)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-configure-0.2.5.tar.gz (9.7 kB view details)

Uploaded Source

File details

Details for the file django-configure-0.2.5.tar.gz.

File metadata

File hashes

Hashes for django-configure-0.2.5.tar.gz
Algorithm Hash digest
SHA256 c15f894b6a8d5ca1d805b6951c70bf8f5c12ed7a572f499a2384fae60550554e
MD5 7f29213ced71b4efc55f6a5e3c697aa4
BLAKE2b-256 f85dba00179eceedda969fbffe047678d37127f9cc40bfafd7e4f1f1854fe46e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page