djangofloor.conf.config_values
¶
Complex settings at runtime¶
Allow to define settings based on references to other settings (overriden in other config files).
Examples:
# file1: djangofloor.config.defaults
from djangofloor.config.config_values import SettingReference
DEBUG = False
TEMPLATE_DEBUG = SettingReference('DEBUG')
# file1: myproject.defaults
DEBUG = True
Since the second file overrides the first one, TEMPLATE_DEBUG always has the same value as DEBUG (True).
-
class
djangofloor.conf.config_values.
AutocreateFile
(value, mode=None, *args, **kwargs)[source]¶ Represent a file name. Its parent directory is automatically created by the “collectstatic” command.
-
class
djangofloor.conf.config_values.
AutocreateFileContent
(value, create_function, mode=None, *args, **kwargs)[source]¶ Return the content of an existing file, or automatically write it and returns the content of the created file. Content must be a unicode string. The first argument of the provided create_function is the name of the file to create.
The file is only written when the “migrate” Django command is called. The first arg of the provided create_function is a bool, in addition of your own *args and **kwargs:
- True when Django is ready and your function is called for writing the file during the “migrate” command
- False when Django is not ready and your function is called for loading settings
-
get_value
(merger, provider_name: str, setting_name: str)[source]¶ Return the value
Parameters: - merger (
djangofloor.utils.SettingMerger
) – merger object, with all interpreted settings - provider_name – name of the provider containing this value
- setting_name – name of the setting containing this value
- merger (
-
class
djangofloor.conf.config_values.
CallableSetting
(value, *required)[source]¶ Require a function(kwargs) as argument, this function will be called with all already computed settings in a dict.
>>> SETTING_1 = True >>> def inverse_value(values): >>> return not values['SETTING_1'] >>> SETTING_2 = CallableSetting(inverse_value, 'SETTING_1')
In local_settings.py
>>> SETTING_1 = False
In your code:
>>> from django.conf import settings >>> if hasattr(settings, 'SETTING_2'): >>> assert(settings.SETTING_1 is False) # default value is overriden by local_settings.py >>> assert(settings.SETTING_2 is True) # SETTING_2 value is dynamically computed on startup
Extra arguments must be strings, that are interpreted as required settings, that must be available before the call to your function. You can also set an attribute called required_settings.
>>> def inverse_value(values): >>> return not values['SETTING_1'] >>> inverse_value.required_settings = ['SETTING_1'] >>> SETTING_2 = CallableSetting(inverse_value)
-
class
djangofloor.conf.config_values.
ConfigValue
(value)[source]¶ Base class for special setting values. When a setting is a
djangofloor.settings.ConfigValue
, then the method get_value(merger) is called for getting the definitive value.-
get_value
(merger, provider_name: str, setting_name: str)[source]¶ Return the intepreted value
Parameters: - merger (
djangofloor.utils.SettingMerger
) – merger object, with all interpreted settings - provider_name – name of the provider containing this value
- setting_name – name of the setting containing this value
- merger (
-
post_collectstatic
(merger, provider_name, setting_name, value)[source]¶ Called after the “collectstatic” command
-
post_migrate
(merger, provider_name, setting_name, value)[source]¶ Called after the “migrate” command
-
-
class
djangofloor.conf.config_values.
Directory
(value, mode=None)[source]¶ Represent a directory on the filesystem, that is automatically created by the “migrate” and “collectstatic” commands
-
class
djangofloor.conf.config_values.
ExpandIterable
(value, func=None)[source]¶ Allow to import an existing list inside a list setting. in defaults.py:
>>> LIST_1 = [0, ] >>> LIST_2 = [1, ExpandIterable('LIST_1'), 2, ] >>> DICT_1 = {0: 0, } >>> DICT_2 = {1: 1, None: ExpandIterable('DICT_1'), 2: 2, }
In case of dict, the key is ignored when the referenced dict is expanded. In local_settings.py
>>> LIST_1 = [3, ] >>> DICT_1 = {3: 3, }
In your code:
>>> from django.conf import settings
Then settings.LIST_2 is equal to [1, 3, 2]. settings.DICT_2 is equal to {1: 1, 2: 2, 3: 3, }.
-
class
djangofloor.conf.config_values.
File
(value, mode=None)[source]¶ Represent a file name. Its parent directory is automatically created by the “migrate” and “collectstatic” command.
-
class
djangofloor.conf.config_values.
Path
(value, mode=None)[source]¶ Represent any path on the filesystem.
-
class
djangofloor.conf.config_values.
RawValue
(value)[source]¶ Return the value as-is. Since by defaults all string values are assumed to be formatted string, you need to use
RawValue
for using values that should be formatted.from djangofloor.conf.config_values import RawValue SETTING_1 = '{DEBUG}' # will be transformed to 'True' or 'False' SETTING_2 = Raw('{DEBUG}') # will be kept as '{DEBUG}'
-
get_value
(merger, provider_name: str, setting_name: str)[source]¶ Return the non-intepreted value
Parameters: - merger (
djangofloor.utils.SettingMerger
) – merger object, with all interpreted settings - provider_name – name of the provider containing this value
- setting_name – name of the setting containing this value
- merger (
-
-
class
djangofloor.conf.config_values.
SettingReference
(value, func=None)[source]¶ Reference any setting object by its name. Allow to reuse a list defined in another setting file.
in defaults.py:
>>> SETTING_1 = True >>> SETTING_2 = SettingReference('SETTING_1')
In local_settings.py
>>> SETTING_1 = False
In your code:
>>> from django.conf import settings
Then settings.SETTING_2 is equal to False