Skip to main content

A Django interactive command for configuration file generation.

Project description

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.ConfigParser()
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):

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

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):

    @staticmethod
    def get_value(section, key, value_type, secret_key):
        int_less_10 = value_type == 'INT_LESS_THAN_10'
        if int_less_10:
            value_type = 'USER_VALUE'
        value = super(Command, Command).get_value(section, key, value_type, secret_key)
        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

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-settings-custom-1.0.9.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_settings_custom-1.0.9-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-settings-custom-1.0.9.tar.gz.

File metadata

  • Download URL: django-settings-custom-1.0.9.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for django-settings-custom-1.0.9.tar.gz
Algorithm Hash digest
SHA256 7889c5a88424a2ccdd35ef72e3224476b53d20dd6cd31a30d3f44676b3859151
MD5 613a1bfe00e96b7d01d4a31144fb27f8
BLAKE2b-256 021c313c098d5d75025596be1facd465c25fb1ec41c7360a7182a90d75701e22

See more details on using hashes here.

File details

Details for the file django_settings_custom-1.0.9-py2.py3-none-any.whl.

File metadata

  • Download URL: django_settings_custom-1.0.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for django_settings_custom-1.0.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 62aa9d2bb0fb87416d4c6ce2ebce6a8b1db960575fb338dd5f2ae7ab4230dcf4
MD5 7111dd10941b0427f7c2243f50cf7dd7
BLAKE2b-256 65357b35f048eb7a3c4efa78f00cd001b6ff428ec4c985942bb933ed89f6db96

See more details on using hashes here.

Supported by

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