mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-26 17:48:54 +00:00
[bug] fix for get_global_setting access (#13377)
- Fix erroneous calls - Update helper function
This commit is contained in:
1 parent
e4d1ba305a
commit
292defb03e
4 files changed
+94
-4
No files matched your search
@@ -301,5 +301,5 @@ def inventree_identifier(override_announce: bool = False):
|
||||
if override_announce or get_global_setting(
|
||||
'INVENTREE_ANNOUNCE_ID', environment_key='INVENTREE_ANNOUNCE_ID'
|
||||
):
|
||||
return get_global_setting('INVENTREE_INSTANCE_ID', default='')
|
||||
return get_global_setting('INVENTREE_INSTANCE_ID', backup_value='')
|
||||
return None
|
||||
@@ -28,6 +28,59 @@ class SettingsTests(TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
collect_system_settings()
|
||||
|
||||
def test_get_global_setting_unexpected_kwargs(self):
|
||||
"""Unexpected keyword arguments are dropped, rather than forwarded."""
|
||||
from common.models import InvenTreeSetting
|
||||
from common.settings import get_global_setting
|
||||
|
||||
with (
|
||||
mock.patch.object(
|
||||
InvenTreeSetting, 'get_setting', return_value=True
|
||||
) as get_setting,
|
||||
mock.patch('common.settings.logger') as logger,
|
||||
):
|
||||
get_global_setting(
|
||||
'BARCODE_STORE_RESULTS', backup=False, default='', create=False
|
||||
)
|
||||
|
||||
get_setting.assert_called_once_with('BARCODE_STORE_RESULTS', create=False)
|
||||
logger.warning.assert_called_once()
|
||||
|
||||
@override_settings(
|
||||
CACHES={
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||
'LOCATION': 'settings-cache-test',
|
||||
}
|
||||
}
|
||||
)
|
||||
def test_get_global_setting_cache_hit(self):
|
||||
"""Unexpected keyword arguments must not cause global cache misses.
|
||||
|
||||
Previously, any extra kwarg became part of the cache key. As the global
|
||||
cache is always populated under the canonical key, such lookups never hit
|
||||
the cache, and required a database query on every call.
|
||||
"""
|
||||
from django.core.cache import cache
|
||||
|
||||
from common.settings import get_global_setting, set_global_setting
|
||||
|
||||
cache.clear()
|
||||
|
||||
set_global_setting('BARCODE_STORE_RESULTS', True)
|
||||
|
||||
# Prime the global cache
|
||||
self.assertTrue(get_global_setting('BARCODE_STORE_RESULTS', cache=True))
|
||||
|
||||
with self.assertNumQueries(0):
|
||||
self.assertTrue(
|
||||
get_global_setting(
|
||||
'BARCODE_STORE_RESULTS', backup=False, create=False, cache=True
|
||||
)
|
||||
)
|
||||
|
||||
cache.clear()
|
||||
|
||||
def test_update_instance_name_no_multi(self):
|
||||
"""Test valid cases for update_instance_name."""
|
||||
self.assertIsNone(common.setting.system.update_instance_name('abc'))
|
||||
|
||||
@@ -39,19 +39,54 @@ def global_setting_overrides() -> dict:
|
||||
return {}
|
||||
|
||||
|
||||
def get_global_setting(key, backup_value=None, environment_key=None, **kwargs):
|
||||
"""Return the value of a global setting using the provided key."""
|
||||
def get_global_setting(
|
||||
key,
|
||||
backup_value=None,
|
||||
environment_key=None,
|
||||
*,
|
||||
create: Optional[bool] = None,
|
||||
cache: Optional[bool] = None,
|
||||
**unexpected,
|
||||
):
|
||||
"""Return the value of a global setting using the provided key.
|
||||
|
||||
Arguments:
|
||||
key: The setting key
|
||||
backup_value: Value to return if the setting is not found (defaults to the setting default)
|
||||
environment_key: Environment variable which, if set, overrides the setting value
|
||||
create: Create the setting in the database if it does not exist (default = True)
|
||||
cache: Use the global cache for the lookup (default = GLOBAL_CACHE_ENABLED)
|
||||
|
||||
Any other keyword arguments are ignored (with a warning). They are not
|
||||
forwarded, as they would otherwise become part of the setting cache key,
|
||||
resulting in a stale cache entry which is never refreshed when the setting is saved.
|
||||
"""
|
||||
import InvenTree.ready
|
||||
from common.models import InvenTreeSetting
|
||||
|
||||
if unexpected:
|
||||
logger.warning(
|
||||
'get_global_setting: ignoring unexpected arguments %s for setting %s',
|
||||
sorted(unexpected.keys()),
|
||||
key,
|
||||
)
|
||||
|
||||
if environment_key:
|
||||
value = environ.get(environment_key)
|
||||
if value:
|
||||
return value
|
||||
|
||||
kwargs = {}
|
||||
|
||||
if backup_value is not None:
|
||||
kwargs['backup_value'] = backup_value
|
||||
|
||||
if create is not None:
|
||||
kwargs['create'] = create
|
||||
|
||||
if cache is not None:
|
||||
kwargs['cache'] = cache
|
||||
|
||||
# Prevent database writes if we are in a read-only command
|
||||
if InvenTree.ready.isReadOnlyCommand():
|
||||
kwargs['create'] = False
|
||||
|
||||
@@ -75,7 +75,9 @@ class BarcodeView(CreateAPIView):
|
||||
barcode = context.pop('barcode', '')
|
||||
|
||||
# Exit if storing barcode scans is disabled
|
||||
if not get_global_setting('BARCODE_STORE_RESULTS', backup=False, create=False):
|
||||
if not get_global_setting(
|
||||
'BARCODE_STORE_RESULTS', backup_value=False, create=False
|
||||
):
|
||||
return
|
||||
|
||||
# Ensure that the response data is stringified first, otherwise cannot be JSON encoded
|
||||
|
||||
Reference in new issue
Block a user