djblets.siteconfig.models¶
Database models for storing site configuration.
- SiteConfigurationSettingsValue¶
An alias for valid value types in site configuration settings.
New in version 3.1.
alias of
Union
[JSONDict
,JSONDictImmutable
,JSONList
,JSONListImmutable
,None
,bool
,float
,int
,str
]
- SiteConfigurationSettings¶
An alias for the container of settings in a site configuration.
New in version 3.1.
alias of
Dict
[str
,Union
[JSONDict
,JSONDictImmutable
,JSONList
,JSONListImmutable
,None
,bool
,float
,int
,str
]]
- class SiteConfigSettingsWrapper(siteconfig: SiteConfiguration)¶
Bases:
object
Wraps the settings for a SiteConfiguration.
This is used by the context processor for templates to wrap accessing settings data, properly returning defaults.
- __init__(siteconfig: SiteConfiguration) None ¶
Initialize the wrapper.
- Parameters:
siteconfig (
SiteConfiguration
) – The site configuration to wrap.
- siteconfig: SiteConfiguration¶
The site configuration associated with this wrapper.
- Type:
- __getattr__(name: str) Optional[object] ¶
Return an attribute from the site configuration.
If the attribute is not present in the site configuration’s settings, the registered default will be returned.
- __annotations__ = {'siteconfig': 'SiteConfiguration'}¶
- class SiteConfiguration(*args, **kwargs)¶
Bases:
Model
Stored version and settings data for a Django site.
This stores dynamic settings for a site, along with version information, allowing the application to alter and apply/synchronize settings across threads, processes, and servers without restarting the server.
Consumers should not create or fetch their own instance of this class through standard Django query functions. Instead, they should use
SiteConfiguration.objects.get_current()
instead. See the documentation for that method for details on how to safely look up and use site configuration.- version¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- settings¶
A JSON dictionary field of settings stored for a site.
- classmethod add_global_defaults(defaults_dict: Dict[str, Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]]) None ¶
Add a dictionary of global defaults for settings.
These defaults will be used when calling
get()
for any setting not stored. Defaults registered for a specific site configuration take precedent over global defaults.- Parameters:
default_dict (
dict
) – A dictionary of defaults, mapping siteconfig settings keys to JSON-serializable values.
- classmethod add_global_default(key: str, default_value: Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]) None ¶
Add a global default value for a settings key.
The default will be used when calling
get()
for this key, if a value is not stored. Defaults registered for a specific site configuration take precedent over global defaults.
- classmethod remove_global_default(key: str) None ¶
Remove a global default value for a settings key.
- Parameters:
key (
str
) – The settings key to remove the default for.
- classmethod clear_global_defaults() None ¶
Clear all default values for this site configuration.
This will clear only global defaults. This will not affect defaults registered on specific site configurations.
- classmethod get_global_defaults() Dict[str, Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]] ¶
Return all global defaults for settings.
- Returns:
A dictionary of all registered global defaults for settings.
- Return type:
- site¶
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parent
is aForwardManyToOneDescriptor
instance.
- get(key: str, default: Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str] = None) Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str] ¶
Return the value for a setting.
If the setting is not found, the default value will be returned. This is represented by the default parameter, if passed in, or a global default (from
add_default()
) if set.If no default is available,
None
will be returned.
- set(key: str, value: Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]) None ¶
Set a value for a setting.
The setting will be stored locally until the model is saved, at which point it will be synchronized with other processes/servers.
- add_defaults(defaults_dict: Dict[str, Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]]) None ¶
Add a dictionary of defaults for settings.
These defaults will be used when calling
get()
for any setting not stored. These will only be registered for this site configuration, and will not be registered for global defaults.- Parameters:
default_dict (
dict
) – A dictionary of defaults, mapping siteconfig settings keys to JSON-serializable values.
- add_default(key: str, default_value: Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]) None ¶
Add a default value for a settings key.
The default will be used when calling
get()
for this key, if a value is not stored. This will only be registered for this site configuration, and will not be registered for global defaults.
- remove_default(key: str) None ¶
Remove a default value on this site configuration.
This will remove only defaults registered on this site configuration. This does not affect global defaults.
- Parameters:
key (
str
) – The settings key to remove the default for.
- clear_defaults() None ¶
Clear all default values for this site configuration.
This will clear only defaults registered on this site configuration. This does not affect global defaults.
- get_defaults() Dict[str, Union[djblets.util.typing.JSONDict, djblets.util.typing.JSONDictImmutable, djblets.util.typing.JSONList, djblets.util.typing.JSONListImmutable, None, bool, float, int, str]] ¶
Return all defaults for this site configuration.
This will return only defaults registered on this site configuration. The result does not include global defaults.
- Returns:
A dictionary of all registered defaults for settings.
- Return type:
- is_expired() bool ¶
Return whether or not this SiteConfiguration is expired.
If the configuration is expired, it will need to be reloaded before accessing any settings.
- Returns:
Whether or not the current state is expired.
- Return type:
- save(*args, clear_caches: bool = True, **kwargs) None ¶
Save the site configuration to the database.
By default, saving will clear the caches across all processes/servers using this site configuration, causing them to be re-fetched on the next request.
- __str__() str ¶
Return a string version of the site configuration.
The returned string will list the associated site’s domain and the stored application version.
- Returns:
The string representation of the site configuration.
- Return type:
- __annotations__ = {}¶
- get_settings_json()¶
- id¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- set_settings_json(json)¶
- site_id¶