Skip to main content

django-settings-custom

License: MIT Build Status Coverage Status PyPI version Versions supported Supports Wheel format Documentation Status

A Django interactive command for configuration file generation.

Getting It

The project is on PyPI (https://pypi.org/project/django-settings-custom/)

pip install django-settings-custom

Installing It

To enable django_settings_custom in your project you need to add it to INSTALLED_APPS in your projects settings.py file:

INSTALLED_APPS = (
    ...
    'django_settings_custom',
    ...
)

Using It

Create a template for your target conf.ini like

[DATABASE]
NAME = { USER_VALUE }
HOST = { USER_VALUE }
PORT = { USER_VALUE }

[DATABASE_CREDENTIALS]
USER = { USER_VALUE }
PASSWORD = { ENCRYPTED_USER_VALUE }

[DJANGO]
KEY = { DJANGO_SECRET_KEY }

# A constant field
[LDAP]
URL = 'ldaps://myldap'

Configure in Django settings

Add settings.py file

SETTINGS_TEMPLATE_FILE = 'PATH_TO_YOUR_TEMPLATE_CONFIGURATION_FILE'
SETTINGS_FILE_PATH = 'TARGET_FOR_CONFIGURATION_FILE'

Launch in command line

python manage.py generate_settings

Or all in command line

python manage.py generate_settings path/to/template/settings.ini target/path/of/settings.ini

Results

The command ask user to fill missing values from template:

[user@localhost a_project]$ ./manage.py generate_conf
** Configuration file generation: **

** Configuration file generation: **
Do you want to generate the secret key for Django ? (Y/n) : y
Django secret key generated

** Enter values for configuration file content **

Value for [DATABASE] NAME: database_name
Value for [DATABASE] HOST: database_host
Value for [DATABASE] PORT: 900
Value for [DATABASE_CREDENTIALS] USER: my_user
Value for [DATABASE_CREDENTIALS] PASSWORD (will be encrypted):

Writing file at /home/user/a_project/conf.ini:
Configuration file successfully generated.
[user@localhost a_project]$ 

It generates the file /home/user/a_project/conf.ini:

[DATABASE]
NAME = database_name
HOST = database_host
PORT = 900

[DATABASE_CREDENTIALS]
USER = my_user
PASSWORD = JbAwLj5Zwz8lMrvcUZq5sP/v6eaUFY5E7U8Fmg63vxI=

# A constant field
[LDAP]
URL = 'ldaps://monldap'

[DJANGO]
KEY = w)r13ne4=id9_8xdojir)3)%%5m3r$co#jwj_)4d*_%%!0+f#sro

And to decrypt values in your code (in settings.py for example), you may use django_settings_custom.encryption.decrypt :

import configparser
from django_settings_custom import encryption

config = configparser.RawConfigParser()
config.read(SETTINGS_FILE_PATH)
database_password = encryption.decrypt(config.get('DATABASE_CREDENTIALS', 'PASSWORD'))

To decrypt values, the function uses the django SECRET_KEY (must be set before).

Miscellaneous

If you don't want to use Django settings

If you don't want to add specific variables to your Django settings file, you can inherit generate_settings.Command to specify command options :

from django_settings_custom.management.commands import generate_settings


class Command(generate_settings.Command):
    settings_template_file = 'The/settings/template/file_path.ini'
    settings_file_path = 'The/target/settings/file_path.ini'

Adding custom tag

To add a custom tag, you can inherit generate_settings.Command and override the method get_value :

import random
from django_settings_custom.management.commands import generate_settings


class Command(generate_settings.Command):

    def get_value(self, section, key, value_type):
        if value_type == 'RANDOM_VALUE':
            return random.uniform(0, 100)
        return super(Command, self).get_value(section, key, value_type)

Or a little more complex example :

from django.core.management.base import CommandError
from django_settings_custom.management.commands import generate_settings


class Command(generate_settings.Command):

    def get_value(self, section, key, value_type):
        int_less_10 = value_type == 'INT_LESS_THAN_10'
        if int_less_10:
            value_type = 'USER_VALUE'
        value = super(Command, self).get_value(section, key, value_type)
        if int_less_10:
            try:
                value = int(value)
                if value >= 10:
                    raise CommandError('This field needs an int less than 10.')
            except ValueError:
                raise CommandError('This field needs an int.')
        return value

Documentation

Release files for django-settings-custom 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-settings-custom 1.1.1
File Size Uploaded
django-settings-custom-1.1.1.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-settings-custom 1.1.1
File Interpreter ABI Platform
django_settings_custom-1.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.4 kB

Release files / django-settings-custom-1.1.1.tar.gz

Download URL django-settings-custom-1.1.1.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bbaf19a784fc454e4e72164c9a9e3235eb9f5381fbf2476dc78ca049884608d0
BLAKE2b-256 checksum
How to use checksums
d9a0f9e48859ccc5fb7d82319103fdeb7e414d2a6f303b04c329bcfe8e9e5369
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release files / django_settings_custom-1.1.1-py2.py3-none-any.whl

Download URL django_settings_custom-1.1.1-py2.py3-none-any.whl
Size 11.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
453ae8388aa0ea63c5e4ccda46c751fee7284f9586ea654438cc5ad10c0029b1
BLAKE2b-256 checksum
How to use checksums
25d4dd0f6d71d0e2643772925328a8142b6784e54fa79f2fdfe4d02f09d9ce05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page