Browse Source

Merge pull request #764 from kvch/set-search-language-from-settings-yml

set search language from settings.yml
Adam Tauber 8 years ago
parent
commit
028e5b43d4
3 changed files with 18 additions and 13 deletions
  1. 16
    13
      searx/preferences.py
  2. 1
    0
      searx/settings.yml
  3. 1
    0
      searx/settings_robot.yml

+ 16
- 13
searx/preferences.py View File

49
 class EnumStringSetting(Setting):
49
 class EnumStringSetting(Setting):
50
     """Setting of a value which can only come from the given choices"""
50
     """Setting of a value which can only come from the given choices"""
51
 
51
 
52
+    def _validate_selection(self, selection):
53
+        if selection not in self.choices:
54
+            raise ValidationException('Invalid value: "{0}"'.format(selection))
55
+
52
     def _post_init(self):
56
     def _post_init(self):
53
         if not hasattr(self, 'choices'):
57
         if not hasattr(self, 'choices'):
54
             raise MissingArgumentException('Missing argument: choices')
58
             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)
58
 
60
 
59
     def parse(self, data):
61
     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)
62
         self.value = data
63
         self.value = data
63
 
64
 
64
 
65
 
65
 class MultipleChoiceSetting(EnumStringSetting):
66
 class MultipleChoiceSetting(EnumStringSetting):
66
     """Setting of values which can only come from the given choices"""
67
     """Setting of values which can only come from the given choices"""
67
 
68
 
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
+
68
     def _post_init(self):
74
     def _post_init(self):
69
         if not hasattr(self, 'choices'):
75
         if not hasattr(self, 'choices'):
70
             raise MissingArgumentException('Missing argument: choices')
76
             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)
74
 
78
 
75
     def parse(self, data):
79
     def parse(self, data):
76
         if data == '':
80
         if data == '':
78
             return
82
             return
79
 
83
 
80
         elements = data.split(',')
84
         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)
84
         self.value = elements
86
         self.value = elements
85
 
87
 
86
     def parse_form(self, data):
88
     def parse_form(self, data):
214
         super(Preferences, self).__init__()
216
         super(Preferences, self).__init__()
215
 
217
 
216
         self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
218
         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),
218
                                    'locale': EnumStringSetting(settings['ui']['default_locale'],
221
                                    'locale': EnumStringSetting(settings['ui']['default_locale'],
219
-                                                               choices=settings['locales'].keys()),
222
+                                                               choices=settings['locales'].keys() + ['']),
220
                                    'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
223
                                    'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
221
                                                                      choices=autocomplete.backends.keys() + ['']),
224
                                                                      choices=autocomplete.backends.keys() + ['']),
222
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],
225
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],

+ 1
- 0
searx/settings.yml View File

5
 search:
5
 search:
6
     safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
6
     safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
7
     autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
7
     autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
8
+    language : "all"
8
 
9
 
9
 server:
10
 server:
10
     port : 8888
11
     port : 8888

+ 1
- 0
searx/settings_robot.yml View File

5
 search:
5
 search:
6
     safe_search : 0
6
     safe_search : 0
7
     autocomplete : ""
7
     autocomplete : ""
8
+    language: "all"
8
 
9
 
9
 server:
10
 server:
10
     port : 11111
11
     port : 11111