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.

Installation

pip install django-app-settings

Documentation

On ReadTheDocs

Development

To run all the tests: tox. See CONTRIBUTING.

Quick usage

# Define your settings class
import appsettings


class MySettings(appsettings.AppSettings):
    boolean_setting = appsettings.BooleanSetting(default=False)
    required_setting = appsettings.StringSetting(required=True)
    named_setting = appsettings.IntegerSetting(name='integer_setting')
    prefixed_setting = appsettings.ListSetting(prefix='my_app_')

    class Meta:
        setting_prefix = 'app_'


# Related settings in settings.py
APP_INTEGER_SETTING = -24
MY_APP_PREFIXED_SETTING = []


# Instantiate your class wherever you need to
appconf = MySettings()
assert appconf.boolean_setting is False  # True (default value)
assert appconf.required_setting == 'hello'  # raises AttributeError
assert appconf.named_setting < 0  # True
assert appconf.prefixed_setting  # False (empty list)


# Values are cached to avoid perf issues
with override_settings(APP_REQUIRED_SETTING='hello',
                       APP_INTEGER_SETTING=0):
    # ...but cache is cleaned on Django's setting_changed signal
    assert appconf.required_setting == 'hello'  # True
    assert appconf.named_setting < 0  # False


# You can still access settings through the class itself (values not cached)
print(MySettings.boolean_setting.get_value())  # explicit call
print(MySettings.boolean_setting.value)  # with property


# Run type checking and required presence on all settings at once
MySettings.check()  # raises Django's ImproperlyConfigured (missing required_setting)
# MySettings.check() is best called in django.apps.AppConfig's ready method

You can easily create your own Setting classes for more complex settings.

import re
import appsettings


class RegexSetting(appsettings.Setting):
    def checker(self, name, value):
        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)' % name)

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

Please check the documentation to see even more advanced usage.

License

Software licensed under ISC license.

Changelog

0.3.0 (2017-11-30)

Going from alpha to beta status. Logic has been reworked.

  • An instance of a subclass of AppSettings will now dynamically get settings values from project settings, and cache them. This allows to use the instance the same way in code and tests, without performance loss. See issue #16.

  • Cache is invalidated when Django sends a setting_changed signal (i.e. when using TestCase or override_settings). See issue #16.

  • Setting main class now accepts callable as default value, and two new parameters to keep control on its behavior: call_default, which tells if the default value should be called (if callable) or not, and transform_default, which tells if the default value should be transformed as well by the transform method. See issue #17.

  • Settings type checkers now have custom parameters like max_length, empty or key_type, that can be passed directly through the settings classes as keyword arguments. Check the documentation for more information.

  • Settings classes have been rewritten more explicitly, using class inheritance instead of hard-to-debug generators. Composed types like float lists or boolean sets have been removed in favor of more flexible list, set and tuple types which now accept an optional item_type parameter.

  • ImportedObjectSetting has been renamed ObjectSetting, and now supports object paths down to arbitrary level of nesting. Before, it only supported object paths down to classes or functions, now you can for example give it the path to a constant in a class within a class, itself contained in a module within a package. It will work as long a the deepest module is importable through importlib.import_module and each object down to the last is obtainable through getattr method.

Many thanks to ziima for having shared good ideas and thoughts!

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.3.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

django_app_settings-0.3.0-py2.py3-none-any.whl (13.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-app-settings-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e802cc57b96aaddb2eefeb3ccf2c037fd4d3843d028f3e08dba8420b89be8020
MD5 9293e9d558a4424383ca6256d0ef64a7
BLAKE2b-256 5ac3dbe757bf0ac53b7caacc25dfa455807efc8674a501e0c25ba03c0d797216

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_app_settings-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0dd591e206d9e29a8375e1b927d6fafcbc762cccc8e4b7eab8e2b2aa6c9c90bc
MD5 2b80c97aa0a467bf695c547e8dd86395
BLAKE2b-256 48c103d1c1c69b1ccb21fd995801cd26aac51652284f4be4e23f5e3674f6012f

See more details on using hashes here.

Provenance

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