From 38d6ba4066a474c1b13e7ccb6f9ea92b43702a4a Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Mon, 10 Jul 2017 12:47:25 +0200 Subject: [PATCH] [enh] generate search url of saved preferences --- searx/preferences.py | 30 ++++++++++++++++++++++---- searx/templates/oscar/preferences.html | 3 +++ searx/webapp.py | 9 +++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/searx/preferences.py b/searx/preferences.py index b6a2ec4cc..92fe3853c 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -1,5 +1,6 @@ from searx import settings, autocomplete from searx.languages import language_codes as languages +from searx.url_utils import urlencode COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years @@ -232,7 +233,7 @@ class PluginsSetting(SwitchableSetting): class Preferences(object): - """Stores, validates and saves preferences to cookies""" + """Validates and saves preferences to cookies""" def __init__(self, themes, categories, engines, plugins): super(Preferences, self).__init__() @@ -247,19 +248,40 @@ class Preferences(object): 'image_proxy': MapSetting(settings['server']['image_proxy'], map={'': settings['server']['image_proxy'], '0': False, - '1': True}), + '1': True, + 'True': True, + 'False': False}), 'method': EnumStringSetting('POST', choices=('GET', 'POST')), 'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0, '1': 1, '2': 2}), 'theme': EnumStringSetting(settings['ui']['default_theme'], choices=themes), - 'results_on_new_tab': MapSetting(False, map={'0': False, '1': True})} + 'results_on_new_tab': MapSetting(False, map={'0': False, + '1': True, + 'False': False, + 'True': True})} self.engines = EnginesSetting('engines', choices=engines) self.plugins = PluginsSetting('plugins', choices=plugins) self.unknown_params = {} - def parse_cookies(self, input_data): + def get_as_url_params(self): + settings_kv = {} + for k, v in self.key_value_settings.items(): + if isinstance(v, MultipleChoiceSetting): + settings_kv[k] = ','.join(v.get_value()) + else: + settings_kv[k] = v.get_value() + + settings_kv['disabled_engines'] = ','.join(self.engines.disabled) + settings_kv['enabled_engines'] = ','.join(self.engines.enabled) + + settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled) + settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled) + + return urlencode(settings_kv) + + def parse_dict(self, input_data): for user_setting_name, user_setting in input_data.items(): if user_setting_name in self.key_value_settings: self.key_value_settings[user_setting_name].parse(user_setting) diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index e5477e737..49f67bcd8 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -283,6 +283,9 @@
{{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }}

+

{{ _('Search URL of the currently saved preferences') }} ({{ _('Note: specifying custom settings in the search URL can reduce privacy by leaking data to the clicked result sites.') }}):
+ +

{{ _('back') }}
diff --git a/searx/webapp.py b/searx/webapp.py index 29b0ce6af..e2825c050 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -392,7 +392,7 @@ def pre_request(): preferences = Preferences(themes, list(categories.keys()), engines, plugins) request.preferences = preferences try: - preferences.parse_cookies(request.cookies) + preferences.parse_dict(request.cookies) except: request.errors.append(gettext('Invalid settings, please edit your preferences')) @@ -402,6 +402,11 @@ def pre_request(): for k, v in request.args.items(): if k not in request.form: request.form[k] = v + try: + preferences.parse_dict(request.form) + except Exception as e: + logger.exception('invalid settings') + request.errors.append(gettext('Invalid settings')) # request.user_plugins request.user_plugins = [] @@ -685,6 +690,8 @@ def preferences(): plugins=plugins, allowed_plugins=allowed_plugins, theme=get_current_theme_name(), + preferences_url_params=request.preferences.get_as_url_params(), + base_url=get_base_url(), preferences=True)