Browse Source

[enh] generate search url of saved preferences

Adam Tauber 7 years ago
parent
commit
38d6ba4066
3 changed files with 37 additions and 5 deletions
  1. 26
    4
      searx/preferences.py
  2. 3
    0
      searx/templates/oscar/preferences.html
  3. 8
    1
      searx/webapp.py

+ 26
- 4
searx/preferences.py View File

1
 from searx import settings, autocomplete
1
 from searx import settings, autocomplete
2
 from searx.languages import language_codes as languages
2
 from searx.languages import language_codes as languages
3
+from searx.url_utils import urlencode
3
 
4
 
4
 
5
 
5
 COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years
6
 COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years
232
 
233
 
233
 
234
 
234
 class Preferences(object):
235
 class Preferences(object):
235
-    """Stores, validates and saves preferences to cookies"""
236
+    """Validates and saves preferences to cookies"""
236
 
237
 
237
     def __init__(self, themes, categories, engines, plugins):
238
     def __init__(self, themes, categories, engines, plugins):
238
         super(Preferences, self).__init__()
239
         super(Preferences, self).__init__()
247
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],
248
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],
248
                                                              map={'': settings['server']['image_proxy'],
249
                                                              map={'': settings['server']['image_proxy'],
249
                                                                   '0': False,
250
                                                                   '0': False,
250
-                                                                  '1': True}),
251
+                                                                  '1': True,
252
+                                                                  'True': True,
253
+                                                                  'False': False}),
251
                                    'method': EnumStringSetting('POST', choices=('GET', 'POST')),
254
                                    'method': EnumStringSetting('POST', choices=('GET', 'POST')),
252
                                    'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0,
255
                                    'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0,
253
                                                                                                     '1': 1,
256
                                                                                                     '1': 1,
254
                                                                                                     '2': 2}),
257
                                                                                                     '2': 2}),
255
                                    'theme': EnumStringSetting(settings['ui']['default_theme'], choices=themes),
258
                                    'theme': EnumStringSetting(settings['ui']['default_theme'], choices=themes),
256
-                                   'results_on_new_tab': MapSetting(False, map={'0': False, '1': True})}
259
+                                   'results_on_new_tab': MapSetting(False, map={'0': False,
260
+                                                                                '1': True,
261
+                                                                                'False': False,
262
+                                                                                'True': True})}
257
 
263
 
258
         self.engines = EnginesSetting('engines', choices=engines)
264
         self.engines = EnginesSetting('engines', choices=engines)
259
         self.plugins = PluginsSetting('plugins', choices=plugins)
265
         self.plugins = PluginsSetting('plugins', choices=plugins)
260
         self.unknown_params = {}
266
         self.unknown_params = {}
261
 
267
 
262
-    def parse_cookies(self, input_data):
268
+    def get_as_url_params(self):
269
+        settings_kv = {}
270
+        for k, v in self.key_value_settings.items():
271
+            if isinstance(v, MultipleChoiceSetting):
272
+                settings_kv[k] = ','.join(v.get_value())
273
+            else:
274
+                settings_kv[k] = v.get_value()
275
+
276
+        settings_kv['disabled_engines'] = ','.join(self.engines.disabled)
277
+        settings_kv['enabled_engines'] = ','.join(self.engines.enabled)
278
+
279
+        settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled)
280
+        settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled)
281
+
282
+        return urlencode(settings_kv)
283
+
284
+    def parse_dict(self, input_data):
263
         for user_setting_name, user_setting in input_data.items():
285
         for user_setting_name, user_setting in input_data.items():
264
             if user_setting_name in self.key_value_settings:
286
             if user_setting_name in self.key_value_settings:
265
                 self.key_value_settings[user_setting_name].parse(user_setting)
287
                 self.key_value_settings[user_setting_name].parse(user_setting)

+ 3
- 0
searx/templates/oscar/preferences.html View File

283
         <br />
283
         <br />
284
         {{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }}
284
         {{ _("These cookies serve your sole convenience, we don't use these cookies to track you.") }}
285
         </p>
285
         </p>
286
+        <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/>
287
+          <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 %}">
288
+        </p>
286
 
289
 
287
         <input type="submit" class="btn btn-primary" value="{{ _('save') }}" />
290
         <input type="submit" class="btn btn-primary" value="{{ _('save') }}" />
288
         <a href="{{ url_for('index') }}"><div class="btn btn-default">{{ _('back') }}</div></a>
291
         <a href="{{ url_for('index') }}"><div class="btn btn-default">{{ _('back') }}</div></a>

+ 8
- 1
searx/webapp.py View File

392
     preferences = Preferences(themes, list(categories.keys()), engines, plugins)
392
     preferences = Preferences(themes, list(categories.keys()), engines, plugins)
393
     request.preferences = preferences
393
     request.preferences = preferences
394
     try:
394
     try:
395
-        preferences.parse_cookies(request.cookies)
395
+        preferences.parse_dict(request.cookies)
396
     except:
396
     except:
397
         request.errors.append(gettext('Invalid settings, please edit your preferences'))
397
         request.errors.append(gettext('Invalid settings, please edit your preferences'))
398
 
398
 
402
     for k, v in request.args.items():
402
     for k, v in request.args.items():
403
         if k not in request.form:
403
         if k not in request.form:
404
             request.form[k] = v
404
             request.form[k] = v
405
+    try:
406
+        preferences.parse_dict(request.form)
407
+    except Exception as e:
408
+        logger.exception('invalid settings')
409
+        request.errors.append(gettext('Invalid settings'))
405
 
410
 
406
     # request.user_plugins
411
     # request.user_plugins
407
     request.user_plugins = []
412
     request.user_plugins = []
685
                   plugins=plugins,
690
                   plugins=plugins,
686
                   allowed_plugins=allowed_plugins,
691
                   allowed_plugins=allowed_plugins,
687
                   theme=get_current_theme_name(),
692
                   theme=get_current_theme_name(),
693
+                  preferences_url_params=request.preferences.get_as_url_params(),
694
+                  base_url=get_base_url(),
688
                   preferences=True)
695
                   preferences=True)
689
 
696
 
690
 
697