Added possibility to hide config variables.

This commit is contained in:
Norman Jäckel 2016-02-14 21:38:26 +01:00
parent 46c125d0c5
commit cc55aff8cc
4 changed files with 39 additions and 7 deletions

View File

@ -137,10 +137,11 @@ class ConfigVariable:
The keyword arguments 'name' and 'default_value' are required.
The keyword arguments 'input_type', 'label' and 'help_text' are for
rendering a HTML form element. If you set 'input_type' to 'choice' you
have to provide 'choices', which is a list of dictionaries containing a
value and a display_name of every possible choice.
The keyword arguments 'input_type', 'label', 'help_text' and 'hidden'
are for rendering a HTML form element. The 'input_type is also used for
validation. If you set 'input_type' to 'choice' you have to provide
'choices', which is a list of dictionaries containing a value and a
display_name of every possible choice.
The keyword arguments 'weight', 'group' and 'subgroup' are for sorting
and grouping.
@ -157,8 +158,9 @@ class ConfigVariable:
command line option.
"""
def __init__(self, name, default_value, input_type='string', label=None,
help_text=None, choices=None, weight=0, group=None,
subgroup=None, validators=None, on_change=None, translatable=False):
help_text=None, choices=None, hidden=False, weight=0,
group=None, subgroup=None, validators=None, on_change=None,
translatable=False):
if input_type not in INPUT_TYPE_MAPPING:
raise ValueError(_('Invalid value for config attribute input_type.'))
if input_type == 'choice' and choices is None:
@ -173,6 +175,7 @@ class ConfigVariable:
self.label = label or name
self.help_text = help_text or ''
self.choices = choices
self.hidden = hidden
self.weight = weight
self.group = group or _('General')
self.subgroup = subgroup
@ -191,8 +194,15 @@ class ConfigVariable:
'value': config[self.name],
'input_type': self.input_type,
'label': self.label,
'help_text': self.help_text
'help_text': self.help_text,
}
if self.input_type == 'choice':
data['choices'] = self.choices
return data
def is_hidden(self):
"""
Returns True if the config variable is hidden so it can be removed
from response of OPTIONS request.
"""
return self.hidden

View File

@ -412,14 +412,20 @@ class ConfigMetadata(SimpleMetadata):
# Build tree.
config_groups = []
for config_variable in config_variables:
if config_variable.is_hidden():
# Skip hidden config variables. Do not even check groups and subgroups.
continue
if not config_groups or config_groups[-1]['name'] != config_variable.group:
# Add new group.
config_groups.append(OrderedDict(
name=config_variable.group,
subgroups=[]))
if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup:
# Add new subgroup.
config_groups[-1]['subgroups'].append(OrderedDict(
name=config_variable.subgroup,
items=[]))
# Add the config variable to the current group and subgroup.
config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data)
# Add tree to metadata.

View File

@ -74,6 +74,7 @@ def setup_motion_config(sender, **kwargs):
default_value=False,
input_type='boolean',
label=ugettext_lazy('Activate amendments'),
hidden=True,
weight=335,
group=ugettext_lazy('Motion'),
subgroup=ugettext_lazy('Amendments'))
@ -82,6 +83,7 @@ def setup_motion_config(sender, **kwargs):
name='motions_amendments_prefix',
default_value=pgettext('Prefix for the identifier for amendments', 'A'),
label=ugettext_lazy('Prefix for the identifier for amendments'),
hidden=True,
weight=340,
group=ugettext_lazy('Motion'),
subgroup=ugettext_lazy('Amendments'))

View File

@ -162,6 +162,14 @@ class ConfigViewSet(TestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {'detail': 'Invalid input. Config value is missing.'})
def test_metadata_with_hidden(self):
self.client.login(username='admin', password='admin')
response = self.client.options(reverse('config-list'))
filter_obj = filter(
lambda item: item['key'] == 'test_var_pud2zah2teeNaiP7IoNa',
response.data['config_groups'][0]['subgroups'][0]['items'])
self.assertEqual(len(list(filter_obj)), 0)
def validator_for_testing(value):
"""
@ -203,3 +211,9 @@ def set_simple_config_view_integration_config_test(sender, **kwargs):
name='test_var_Hi7Oje8Oith7goopeeng',
default_value='',
validators=(validator_for_testing,))
yield ConfigVariable(
name='test_var_pud2zah2teeNaiP7IoNa',
default_value=None,
label='test_label_xaing7eefaePheePhei6',
hidden=True)