[enh] generate search url of saved preferences

This commit is contained in:
Adam Tauber 2017-07-10 12:47:25 +02:00
parent 36af8f9d67
commit 38d6ba4066
3 changed files with 37 additions and 5 deletions

View File

@ -1,5 +1,6 @@
from searx import settings, autocomplete from searx import settings, autocomplete
from searx.languages import language_codes as languages from searx.languages import language_codes as languages
from searx.url_utils import urlencode
COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years
@ -232,7 +233,7 @@ class PluginsSetting(SwitchableSetting):
class Preferences(object): class Preferences(object):
"""Stores, validates and saves preferences to cookies""" """Validates and saves preferences to cookies"""
def __init__(self, themes, categories, engines, plugins): def __init__(self, themes, categories, engines, plugins):
super(Preferences, self).__init__() super(Preferences, self).__init__()
@ -247,19 +248,40 @@ class Preferences(object):
'image_proxy': MapSetting(settings['server']['image_proxy'], 'image_proxy': MapSetting(settings['server']['image_proxy'],
map={'': settings['server']['image_proxy'], map={'': settings['server']['image_proxy'],
'0': False, '0': False,
'1': True}), '1': True,
'True': True,
'False': False}),
'method': EnumStringSetting('POST', choices=('GET', 'POST')), 'method': EnumStringSetting('POST', choices=('GET', 'POST')),
'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0, 'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0,
'1': 1, '1': 1,
'2': 2}), '2': 2}),
'theme': EnumStringSetting(settings['ui']['default_theme'], choices=themes), '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.engines = EnginesSetting('engines', choices=engines)
self.plugins = PluginsSetting('plugins', choices=plugins) self.plugins = PluginsSetting('plugins', choices=plugins)
self.unknown_params = {} 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(): for user_setting_name, user_setting in input_data.items():
if user_setting_name in self.key_value_settings: if user_setting_name in self.key_value_settings:
self.key_value_settings[user_setting_name].parse(user_setting) self.key_value_settings[user_setting_name].parse(user_setting)

View File

@ -283,6 +283,9 @@
<br /> <br />
{{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }} {{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }}
</p> </p>
<p style="margin:20px 0;">{{ _('Search URL of the currently saved preferences') }} <small class="text-muted">({{ _('Note: specifying custom settings in the search URL can reduce privacy by leaking data to the clicked result sites.') }})</small>:<br/>
<input readonly="" class="form-control select-all-on-click cursor-text" type="url" value="{{ url_for('index', _external=True) }}?{{ preferences_url_params|e }}{% raw %}&amp;q=%s{% endraw %}">
</p>
<input type="submit" class="btn btn-primary" value="{{ _('save') }}" /> <input type="submit" class="btn btn-primary" value="{{ _('save') }}" />
<a href="{{ url_for('index') }}"><div class="btn btn-default">{{ _('back') }}</div></a> <a href="{{ url_for('index') }}"><div class="btn btn-default">{{ _('back') }}</div></a>

View File

@ -392,7 +392,7 @@ def pre_request():
preferences = Preferences(themes, list(categories.keys()), engines, plugins) preferences = Preferences(themes, list(categories.keys()), engines, plugins)
request.preferences = preferences request.preferences = preferences
try: try:
preferences.parse_cookies(request.cookies) preferences.parse_dict(request.cookies)
except: except:
request.errors.append(gettext('Invalid settings, please edit your preferences')) request.errors.append(gettext('Invalid settings, please edit your preferences'))
@ -402,6 +402,11 @@ def pre_request():
for k, v in request.args.items(): for k, v in request.args.items():
if k not in request.form: if k not in request.form:
request.form[k] = v 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
request.user_plugins = [] request.user_plugins = []
@ -685,6 +690,8 @@ def preferences():
plugins=plugins, plugins=plugins,
allowed_plugins=allowed_plugins, allowed_plugins=allowed_plugins,
theme=get_current_theme_name(), theme=get_current_theme_name(),
preferences_url_params=request.preferences.get_as_url_params(),
base_url=get_base_url(),
preferences=True) preferences=True)