Skip to main content

Application settings helper for Django apps.

Project description

Django AppSettings

Application settings helper for Django apps.

Why another app settings app? Because none of the other suited my needs!

This one is simple to use, and works with unit tests overriding settings.

Quick usage

# my_package/apps.py

from django.apps import AppConfig
import appsettings as aps


class AppSettings(aps.AppSettings):
    my_setting = aps.Setting(name='basic_setting', default=25)

    required_setting = aps.Setting(required=True)  # name='REQUIRED_SETTING'

    typed_setting = aps.StringSetting(prefix='string_')
    # -> typed_setting.full_name == 'STRING_TYPED_SETTING'

    custom_setting = RegexSetting()  # -> see RegexSetting class below

    class Meta:
      # default prefix for every settings
      setting_prefix = 'example_'


class RegexSetting(Setting):
    def check():
        value = self.get_raw()  # should always be called to check required condition
        if value != self.default:  # always allow default to pass
            re_type = type(re.compile(r'^$'))
            if not isinstance(value, (re_type, str)):
                # raise whatever exception
                raise ValueError('%s must be a a string or a compiled regex '
                                 '(use re.compile)' % self.full_name)

    def transform(self):
        value = self.get_raw()
        # ensure it always return a compiled regex
        if isinstance(value, str):
            value = re.compile(value)
        return value


class MyAppConfig(AppConfig):
    name = 'my_app'
    verbose_name = 'My Application'

    def ready(self):
        # check every settings at startup, raise one exception
        # with all errors in its message
        AppSettings.check()
# django_project/settings.py
EXAMPLE_BASIC_SETTING = 26
EXAMPLE_REQUIRED_SETTING = 'something'
# my_package/other_module.py

from .apps import AppSettings


regex = AppSettings.custom_setting.get()  # alias for transform()

# instantiate the class to load each and every settings
appsettings = AppSettings()
appsettings.my_setting == 25  # False: 26

Settings classes:

  • StringSetting: default = ‘’

  • IntegerSetting: default = 0

  • PositiveIntegerSetting: default = 0

  • BooleanSetting: default = False

  • FloatSetting: default = 0.0

  • PositiveFloatSetting: default = 0.0

  • ListSetting: default = []

  • SetSetting: default = ()

  • DictSetting: default = {}

  • ImportedObjectSetting: default = None

Are the following settings useful? Please tell me on Gitter.

  • StringListSetting: default = []

  • StringSetSetting: default = ()

  • IntegerListSetting: default = []

  • IntegerSetSetting: default = ()

  • BooleanListSetting: default = []

  • BooleanSetSetting: default = ()

  • FloatListSetting: default = []

  • FloatSetSetting: default = ()

License

Software licensed under ISC license.

Installation

pip install django-app-settings

Documentation

On ReadTheDocs

Development

To run all the tests: tox

Changelog

0.2.5 (2017-06-02)

  • Add six dependency (now required).

  • Rename Int settings to Integer, and Bool ones to Boolean.

  • Remove metaclass generated getters and checkers.

0.2.4 (2017-05-02)

  • Settings are not checked when they default to the provided default value.

  • Settings classes received better default values corresponding to their types.

0.2.3 (2017-05-02)

  • Add full_name property to Setting class.

  • Add required parameter to Setting class (default False).

0.2.2 (2017-04-17)

  • Import settings classes in main module to simplify imports.

0.2.1 (2017-04-17)

  • Add PositiveInt and PositiveFloat settings.

  • Add support for Django 1.11.

  • Implement basic settings classes.

0.2.0 (2017-04-17)

  • Implement basic Setting class.

  • Pin dependencies.

  • Change distribution name to app-settings.

0.1.0 (2017-03-23)

  • Alpha release on PyPI.

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-app-settings-0.2.5.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

django_app_settings-0.2.5-py2.py3-none-any.whl (8.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-app-settings-0.2.5.tar.gz.

File metadata

File hashes

Hashes for django-app-settings-0.2.5.tar.gz
Algorithm Hash digest
SHA256 0dbc5cc5bc94a2922aee7e34e2ab9ea665bc7f297db11fb4f951a0848b21c450
MD5 d162ff045039d8c1879d2fb699082aac
BLAKE2b-256 5118e1cb08cdfc2a733295c2868d7f54d19d5dc23b17e29aac7396f75e8b1375

See more details on using hashes here.

File details

Details for the file django_app_settings-0.2.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_app_settings-0.2.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 42c89e6840b15f9c6c740d4e35fea900cd81156d6aa8012f079124f1b2c722f6
MD5 d15d8da39ba12556269bbdd2e008c371
BLAKE2b-256 44849ab3fcfe9831c71809a8a01279d1cf1adbbd3ffc63cc7cc2fbefbb3afc23

See more details on using hashes here.

Supported by

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