From 12c369e858b45b4904ab079c3aefebaa18e7ece4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Mon, 14 Nov 2016 22:24:40 +0100 Subject: [PATCH 1/2] preferences: refactor to check consistently input values --- searx/preferences.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/searx/preferences.py b/searx/preferences.py index 8b787fc59..045f0e8c6 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -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'], From 299c8823045a269c83d19cb6d05b24ef334207af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Mon, 14 Nov 2016 22:32:40 +0100 Subject: [PATCH 2/2] search: make language configurable from settings.yml --- searx/preferences.py | 3 ++- searx/settings.yml | 1 + searx/settings_robot.yml | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/searx/preferences.py b/searx/preferences.py index 045f0e8c6..4436b8fe8 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -216,7 +216,8 @@ class Preferences(object): super(Preferences, self).__init__() self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories), - 'language': EnumStringSetting('all', choices=LANGUAGE_CODES), + 'language': EnumStringSetting(settings['search']['language'], + choices=LANGUAGE_CODES), 'locale': EnumStringSetting(settings['ui']['default_locale'], choices=settings['locales'].keys() + ['']), 'autocomplete': EnumStringSetting(settings['search']['autocomplete'], diff --git a/searx/settings.yml b/searx/settings.yml index 573cf5458..733341c31 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -5,6 +5,7 @@ general: search: safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default + language : "all" server: port : 8888 diff --git a/searx/settings_robot.yml b/searx/settings_robot.yml index 43dc9b00a..7d2701449 100644 --- a/searx/settings_robot.yml +++ b/searx/settings_robot.yml @@ -5,6 +5,7 @@ general: search: safe_search : 0 autocomplete : "" + language: "all" server: port : 11111