Add on_change_callback to config variables. These callback can be defined in the ConfigVariable object and is called, when the variable is changed.
This commit is contained in:
parent
a28ca8a4a3
commit
404d0fc272
@ -31,9 +31,12 @@ class ConfigHandler(object):
|
|||||||
return self[key]
|
return self[key]
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
|
# Save the new value to the database
|
||||||
updated_rows = ConfigStore.objects.filter(key=key).update(value=value)
|
updated_rows = ConfigStore.objects.filter(key=key).update(value=value)
|
||||||
if not updated_rows:
|
if not updated_rows:
|
||||||
ConfigStore.objects.create(key=key, value=value)
|
ConfigStore.objects.create(key=key, value=value)
|
||||||
|
|
||||||
|
# Update cache
|
||||||
try:
|
try:
|
||||||
self._cache[key] = value
|
self._cache[key] = value
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -41,6 +44,13 @@ class ConfigHandler(object):
|
|||||||
# called. In this case nothing should happen.
|
# called. In this case nothing should happen.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Call on_change callback
|
||||||
|
for receiver, config_page in config_signal.send(sender=self):
|
||||||
|
for config_variable in config_page.variables:
|
||||||
|
if config_variable.name == key and config_variable.on_change:
|
||||||
|
config_variable.on_change()
|
||||||
|
break
|
||||||
|
|
||||||
def setup_cache(self):
|
def setup_cache(self):
|
||||||
"""
|
"""
|
||||||
Loads all config variables from the database and by sending a
|
Loads all config variables from the database and by sending a
|
||||||
@ -144,9 +154,11 @@ class ConfigVariable(object):
|
|||||||
A simple object class to wrap new config variables. The keyword
|
A simple object class to wrap new config variables. The keyword
|
||||||
arguments 'name' and 'default_value' are required. The keyword
|
arguments 'name' and 'default_value' are required. The keyword
|
||||||
argument 'form_field' has to be set, if the variable should appear
|
argument 'form_field' has to be set, if the variable should appear
|
||||||
on the ConfigView.
|
on the ConfigView. The argument 'on_change' can get a callback
|
||||||
|
which is called every time, the variable is changed.
|
||||||
"""
|
"""
|
||||||
def __init__(self, name, default_value, form_field=None):
|
def __init__(self, name, default_value, form_field=None, on_change=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.default_value = default_value
|
self.default_value = default_value
|
||||||
self.form_field = form_field
|
self.form_field = form_field
|
||||||
|
self.on_change = on_change
|
||||||
|
@ -26,6 +26,9 @@ class HandleConfigTest(TestCase):
|
|||||||
def get_config_var(self, key):
|
def get_config_var(self, key):
|
||||||
return config[key]
|
return config[key]
|
||||||
|
|
||||||
|
def set_config_var(self, key, value):
|
||||||
|
config[key] = value
|
||||||
|
|
||||||
def test_get_config_default_value(self):
|
def test_get_config_default_value(self):
|
||||||
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
|
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
|
||||||
self.assertTrue(config['bool_var'])
|
self.assertTrue(config['bool_var'])
|
||||||
@ -73,6 +76,19 @@ class HandleConfigTest(TestCase):
|
|||||||
"""
|
"""
|
||||||
config['my_config_var'] = 'value'
|
config['my_config_var'] = 'value'
|
||||||
|
|
||||||
|
def test_on_change(self):
|
||||||
|
"""
|
||||||
|
Tests that the special callback is called and raises a special
|
||||||
|
message.
|
||||||
|
"""
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
Exception,
|
||||||
|
'Change callback dhcnfg34dlg06kdg successfully called.',
|
||||||
|
self.set_config_var,
|
||||||
|
key='var_with_callback_ghvnfjd5768gdfkwg0hm2',
|
||||||
|
value='new_string_kbmbnfhdgibkdjshg452bc')
|
||||||
|
self.assertEqual(config['var_with_callback_ghvnfjd5768gdfkwg0hm2'], 'new_string_kbmbnfhdgibkdjshg452bc')
|
||||||
|
|
||||||
|
|
||||||
class ConfigFormTest(TestCase):
|
class ConfigFormTest(TestCase):
|
||||||
|
|
||||||
@ -286,3 +302,16 @@ def set_simple_config_page_disabled_page(sender, **kwargs):
|
|||||||
url='testsimplepage3',
|
url='testsimplepage3',
|
||||||
required_permission='No permission required',
|
required_permission='No permission required',
|
||||||
variables=(ConfigVariable(name='hidden_config_var_2', default_value=''),))
|
variables=(ConfigVariable(name='hidden_config_var_2', default_value=''),))
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(config_signal, dispatch_uid='set_simple_config_page_with_callback_for_testing')
|
||||||
|
def set_simple_config_page_with_callback(sender, **kwargs):
|
||||||
|
def callback():
|
||||||
|
raise Exception('Change callback dhcnfg34dlg06kdg successfully called.')
|
||||||
|
return ConfigPage(title='Hvndfhsbgkridfgdfg',
|
||||||
|
url='testsimplepage4',
|
||||||
|
required_permission='No permission required',
|
||||||
|
variables=(ConfigVariable(
|
||||||
|
name='var_with_callback_ghvnfjd5768gdfkwg0hm2',
|
||||||
|
default_value='',
|
||||||
|
on_change=callback),))
|
||||||
|
Loading…
Reference in New Issue
Block a user