Bläddra i källkod

[enh] generate search url of saved preferences

Adam Tauber 7 år sedan
förälder
incheckning
38d6ba4066
3 ändrade filer med 37 tillägg och 5 borttagningar
  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 Visa fil

@@ -1,5 +1,6 @@
1 1
 from searx import settings, autocomplete
2 2
 from searx.languages import language_codes as languages
3
+from searx.url_utils import urlencode
3 4
 
4 5
 
5 6
 COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years
@@ -232,7 +233,7 @@ class PluginsSetting(SwitchableSetting):
232 233
 
233 234
 
234 235
 class Preferences(object):
235
-    """Stores, validates and saves preferences to cookies"""
236
+    """Validates and saves preferences to cookies"""
236 237
 
237 238
     def __init__(self, themes, categories, engines, plugins):
238 239
         super(Preferences, self).__init__()
@@ -247,19 +248,40 @@ class Preferences(object):
247 248
                                    'image_proxy': MapSetting(settings['server']['image_proxy'],
248 249
                                                              map={'': settings['server']['image_proxy'],
249 250
                                                                   '0': False,
250
-                                                                  '1': True}),
251
+                                                                  '1': True,
252
+                                                                  'True': True,
253
+                                                                  'False': False}),
251 254
                                    'method': EnumStringSetting('POST', choices=('GET', 'POST')),
252 255
                                    'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0,
253 256
                                                                                                     '1': 1,
254 257
                                                                                                     '2': 2}),
255 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 264
         self.engines = EnginesSetting('engines', choices=engines)
259 265
         self.plugins = PluginsSetting('plugins', choices=plugins)
260 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 285
         for user_setting_name, user_setting in input_data.items():
264 286
             if user_setting_name in self.key_value_settings:
265 287
                 self.key_value_settings[user_setting_name].parse(user_setting)

+ 3
- 0
searx/templates/oscar/preferences.html Visa fil

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

+ 8
- 1
searx/webapp.py Visa fil

@@ -392,7 +392,7 @@ def pre_request():
392 392
     preferences = Preferences(themes, list(categories.keys()), engines, plugins)
393 393
     request.preferences = preferences
394 394
     try:
395
-        preferences.parse_cookies(request.cookies)
395
+        preferences.parse_dict(request.cookies)
396 396
     except:
397 397
         request.errors.append(gettext('Invalid settings, please edit your preferences'))
398 398
 
@@ -402,6 +402,11 @@ def pre_request():
402 402
     for k, v in request.args.items():
403 403
         if k not in request.form:
404 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 411
     # request.user_plugins
407 412
     request.user_plugins = []
@@ -685,6 +690,8 @@ def preferences():
685 690
                   plugins=plugins,
686 691
                   allowed_plugins=allowed_plugins,
687 692
                   theme=get_current_theme_name(),
693
+                  preferences_url_params=request.preferences.get_as_url_params(),
694
+                  base_url=get_base_url(),
688 695
                   preferences=True)
689 696
 
690 697