Skip to content

This package provides a smart settings object that searches for app settings in project level settings with provided prefix.

License

Notifications You must be signed in to change notification settings

NikBelyaev/django-app-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-app-settings

This package allows the apps to set its own settings and search for them in the project with the specified prefix. This approach makes it convenient to implement app settings in any project with classic way and not spend time to write your own settings module.

In Adding to your app section described the way how you can use this package in your own app.

In Project level usage section you will be informed about what you need to tell to your app users to set the settings correct.

Installation

  1. Clone repo:
git clone https://github.com/1mace1/django-app-settings.git
  1. Change directory:
cd django-app-settings/
  1. Make a package:
python setup.py sdist
  1. Install with pip:
pip install dist/django-app-settings-*.tar.gz
  1. Add django-app-settings to your package requirements

Adding to your app

  1. Import Settings in to your app settings.py:
from django_app_settings import Settings
  1. Set the settings names that are required in your app:
settings = (
    'SETTING1',
    'SETTING2',
    ...
)

Warning

All the settings names should be uppercase!

  1. (optional) Set defaults for your settings so that the setting can fall back to defaults if it is not found in project:
defaults = {
    'SETTING1': 'value',
    ...
}
  1. (optional) Set the app_prefix in order to be able to find your settings in the project using this prefix:
app_prefix = 'MYAPP'

Note

This is an optional step, so you don't have to set app_prefix, but I strongly recommend you do this so that there are no conflicts with other project settings

  1. (optional) If you have constant settings that don't need to be changed, you can use internal_settings. User at the project level will not have access to these settings:
internal_settings = {
    'INTERNAL1': 'VALUE1',
    ...
}
  1. Create Settings object:
app_settings = Settings(settings, app_prefix=app_prefix, defaults=defaults, internal_settings=internal_settings)

Summing up, your settings.py should be:

from django_app_settings import Settings

app_prefix = 'MY_APP'
settings = (
    'SETTING1',
    'SETTING2'
)
defaults = {
    'SETTING1': 'value'
}
internal_settings = {
    'INTERNAL1': 'VALUE1',
}

app_settings = Settings(settings, app_prefix=app_prefix, defaults=defaults, internal_settings=internal_settings)

You can skip the optional steps and set the settings only so that your app_settings are like this:

from django_app_settings import Settings

settings = (
    'SETTING1',
    'SETTING2'
)

app_settings = Settings(settings)

Usage in app

For now, you can import app_settings anywhere and use as below

from your_app.settings import app_settings

app_settings.SETTING1

All the settings in settings tuple are mandatory. So if default value for particular setting are not specified and there is no setting in project level, the user will get an error. Therefore, please, warn users of your app to set the required settings as shown below in Project level usage.

Project level usage

People who will use your app with this package should set settings in their project level settings.py with your app_prefix or just leave them. So if your app_prefix is SOME_APP as above then project level settings should be:

MY_APP_SETTING1 = 'some value'
MY_APP_SETTING2 = 'another value'

About

This package provides a smart settings object that searches for app settings in project level settings with provided prefix.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages