Repository: searx/searx

Issue:
Search URL settings are not used
Hi,

I configured Searx tested the generated preferences URL several times now also with different browser, but it is always the same, the settings are not used.
That's the URL:

`https://***/searx/?preferences=eJx1VMmO2zAM_Zr6YrTocujJh6JF0QEKzKAz7VVgJNohLIseUU7ivy-VcWy5yyGOTVGP73GLKJNPYjiYgGeT4NB8BS9YOSYTUdifMDYM-vmGY1d5CN0EHTYOK88W_PXNkcDBozOjnzoK0vyi4bWnHs2RU4-zvHr_-RF9a-5Cy3GARByy7X7EkKMoBeMp9CuPQ-SzYMx8Vr9P1qKI-XJ_p1fOkRJWNCgVM0a-zAttmBJbHkaPSZlNts-_jiuBFgUh2mPztkpHHLBhsRArDHvm356eHh5vATS0Mqagr0ZsZO_V8hTB9srt54_v6jewJugqL4Nn_hYSdhxnI-jRpi05GDQCStOT7UGVmJY8ZnltRKyF23SGiLWjqNcygKGkpyeGZIywJfD1gI5AjRTAmBM55AwQ0Tn626lj7jzWo4e5hnEsIlprX6dTgdBRpyRBFKTDgBF8AasVyT4gvSTEoXRpXWRyG3CYQYld65I_V6lboILTMAlZY65_ejRDcHgp0TvEPtGAsqTiBf3m76g7pP9B84mwjPvuUtASRDei1nDF2ix_JXW91UJkXrPh6RAhznVmKyQl7dwJdMJ6bz1QOmgzYlrEzHDMcEUuo44AgS1CvkjaOb2Q2AyY5oGDTs_OLXE_c2I5cg9hk3kEJZ0fu4SuktsIA2RhuJwP86AdripThCBeO9uVUejyPGmFSxPrqEYcuWi25zOEXV9tc1m_KP1nafYtdfv6Q9hKfWmGm9A_3G635UwiVndLGdFCiLWkONk0xUxGLGGweGWTa2nZYZ0fS1YKlE3Uuw8fPl52E5U8HJYbC7sbjXUsrnTXHXRbELfO-1fGSqskiGnMK7B0dV3t8Lq08pbdtYpuVdoKWA2Yjuyah_vHp2rZWjo0zXL8Gw8DTj0=&q=`

What could be the issue?

Thanks,
Hyper





Discussion:
hi, i just tried your issue on python3.6

here is the traceback

```python
Traceback (most recent call last):
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/app.py", line 1811, in full_dispatch_request
    rv = self.preprocess_request()
  File "/home/q/envs/searx/lib/python3.6/site-packages/flask/app.py", line 2087, in preprocess_request
    rv = func()
  File "/home/q/git/searx/searx/webapp.py", line 422, in pre_request
    preferences.parse_encoded_data(request.form['preferences'])
  File "/home/q/git/searx/searx/preferences.py", line 295, in parse_encoded_data
    self.parse_dict({x: y[0] for x, y in parse_qs(unicode(decoded_data)).items()})
  File "/home/q/git/searx/searx/preferences.py", line 300, in parse_dict
    self.key_value_settings[user_setting_name].parse(user_setting)
  File "/home/q/git/searx/searx/preferences.py", line 94, in parse
    self._validate_selections(elements)
  File "/home/q/git/searx/searx/preferences.py", line 81, in _validate_selections
    raise ValidationException('Invalid value: "{0}"'.format(selections))
searx.preferences.ValidationException: Invalid value: "["general'"]"
```
it seem there is error on quote character there
this is an error from str typecase 

original

```python
>>> url_params = 'e...='
>>> from zlib import compress, decompress
>>> decoded_data = decompress(urlsafe_b64decode(url_params.encode('utf-8')))
>>> dict_data = {x: y[0] for x, y in parse_qs(unicode(decoded_data)).items()}
>>> dict_data['categories']
"general'"
```

compare to this

```python
>>> dict_data = {x: y[0] for x, y in parse_qs(unicode(decoded_data)).items()}
>>> dict_data['categories']
>>> dict_data = {}
>>> for x, y in parse_qs(decoded_data).items():
>>>     dict_data[x.decode('utf8')] = y[0].decode('utf8')
>>> dict_data['categories']
'general'
```

will make pr for this later


Resolving patch:
diff --git a/searx/preferences.py b/searx/preferences.py
--- a/searx/preferences.py
+++ b/searx/preferences.py
@@ -292,7 +292,10 @@ def get_as_url_params(self):
 
     def parse_encoded_data(self, input_data):
         decoded_data = decompress(urlsafe_b64decode(input_data.encode('utf-8')))
-        self.parse_dict({x: y[0] for x, y in parse_qs(unicode(decoded_data)).items()})
+        dict_data = {}
+        for x, y in parse_qs(decoded_data).items():
+            dict_data[x.decode('utf8')] = y[0].decode('utf8')
+        self.parse_dict(dict_data)
 
     def parse_dict(self, input_data):
         for user_setting_name, user_setting in input_data.items():


Test patch:
diff --git a/tests/unit/test_preferences.py b/tests/unit/test_preferences.py
--- a/tests/unit/test_preferences.py
+++ b/tests/unit/test_preferences.py
@@ -122,3 +122,23 @@ def test_plugins_setting_few_default_enabled(self):
         plugin3 = PluginStub('plugin3', True)
         setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3])
         self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin3']))
+
+
+class TestPreferences(SearxTestCase):
+
+    def test_encode(self):
+        from searx.preferences import Preferences
+        pref = Preferences(['oscar'], ['general'], {}, [])
+        url_params = 'eJx1VMmO2zAM_Zr6YrTocujJh6JF0QEKzKAz7VVgJNohLIseUU7ivy-VcWy5yyGOTVGP73GLKJNPYjiYgGeT4NB8BS9YOSY' \
+            'TUdifMDYM-vmGY1d5CN0EHTYOK88W_PXNkcDBozOjnzoK0vyi4bWnHs2RU4-zvHr_-RF9a-5Cy3GARByy7X7EkKMoBeMp9CuPQ-SzYMx' \
+            '8Vr9P1qKI-XJ_p1fOkRJWNCgVM0a-zAttmBJbHkaPSZlNts-_jiuBFgUh2mPztkpHHLBhsRArDHvm356eHh5vATS0Mqagr0ZsZO_V8hT' \
+            'B9srt54_v6jewJugqL4Nn_hYSdhxnI-jRpi05GDQCStOT7UGVmJY8ZnltRKyF23SGiLWjqNcygKGkpyeGZIywJfD1gI5AjRTAmBM55Aw' \
+            'Q0Tn626lj7jzWo4e5hnEsIlprX6dTgdBRpyRBFKTDgBF8AasVyT4gvSTEoXRpXWRyG3CYQYld65I_V6lboILTMAlZY65_ejRDcHgp0Tv' \
+            'EPtGAsqTiBf3m76g7pP9B84mwjPvuUtASRDei1nDF2ix_JXW91UJkXrPh6RAhznVmKyQl7dwJdMJ6bz1QOmgzYlrEzHDMcEUuo44AgS1' \
+            'CvkjaOb2Q2AyY5oGDTs_OLXE_c2I5cg9hk3kEJZ0fu4SuktsIA2RhuJwP86AdripThCBeO9uVUejyPGmFSxPrqEYcuWi25zOEXV9tc1m' \
+            '_KP1nafYtdfv6Q9hKfWmGm9A_3G635UwiVndLGdFCiLWkONk0xUxGLGGweGWTa2nZYZ0fS1YKlE3Uuw8fPl52E5U8HJYbC7sbjXUsrnT' \
+            'XHXRbELfO-1fGSqskiGnMK7B0dV3t8Lq08pbdtYpuVdoKWA2Yjuyah_vHp2rZWjo0zXL8Gw8DTj0='
+        pref.parse_encoded_data(url_params)
+        self.assertEqual(
+            vars(pref.key_value_settings['categories']),
+            {'value': ['general'], 'choices': ['general', 'none']})