Skip to main content

Stay informed of it

Project description

Django Dynamic Settings

Build status PyPI PyPI - Django version PyPI - Python version PyPI - License

Django Dynamic Settings allows you to create & use dynamic settings backed by a database.

Installation

Installation using pip:

pip install dj-dynamic-settings

dj_dynamic_settings app has to be added to INSTALLED_APPS and migrate command has to be run.

INSTALLED_APPS = (
    # other apps here...
    "dj_dynamic_settings",
)

dj_dynamic_settings.urls must be included to a desired url path.

urlpatterns = [
    ...,
    url(r"^api/v1/", include("dj_dynamic_settings.urls")),
]

Setting class must be defined & registered. Please make sure that this class' module runs whenever the application runs.

from dj_dynamic_settings.registry import BaseSetting, registry
from dj_dynamic_settings.validators import TypeValidator


@registry.register
class FeatureActive(BaseSetting):
    key = "FEATURE_ACTIVE"
    validators = [TypeValidator(bool)]
    default = False
    description = "Flag for Feature X"

Create Setting instance using view.

import requests

requests.post(
    url="https://your-app.com/api/v1/dynamic_settings/",
    headers={
        "Authorization": "Token <secret-login-token>",
    },
    json={
        "key": "FEATURE_ACTIVE",
        "value": True,
        "is_active": True,
    }
)

Access this setting as in django.conf.settings

from dj_dynamic_settings.conf import settings


settings.FEATURE_ACTIVE  # True

Create / Update Triggers

To fire a callback method when a specific setting value updated or created, you can implement post_save_actions in BaseSetting inherited class

Following example shows how to implement post_save_actions method.

The callback method will be called with following kwargs:

key=instance.key
value=instance.value
created=created # is create operation

Note: post_save_actions returns an array, so you can add multiple callback methods. These callback methods will be called synchronously.

class PostUpdateTestConfiguration(BaseSetting):
    key = "X_FEATURE_POST_UPDATE"
    validators = [...]

    @classmethod
    def post_save_actions(cls):
        return [
            on_data_updated,
        ]

def on_data_updated(*args, **kwargs):
    pass

Testing Tools

override_settings()

You can override a setting for a test method or test class.

from dj_dynamic_settings.utils import override_settings
from django.test import TestCase

@override_settings(SOME_SETTING="some_setting")
class FeatureTestCase(TestCase):

    @override_settings(SOME_OTHER_SETTING="SOME_OTHER_SETTING")
    def test_feature(self):
        # Some stuff
        pass

    
    def test_feature_x(self):
        with override_settings(SOME_OTHER_SETTING="SOME_OTHER_SETTING"):
            # Some stuff
            pass

Download files

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

Source Distribution

dj-dynamic-settings-0.2.10.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

dj_dynamic_settings-0.2.10-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file dj-dynamic-settings-0.2.10.tar.gz.

File metadata

  • Download URL: dj-dynamic-settings-0.2.10.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for dj-dynamic-settings-0.2.10.tar.gz
Algorithm Hash digest
SHA256 e66d3811f85ddaf27e5971794b7a47243e2c43751625628372525bd121230ae6
MD5 ea9820c3baf9cdee74f4d8ba14172ef7
BLAKE2b-256 b6ccdf3ee077fbcf3005011f9f63f74fd8cf6f4178c069a67beb5753a4787372

See more details on using hashes here.

File details

Details for the file dj_dynamic_settings-0.2.10-py3-none-any.whl.

File metadata

File hashes

Hashes for dj_dynamic_settings-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 6da6d9f45f13cadb6a57fc196a00b847308f82b6c81d8ddb6fed89634747b811
MD5 2cadcc2228ce8bdc770810296c2425dc
BLAKE2b-256 eacbfb3b3e4d1cb74c74ab0bdc00fdd0e4887fd2c712c46425c4470389160a49

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