Merge pull request #877 from normanjaeckel/ConfigChangeCallback

Add on_change_callback to config variables. These callback can be define...
This commit is contained in:
Oskar Hahn 2013-09-09 02:04:54 -07:00
commit 5961628799
2 changed files with 43 additions and 2 deletions

View File

@ -31,9 +31,12 @@ class ConfigHandler(object):
return self[key]
def __setitem__(self, key, value):
# Save the new value to the database
updated_rows = ConfigStore.objects.filter(key=key).update(value=value)
if not updated_rows:
ConfigStore.objects.create(key=key, value=value)
# Update cache
try:
self._cache[key] = value
except AttributeError:
@ -41,6 +44,13 @@ class ConfigHandler(object):
# called. In this case nothing should happen.
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):
"""
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
arguments 'name' and 'default_value' are required. The keyword
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.default_value = default_value
self.form_field = form_field
self.on_change = on_change

View File

@ -26,6 +26,9 @@ class HandleConfigTest(TestCase):
def get_config_var(self, key):
return config[key]
def set_config_var(self, key, value):
config[key] = value
def test_get_config_default_value(self):
self.assertEqual(config['string_var'], 'default_string_rien4ooCZieng6ah')
self.assertTrue(config['bool_var'])
@ -73,6 +76,19 @@ class HandleConfigTest(TestCase):
"""
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):
@ -286,3 +302,16 @@ def set_simple_config_page_disabled_page(sender, **kwargs):
url='testsimplepage3',
required_permission='No permission required',
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),))