Browse Source

preferences: refactor to check consistently input values

Noémi Ványi 8 years ago
parent
commit
12c369e858
1 changed files with 14 additions and 12 deletions
  1. 14
    12
      searx/preferences.py

+ 14
- 12
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):
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('all', choices=LANGUAGE_CODES),
218
                                    'locale': EnumStringSetting(settings['ui']['default_locale'],
220
                                    'locale': EnumStringSetting(settings['ui']['default_locale'],
219
-                                                               choices=settings['locales'].keys()),
221
+                                                               choices=settings['locales'].keys() + ['']),
220
                                    'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
222
                                    'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
221
                                                                      choices=autocomplete.backends.keys() + ['']),
223
                                                                      choices=autocomplete.backends.keys() + ['']),
222
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],
224
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],