on master help sign in
Merge pull request #764 from kvch/set-search-language-from-settings-yml

set search language from settings.yml

commit028e5b43d4aa43c498a60de9c6dac0bb3a0bb96d authorAdam Tauber <asciimoo@gmail.com> dateMon 14 Nov 2016, 23:42 (10 years ago) parent044809e299c882
3 files changed+18−13
searx/preferences.py+16−13 searx/settings.yml+1 searx/settings_robot.yml+1
searx/preferences.py+16−13
@@ -49,28 +49,32 @@ class StringSetting(Setting):
4949class EnumStringSetting(Setting):
5050 """Setting of a value which can only come from the given choices"""
5151
52+ def _validate_selection(self, selection):
53+ if selection not in self.choices:
54+ raise ValidationException('Invalid value: "{0}"'.format(selection))
55+
5256 def _post_init(self):
5357 if not hasattr(self, 'choices'):
5458 raise MissingArgumentException('Missing argument: choices')
55−
56− if self.value != '' and self.value not in self.choices:
57− raise ValidationException('Invalid default value: {0}'.format(self.value))
59+ self._validate_selection(self.value)
5860
5961 def parse(self, data):
60− if data not in self.choices and data != self.value:
61− raise ValidationException('Invalid choice: {0}'.format(data))
62+ self._validate_selection(data)
6263 self.value = data
6364
6465
6566class MultipleChoiceSetting(EnumStringSetting):
6667 """Setting of values which can only come from the given choices"""
6768
69+ def _validate_selections(self, selections):
70+ for item in selections:
71+ if item not in self.choices:
72+ raise ValidationException('Invalid value: "{0}"'.format(selections))
73+
6874 def _post_init(self):
6975 if not hasattr(self, 'choices'):
7076 raise MissingArgumentException('Missing argument: choices')
71− for item in self.value:
72− if item not in self.choices:
73− raise ValidationException('Invalid default value: {0}'.format(self.value))
77+ self._validate_selections(self.value)
7478
7579 def parse(self, data):
7680 if data == '':
@@ -78,9 +82,7 @@ class MultipleChoiceSetting(EnumStringSetting):
7882 return
7983
8084 elements = data.split(',')
81− for item in elements:
82− if item not in self.choices:
83− raise ValidationException('Invalid choice: {0}'.format(item))
85+ self._validate_selections(elements)
8486 self.value = elements
8587
8688 def parse_form(self, data):
@@ -214,9 +216,10 @@ class Preferences(object):
214216 super(Preferences, self).__init__()
215217
216218 self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
217− 'language': EnumStringSetting('all', choices=LANGUAGE_CODES),
219+ 'language': EnumStringSetting(settings['search']['language'],
220+ choices=LANGUAGE_CODES),
218221 'locale': EnumStringSetting(settings['ui']['default_locale'],
219− choices=settings['locales'].keys()),
222+ choices=settings['locales'].keys() + ['']),
220223 'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
221224 choices=autocomplete.backends.keys() + ['']),
222225 'image_proxy': MapSetting(settings['server']['image_proxy'],
searx/settings.yml+1
@@ -5,6 +5,7 @@ general:
55search:
66 safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
77 autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
8+ language : "all"
89
910server:
1011 port : 8888
searx/settings_robot.yml+1
@@ -5,6 +5,7 @@ general:
55search:
66 safe_search : 0
77 autocomplete : ""
8+ language: "all"
89
910server:
1011 port : 11111