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 'name' and 'default_value' are required.
The keyword arguments 'input_type', 'label' and 'help_text' are for The keyword arguments 'input_type', 'label', 'help_text' and 'hidden'
rendering a HTML form element. If you set 'input_type' to 'choice' you are for rendering a HTML form element. The 'input_type is also used for
have to provide 'choices', which is a list of dictionaries containing a validation. If you set 'input_type' to 'choice' you have to provide
value and a display_name of every possible choice. '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 The keyword arguments 'weight', 'group' and 'subgroup' are for sorting
and grouping. and grouping.
@ -157,8 +158,9 @@ class ConfigVariable:
command line option. command line option.
""" """
def __init__(self, name, default_value, input_type='string', label=None, def __init__(self, name, default_value, input_type='string', label=None,
help_text=None, choices=None, weight=0, group=None, help_text=None, choices=None, hidden=False, weight=0,
subgroup=None, validators=None, on_change=None, translatable=False): group=None, subgroup=None, validators=None, on_change=None,
translatable=False):
if input_type not in INPUT_TYPE_MAPPING: if input_type not in INPUT_TYPE_MAPPING:
raise ValueError(_('Invalid value for config attribute input_type.')) raise ValueError(_('Invalid value for config attribute input_type.'))
if input_type == 'choice' and choices is None: if input_type == 'choice' and choices is None:
@ -173,6 +175,7 @@ class ConfigVariable:
self.label = label or name self.label = label or name
self.help_text = help_text or '' self.help_text = help_text or ''
self.choices = choices self.choices = choices
self.hidden = hidden
self.weight = weight self.weight = weight
self.group = group or _('General') self.group = group or _('General')
self.subgroup = subgroup self.subgroup = subgroup
@ -191,8 +194,15 @@ class ConfigVariable:
'value': config[self.name], 'value': config[self.name],
'input_type': self.input_type, 'input_type': self.input_type,
'label': self.label, 'label': self.label,
'help_text': self.help_text 'help_text': self.help_text,
} }
if self.input_type == 'choice': if self.input_type == 'choice':
data['choices'] = self.choices data['choices'] = self.choices
return data 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. # Build tree.
config_groups = [] config_groups = []
for config_variable in config_variables: 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: if not config_groups or config_groups[-1]['name'] != config_variable.group:
# Add new group.
config_groups.append(OrderedDict( config_groups.append(OrderedDict(
name=config_variable.group, name=config_variable.group,
subgroups=[])) subgroups=[]))
if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup: 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( config_groups[-1]['subgroups'].append(OrderedDict(
name=config_variable.subgroup, name=config_variable.subgroup,
items=[])) items=[]))
# Add the config variable to the current group and subgroup.
config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data) config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data)
# Add tree to metadata. # Add tree to metadata.

View File

@ -74,6 +74,7 @@ def setup_motion_config(sender, **kwargs):
default_value=False, default_value=False,
input_type='boolean', input_type='boolean',
label=ugettext_lazy('Activate amendments'), label=ugettext_lazy('Activate amendments'),
hidden=True,
weight=335, weight=335,
group=ugettext_lazy('Motion'), group=ugettext_lazy('Motion'),
subgroup=ugettext_lazy('Amendments')) subgroup=ugettext_lazy('Amendments'))
@ -82,6 +83,7 @@ def setup_motion_config(sender, **kwargs):
name='motions_amendments_prefix', name='motions_amendments_prefix',
default_value=pgettext('Prefix for the identifier for amendments', 'A'), default_value=pgettext('Prefix for the identifier for amendments', 'A'),
label=ugettext_lazy('Prefix for the identifier for amendments'), label=ugettext_lazy('Prefix for the identifier for amendments'),
hidden=True,
weight=340, weight=340,
group=ugettext_lazy('Motion'), group=ugettext_lazy('Motion'),
subgroup=ugettext_lazy('Amendments')) 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.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {'detail': 'Invalid input. Config value is missing.'}) 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): def validator_for_testing(value):
""" """
@ -203,3 +211,9 @@ def set_simple_config_view_integration_config_test(sender, **kwargs):
name='test_var_Hi7Oje8Oith7goopeeng', name='test_var_Hi7Oje8Oith7goopeeng',
default_value='', default_value='',
validators=(validator_for_testing,)) validators=(validator_for_testing,))
yield ConfigVariable(
name='test_var_pud2zah2teeNaiP7IoNa',
default_value=None,
label='test_label_xaing7eefaePheePhei6',
hidden=True)