From 727c287856629cf7c11b87cecbf1b32bb0ca1831 Mon Sep 17 00:00:00 2001 From: marc Date: Mon, 31 Oct 2016 00:54:29 -0600 Subject: [fix] backwards compatibility with old language cookies --- searx/preferences.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'searx/preferences.py') diff --git a/searx/preferences.py b/searx/preferences.py index 4436b8fe8..b27cfb7f4 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -59,7 +59,18 @@ class EnumStringSetting(Setting): self._validate_selection(self.value) def parse(self, data): - self._validate_selection(data) + if data not in self.choices and data != self.value: + # hack to give some backwards compatibility with old language cookies + data = str(data).replace('_', '-') + lang = data[:2] + if data in self.choices: + pass + elif lang in self.choices: + data = lang + elif data == 'ar-XA': + data = 'ar-SA' + else: + data = 'all' self.value = data -- cgit v1.2.3 From 93233c786a1970b11a2f42e5afb3e3a2d8814505 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 13 Dec 2016 20:55:56 -0600 Subject: Refactor search language preference. --- searx/preferences.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'searx/preferences.py') diff --git a/searx/preferences.py b/searx/preferences.py index b27cfb7f4..7dc0e3172 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -59,18 +59,7 @@ class EnumStringSetting(Setting): self._validate_selection(self.value) def parse(self, data): - if data not in self.choices and data != self.value: - # hack to give some backwards compatibility with old language cookies - data = str(data).replace('_', '-') - lang = data[:2] - if data in self.choices: - pass - elif lang in self.choices: - data = lang - elif data == 'ar-XA': - data = 'ar-SA' - else: - data = 'all' + self._validate_selection(data) self.value = data @@ -106,6 +95,25 @@ class MultipleChoiceSetting(EnumStringSetting): resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE) +class SearchLanguageSetting(EnumStringSetting): + """Available choices may change, so user's value may not be in choices anymore""" + + def parse(self, data): + if data not in self.choices and data != self.value: + # hack to give some backwards compatibility with old language cookies + data = str(data).replace('_', '-') + lang = data.split('-')[0] + if data in self.choices: + pass + elif lang in self.choices: + data = lang + elif data == 'ar-XA': + data = 'ar-SA' + else: + data = 'all' + self.value = data + + class MapSetting(Setting): """Setting of a value that has to be translated in order to be storable""" @@ -227,8 +235,8 @@ class Preferences(object): super(Preferences, self).__init__() self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories), - 'language': EnumStringSetting(settings['search']['language'], - choices=LANGUAGE_CODES), + 'language': SearchLanguageSetting(settings['search']['language'], + choices=LANGUAGE_CODES), 'locale': EnumStringSetting(settings['ui']['default_locale'], choices=settings['locales'].keys() + ['']), 'autocomplete': EnumStringSetting(settings['search']['autocomplete'], -- cgit v1.2.3 From e0c270bd72f7b2a40222e3ed264e25d36cb0fc30 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 13 Dec 2016 23:51:15 -0600 Subject: tests for language support in engines --- searx/preferences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'searx/preferences.py') diff --git a/searx/preferences.py b/searx/preferences.py index 7dc0e3172..3aeb87e9f 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -110,7 +110,7 @@ class SearchLanguageSetting(EnumStringSetting): elif data == 'ar-XA': data = 'ar-SA' else: - data = 'all' + data = self.value self.value = data -- cgit v1.2.3