1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-04 20:30:11 +01:00

preferences: refactor to check consistently input values

This commit is contained in:
Noémi Ványi 2016-11-14 22:24:40 +01:00
parent 044809e298
commit 12c369e858

View File

@ -49,28 +49,32 @@ class StringSetting(Setting):
class EnumStringSetting(Setting):
"""Setting of a value which can only come from the given choices"""
def _validate_selection(self, selection):
if selection not in self.choices:
raise ValidationException('Invalid value: "{0}"'.format(selection))
def _post_init(self):
if not hasattr(self, 'choices'):
raise MissingArgumentException('Missing argument: choices')
if self.value != '' and self.value not in self.choices:
raise ValidationException('Invalid default value: {0}'.format(self.value))
self._validate_selection(self.value)
def parse(self, data):
if data not in self.choices and data != self.value:
raise ValidationException('Invalid choice: {0}'.format(data))
self._validate_selection(data)
self.value = data
class MultipleChoiceSetting(EnumStringSetting):
"""Setting of values which can only come from the given choices"""
def _validate_selections(self, selections):
for item in selections:
if item not in self.choices:
raise ValidationException('Invalid value: "{0}"'.format(selections))
def _post_init(self):
if not hasattr(self, 'choices'):
raise MissingArgumentException('Missing argument: choices')
for item in self.value:
if item not in self.choices:
raise ValidationException('Invalid default value: {0}'.format(self.value))
self._validate_selections(self.value)
def parse(self, data):
if data == '':
@ -78,9 +82,7 @@ class MultipleChoiceSetting(EnumStringSetting):
return
elements = data.split(',')
for item in elements:
if item not in self.choices:
raise ValidationException('Invalid choice: {0}'.format(item))
self._validate_selections(elements)
self.value = elements
def parse_form(self, data):
@ -216,7 +218,7 @@ class Preferences(object):
self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
'language': EnumStringSetting('all', choices=LANGUAGE_CODES),
'locale': EnumStringSetting(settings['ui']['default_locale'],
choices=settings['locales'].keys()),
choices=settings['locales'].keys() + ['']),
'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
choices=autocomplete.backends.keys() + ['']),
'image_proxy': MapSetting(settings['server']['image_proxy'],