Browse Source

Refactor search language preference.

marc 8 years ago
parent
commit
93233c786a
1 changed files with 22 additions and 14 deletions
  1. 22
    14
      searx/preferences.py

+ 22
- 14
searx/preferences.py View File

@@ -59,18 +59,7 @@ class EnumStringSetting(Setting):
59 59
         self._validate_selection(self.value)
60 60
 
61 61
     def parse(self, data):
62
-        if data not in self.choices and data != self.value:
63
-            # hack to give some backwards compatibility with old language cookies
64
-            data = str(data).replace('_', '-')
65
-            lang = data[:2]
66
-            if data in self.choices:
67
-                pass
68
-            elif lang in self.choices:
69
-                data = lang
70
-            elif data == 'ar-XA':
71
-                data = 'ar-SA'
72
-            else:
73
-                data = 'all'
62
+        self._validate_selection(data)
74 63
         self.value = data
75 64
 
76 65
 
@@ -106,6 +95,25 @@ class MultipleChoiceSetting(EnumStringSetting):
106 95
         resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE)
107 96
 
108 97
 
98
+class SearchLanguageSetting(EnumStringSetting):
99
+    """Available choices may change, so user's value may not be in choices anymore"""
100
+
101
+    def parse(self, data):
102
+        if data not in self.choices and data != self.value:
103
+            # hack to give some backwards compatibility with old language cookies
104
+            data = str(data).replace('_', '-')
105
+            lang = data.split('-')[0]
106
+            if data in self.choices:
107
+                pass
108
+            elif lang in self.choices:
109
+                data = lang
110
+            elif data == 'ar-XA':
111
+                data = 'ar-SA'
112
+            else:
113
+                data = 'all'
114
+        self.value = data
115
+
116
+
109 117
 class MapSetting(Setting):
110 118
     """Setting of a value that has to be translated in order to be storable"""
111 119
 
@@ -227,8 +235,8 @@ class Preferences(object):
227 235
         super(Preferences, self).__init__()
228 236
 
229 237
         self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
230
-                                   'language': EnumStringSetting(settings['search']['language'],
231
-                                                                 choices=LANGUAGE_CODES),
238
+                                   'language': SearchLanguageSetting(settings['search']['language'],
239
+                                                                     choices=LANGUAGE_CODES),
232 240
                                    'locale': EnumStringSetting(settings['ui']['default_locale'],
233 241
                                                                choices=settings['locales'].keys() + ['']),
234 242
                                    'autocomplete': EnumStringSetting(settings['search']['autocomplete'],