mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Fix/settings bugs and added model SettingKeyType typing (#4944)
* fix .gitignore and spelling issues * Fix setting get not cached correctly * Add model to settings key type * Fix plugin setting slug url * Fix typo * Fix resetting of related setting field * Improved model comment
This commit is contained in:
parent
037654610e
commit
4d9e92011e
2
.gitignore
vendored
2
.gitignore
vendored
@ -97,7 +97,7 @@ node_modules/
|
|||||||
maintenance_mode_state.txt
|
maintenance_mode_state.txt
|
||||||
|
|
||||||
# plugin dev directory
|
# plugin dev directory
|
||||||
./plugins/
|
InvenTree/plugins/
|
||||||
|
|
||||||
# Compiled translation files
|
# Compiled translation files
|
||||||
*.mo
|
*.mo
|
||||||
|
@ -127,6 +127,7 @@ class SettingsKeyType(TypedDict, total=False):
|
|||||||
before_save: Function that gets called after save with *args, **kwargs (optional)
|
before_save: Function that gets called after save with *args, **kwargs (optional)
|
||||||
after_save: Function that gets called after save with *args, **kwargs (optional)
|
after_save: Function that gets called after save with *args, **kwargs (optional)
|
||||||
protected: Protected values are not returned to the client, instead "***" is returned (optional, default: False)
|
protected: Protected values are not returned to the client, instead "***" is returned (optional, default: False)
|
||||||
|
model: Auto create a dropdown menu to select an associated model instance (e.g. 'company.company', 'auth.user' and 'auth.group' are possible too, optional)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
@ -139,6 +140,7 @@ class SettingsKeyType(TypedDict, total=False):
|
|||||||
before_save: Callable[..., None]
|
before_save: Callable[..., None]
|
||||||
after_save: Callable[..., None]
|
after_save: Callable[..., None]
|
||||||
protected: bool
|
protected: bool
|
||||||
|
model: str
|
||||||
|
|
||||||
|
|
||||||
class BaseInvenTreeSetting(models.Model):
|
class BaseInvenTreeSetting(models.Model):
|
||||||
@ -170,12 +172,12 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
# Execute before_save action
|
# Execute before_save action
|
||||||
self._call_settings_function('before_save', args, kwargs)
|
self._call_settings_function('before_save', args, kwargs)
|
||||||
|
|
||||||
# Update this setting in the cache
|
super().save()
|
||||||
|
|
||||||
|
# Update this setting in the cache after it was saved so a pk exists
|
||||||
if do_cache:
|
if do_cache:
|
||||||
self.save_to_cache()
|
self.save_to_cache()
|
||||||
|
|
||||||
super().save()
|
|
||||||
|
|
||||||
# Execute after_save action
|
# Execute after_save action
|
||||||
self._call_settings_function('after_save', args, kwargs)
|
self._call_settings_function('after_save', args, kwargs)
|
||||||
|
|
||||||
@ -205,6 +207,10 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
|
|
||||||
ckey = self.cache_key
|
ckey = self.cache_key
|
||||||
|
|
||||||
|
# skip saving to cache if no pk is set
|
||||||
|
if self.pk is None:
|
||||||
|
return
|
||||||
|
|
||||||
logger.debug(f"Saving setting '{ckey}' to cache")
|
logger.debug(f"Saving setting '{ckey}' to cache")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -254,7 +260,7 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
results = cls.objects.all()
|
results = cls.objects.all()
|
||||||
|
|
||||||
if exclude_hidden:
|
if exclude_hidden:
|
||||||
# Keys which start with an undersore are used for internal functionality
|
# Keys which start with an underscore are used for internal functionality
|
||||||
results = results.exclude(key__startswith='_')
|
results = results.exclude(key__startswith='_')
|
||||||
|
|
||||||
# Optionally filter by other keys
|
# Optionally filter by other keys
|
||||||
@ -469,7 +475,7 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
|
|
||||||
If it does not exist, return the backup value (default = None)
|
If it does not exist, return the backup value (default = None)
|
||||||
"""
|
"""
|
||||||
# If no backup value is specified, atttempt to retrieve a "default" value
|
# If no backup value is specified, attempt to retrieve a "default" value
|
||||||
if backup_value is None:
|
if backup_value is None:
|
||||||
backup_value = cls.get_setting_default(key, **kwargs)
|
backup_value = cls.get_setting_default(key, **kwargs)
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ def default_currency(*args, **kwargs):
|
|||||||
|
|
||||||
@register.simple_tag()
|
@register.simple_tag()
|
||||||
def setting_object(key, *args, **kwargs):
|
def setting_object(key, *args, **kwargs):
|
||||||
"""Return a setting object speciifed by the given key.
|
"""Return a setting object specified by the given key.
|
||||||
|
|
||||||
(Or return None if the setting does not exist)
|
(Or return None if the setting does not exist)
|
||||||
if a user-setting was requested return that
|
if a user-setting was requested return that
|
||||||
|
@ -164,7 +164,7 @@ class PluginSettingList(ListAPI):
|
|||||||
|
|
||||||
|
|
||||||
def check_plugin(plugin_slug: str, plugin_pk: int) -> InvenTreePlugin:
|
def check_plugin(plugin_slug: str, plugin_pk: int) -> InvenTreePlugin:
|
||||||
"""Check that a plugin for the provided slug exsists and get the config.
|
"""Check that a plugin for the provided slug exists and get the config.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
plugin_slug (str): Slug for plugin.
|
plugin_slug (str): Slug for plugin.
|
||||||
@ -247,7 +247,7 @@ plugin_api_urls = [
|
|||||||
re_path(r'^plugins/', include([
|
re_path(r'^plugins/', include([
|
||||||
# Plugin settings URLs
|
# Plugin settings URLs
|
||||||
re_path(r'^settings/', include([
|
re_path(r'^settings/', include([
|
||||||
re_path(r'^(?P<plugin>\w+)/(?P<key>\w+)/', PluginSettingDetail.as_view(), name='api-plugin-setting-detail'), # Used for admin interface
|
re_path(r'^(?P<plugin>[-\w]+)/(?P<key>\w+)/', PluginSettingDetail.as_view(), name='api-plugin-setting-detail'), # Used for admin interface
|
||||||
re_path(r'^.*$', PluginSettingList.as_view(), name='api-plugin-setting-list'),
|
re_path(r'^.*$', PluginSettingList.as_view(), name='api-plugin-setting-list'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ plugin_api_urls = [
|
|||||||
re_path(r'^.*$', PluginDetail.as_view(), name='api-plugin-detail'),
|
re_path(r'^.*$', PluginDetail.as_view(), name='api-plugin-detail'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
# Plugin managment
|
# Plugin management
|
||||||
re_path(r'^install/', PluginInstall.as_view(), name='api-plugin-install'),
|
re_path(r'^install/', PluginInstall.as_view(), name='api-plugin-install'),
|
||||||
re_path(r'^activate/', PluginActivate.as_view(), name='api-plugin-activate'),
|
re_path(r'^activate/', PluginActivate.as_view(), name='api-plugin-activate'),
|
||||||
|
|
||||||
|
@ -111,8 +111,13 @@ function editSetting(key, options={}) {
|
|||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
processBeforeUpload: function(data) {
|
processBeforeUpload: function(data) {
|
||||||
|
if(response.type === 'related field' && data.value === null) {
|
||||||
|
// related fields throw an error because they are set to null on delete
|
||||||
|
data.value = "";
|
||||||
|
} else {
|
||||||
// Convert value to string
|
// Convert value to string
|
||||||
data.value = data.value.toString();
|
data.value = data.value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user