mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-27 10:08:44 +00:00
* [bug] fix for get_global_setting access (#13377)
- Fix erroneous calls
- Update helper function
(cherry picked from commit 292defb03e)
* fix import
---------
Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
1 parent
890b69fd25
commit
a0587768e7
4 files changed
+96
-4
No files matched your search
@@ -306,5 +306,5 @@ def inventree_identifier(override_announce: bool = False):
|
|||||||
'INVENTREE_ANNOUNCE_ID', environment_key='INVENTREE_ANNOUNCE_ID'
|
'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
|
return None
|
||||||
@@ -41,6 +41,61 @@ class SettingsValidatorTests(TestCase):
|
|||||||
'test part',
|
'test part',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_get_global_setting_unexpected_kwargs(self):
|
||||||
|
"""Unexpected keyword arguments are dropped, rather than forwarded."""
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
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):
|
def test_update_instance_name_no_multi(self):
|
||||||
"""Test valid cases for update_instance_name."""
|
"""Test valid cases for update_instance_name."""
|
||||||
self.assertIsNone(common.setting.system.update_instance_name('abc'))
|
self.assertIsNone(common.setting.system.update_instance_name('abc'))
|
||||||
|
|||||||
@@ -39,19 +39,54 @@ def global_setting_overrides() -> dict:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def get_global_setting(key, backup_value=None, environment_key=None, **kwargs):
|
def get_global_setting(
|
||||||
"""Return the value of a global setting using the provided key."""
|
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
|
import InvenTree.ready
|
||||||
from common.models import InvenTreeSetting
|
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:
|
if environment_key:
|
||||||
value = environ.get(environment_key)
|
value = environ.get(environment_key)
|
||||||
if value:
|
if value:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
kwargs = {}
|
||||||
|
|
||||||
if backup_value is not None:
|
if backup_value is not None:
|
||||||
kwargs['backup_value'] = backup_value
|
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
|
# Prevent database writes if we are in a read-only command
|
||||||
if InvenTree.ready.isReadOnlyCommand():
|
if InvenTree.ready.isReadOnlyCommand():
|
||||||
kwargs['create'] = False
|
kwargs['create'] = False
|
||||||
|
|||||||
@@ -53,7 +53,9 @@ class BarcodeView(CreateAPIView):
|
|||||||
barcode = context.pop('barcode', '')
|
barcode = context.pop('barcode', '')
|
||||||
|
|
||||||
# Exit if storing barcode scans is disabled
|
# 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
|
return
|
||||||
|
|
||||||
# Ensure that the response data is stringified first, otherwise cannot be JSON encoded
|
# Ensure that the response data is stringified first, otherwise cannot be JSON encoded
|
||||||
|
|||||||
Reference in new issue
Block a user