2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 11:40:58 +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:
Lukas
2023-06-01 15:53:06 +02:00
committed by GitHub
parent 037654610e
commit 4d9e92011e
5 changed files with 23 additions and 12 deletions

View File

@ -127,6 +127,7 @@ class SettingsKeyType(TypedDict, total=False):
before_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)
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
@ -139,6 +140,7 @@ class SettingsKeyType(TypedDict, total=False):
before_save: Callable[..., None]
after_save: Callable[..., None]
protected: bool
model: str
class BaseInvenTreeSetting(models.Model):
@ -170,12 +172,12 @@ class BaseInvenTreeSetting(models.Model):
# Execute before_save action
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:
self.save_to_cache()
super().save()
# Execute after_save action
self._call_settings_function('after_save', args, kwargs)
@ -205,6 +207,10 @@ class BaseInvenTreeSetting(models.Model):
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")
try:
@ -254,7 +260,7 @@ class BaseInvenTreeSetting(models.Model):
results = cls.objects.all()
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='_')
# 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 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:
backup_value = cls.get_setting_default(key, **kwargs)