2020-02-23 20:52:00 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-26 20:18:20 +02:00
|
|
|
# lint: pylint
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Searx preferences implementation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# pylint: disable=useless-object-inheritance
|
|
|
|
|
2017-10-25 23:56:37 +02:00
|
|
|
from base64 import urlsafe_b64encode, urlsafe_b64decode
|
|
|
|
from zlib import compress, decompress
|
2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import parse_qs, urlencode
|
2017-10-25 23:56:37 +02:00
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
from searx import settings, autocomplete
|
|
|
|
from searx.languages import language_codes as languages
|
2020-10-01 11:29:31 +02:00
|
|
|
from searx.webutils import VALID_LANGUAGE_CODE
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years
|
2020-02-23 10:03:42 +01:00
|
|
|
LANGUAGE_CODES = [l[0] for l in languages]
|
2019-01-06 15:27:46 +01:00
|
|
|
LANGUAGE_CODES.append('all')
|
2016-04-08 16:38:05 +02:00
|
|
|
DISABLED = 0
|
|
|
|
ENABLED = 1
|
2017-11-01 13:58:48 +01:00
|
|
|
DOI_RESOLVERS = list(settings['doi_resolvers'])
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MissingArgumentException(Exception):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Exption from ``cls._post_init`` when a argument is missed.
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ValidationException(Exception):
|
2020-02-23 20:52:00 +01:00
|
|
|
|
|
|
|
"""Exption from ``cls._post_init`` when configuration value is invalid.
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
2020-08-12 09:42:27 +02:00
|
|
|
class Setting:
|
2016-04-08 16:38:05 +02:00
|
|
|
"""Base class of user settings"""
|
|
|
|
|
2020-10-23 16:22:55 +02:00
|
|
|
def __init__(self, default_value, locked=False, **kwargs):
|
2020-08-27 14:38:39 +02:00
|
|
|
super().__init__()
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value = default_value
|
2020-10-23 16:22:55 +02:00
|
|
|
self.locked = locked
|
2016-11-30 18:43:03 +01:00
|
|
|
for key, value in kwargs.items():
|
2016-04-08 16:38:05 +02:00
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
self._post_init()
|
|
|
|
|
|
|
|
def _post_init(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse ``data`` and store the result at ``self.value``
|
|
|
|
|
|
|
|
If needed, its overwritten in the inheritance.
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value = data
|
|
|
|
|
|
|
|
def get_value(self):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Returns the value of the setting
|
|
|
|
|
|
|
|
If needed, its overwritten in the inheritance.
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
return self.value
|
|
|
|
|
|
|
|
def save(self, name, resp):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Save cookie ``name`` in the HTTP reponse obect
|
|
|
|
|
|
|
|
If needed, its overwritten in the inheritance."""
|
2016-11-30 18:43:03 +01:00
|
|
|
resp.set_cookie(name, self.value, max_age=COOKIE_MAX_AGE)
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StringSetting(Setting):
|
|
|
|
"""Setting of plain string values"""
|
|
|
|
|
|
|
|
|
|
|
|
class EnumStringSetting(Setting):
|
|
|
|
"""Setting of a value which can only come from the given choices"""
|
|
|
|
|
|
|
|
def _post_init(self):
|
|
|
|
if not hasattr(self, 'choices'):
|
|
|
|
raise MissingArgumentException('Missing argument: choices')
|
2016-11-14 22:24:40 +01:00
|
|
|
self._validate_selection(self.value)
|
2016-04-08 16:38:05 +02:00
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def _validate_selection(self, selection):
|
|
|
|
if selection not in self.choices: # pylint: disable=no-member
|
|
|
|
raise ValidationException('Invalid value: "{0}"'.format(selection))
|
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse and validate ``data`` and store the result at ``self.value``
|
|
|
|
"""
|
2016-12-14 03:55:56 +01:00
|
|
|
self._validate_selection(data)
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value = data
|
|
|
|
|
|
|
|
|
|
|
|
class MultipleChoiceSetting(EnumStringSetting):
|
|
|
|
"""Setting of values which can only come from the given choices"""
|
|
|
|
|
2016-11-14 22:24:40 +01:00
|
|
|
def _validate_selections(self, selections):
|
|
|
|
for item in selections:
|
2020-02-23 20:52:00 +01:00
|
|
|
if item not in self.choices: # pylint: disable=no-member
|
2016-11-14 22:24:40 +01:00
|
|
|
raise ValidationException('Invalid value: "{0}"'.format(selections))
|
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
def _post_init(self):
|
|
|
|
if not hasattr(self, 'choices'):
|
|
|
|
raise MissingArgumentException('Missing argument: choices')
|
2016-11-14 22:24:40 +01:00
|
|
|
self._validate_selections(self.value)
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse and validate ``data`` and store the result at ``self.value``
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
if data == '':
|
|
|
|
self.value = []
|
|
|
|
return
|
|
|
|
|
|
|
|
elements = data.split(',')
|
2016-11-14 22:24:40 +01:00
|
|
|
self._validate_selections(elements)
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value = elements
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def parse_form(self, data): # pylint: disable=missing-function-docstring
|
2020-10-23 16:22:55 +02:00
|
|
|
if self.locked:
|
|
|
|
return
|
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value = []
|
|
|
|
for choice in data:
|
2020-02-23 20:52:00 +01:00
|
|
|
if choice in self.choices and choice not in self.value: # pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
self.value.append(choice)
|
|
|
|
|
|
|
|
def save(self, name, resp):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Save cookie ``name`` in the HTTP reponse obect
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE)
|
|
|
|
|
|
|
|
|
2020-02-01 11:01:17 +01:00
|
|
|
class SetSetting(Setting):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Setting of values of type ``set`` (comma separated string) """
|
2020-02-01 11:01:17 +01:00
|
|
|
def _post_init(self):
|
|
|
|
if not hasattr(self, 'values'):
|
|
|
|
self.values = set()
|
|
|
|
|
|
|
|
def get_value(self):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Returns a string with comma separated values.
|
|
|
|
"""
|
2020-02-01 11:01:17 +01:00
|
|
|
return ','.join(self.values)
|
|
|
|
|
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse and validate ``data`` and store the result at ``self.value``
|
|
|
|
"""
|
2020-02-01 11:01:17 +01:00
|
|
|
if data == '':
|
2020-02-23 20:52:00 +01:00
|
|
|
self.values = set() # pylint: disable=attribute-defined-outside-init
|
2020-02-01 11:01:17 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
elements = data.split(',')
|
|
|
|
for element in elements:
|
|
|
|
self.values.add(element)
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def parse_form(self, data): # pylint: disable=missing-function-docstring
|
2020-10-23 16:22:55 +02:00
|
|
|
if self.locked:
|
|
|
|
return
|
|
|
|
|
2020-02-01 11:01:17 +01:00
|
|
|
elements = data.split(',')
|
2020-02-23 20:52:00 +01:00
|
|
|
self.values = set(elements) # pylint: disable=attribute-defined-outside-init
|
2020-02-01 11:01:17 +01:00
|
|
|
|
|
|
|
def save(self, name, resp):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Save cookie ``name`` in the HTTP reponse obect
|
|
|
|
"""
|
2020-02-01 11:01:17 +01:00
|
|
|
resp.set_cookie(name, ','.join(self.values), max_age=COOKIE_MAX_AGE)
|
|
|
|
|
|
|
|
|
2016-12-14 03:55:56 +01:00
|
|
|
class SearchLanguageSetting(EnumStringSetting):
|
|
|
|
"""Available choices may change, so user's value may not be in choices anymore"""
|
|
|
|
|
2020-02-23 10:03:42 +01:00
|
|
|
def _validate_selection(self, selection):
|
2020-10-01 11:29:31 +02:00
|
|
|
if selection != '' and not VALID_LANGUAGE_CODE.match(selection):
|
2020-02-23 10:03:42 +01:00
|
|
|
raise ValidationException('Invalid language code: "{0}"'.format(selection))
|
|
|
|
|
2016-12-14 03:55:56 +01:00
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse and validate ``data`` and store the result at ``self.value``
|
|
|
|
"""
|
|
|
|
if data not in self.choices and data != self.value: # pylint: disable=no-member
|
2016-12-14 03:55:56 +01:00
|
|
|
# hack to give some backwards compatibility with old language cookies
|
|
|
|
data = str(data).replace('_', '-')
|
|
|
|
lang = data.split('-')[0]
|
2020-02-23 20:52:00 +01:00
|
|
|
# pylint: disable=no-member
|
2016-12-14 03:55:56 +01:00
|
|
|
if data in self.choices:
|
|
|
|
pass
|
|
|
|
elif lang in self.choices:
|
|
|
|
data = lang
|
|
|
|
else:
|
2016-12-14 06:51:15 +01:00
|
|
|
data = self.value
|
2020-10-01 11:29:31 +02:00
|
|
|
self._validate_selection(data)
|
2016-12-14 03:55:56 +01:00
|
|
|
self.value = data
|
|
|
|
|
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
class MapSetting(Setting):
|
|
|
|
"""Setting of a value that has to be translated in order to be storable"""
|
|
|
|
|
|
|
|
def _post_init(self):
|
|
|
|
if not hasattr(self, 'map'):
|
|
|
|
raise MissingArgumentException('missing argument: map')
|
2020-02-23 20:52:00 +01:00
|
|
|
if self.value not in self.map.values(): # pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
raise ValidationException('Invalid default value')
|
|
|
|
|
|
|
|
def parse(self, data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse and validate ``data`` and store the result at ``self.value``
|
|
|
|
"""
|
|
|
|
# pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
if data not in self.map:
|
|
|
|
raise ValidationException('Invalid choice: {0}'.format(data))
|
|
|
|
self.value = self.map[data]
|
2020-02-23 20:52:00 +01:00
|
|
|
self.key = data # pylint: disable=attribute-defined-outside-init
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
def save(self, name, resp):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Save cookie ``name`` in the HTTP reponse obect
|
|
|
|
"""
|
2017-01-21 20:08:08 +01:00
|
|
|
if hasattr(self, 'key'):
|
2016-11-30 18:43:03 +01:00
|
|
|
resp.set_cookie(name, self.key, max_age=COOKIE_MAX_AGE)
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SwitchableSetting(Setting):
|
|
|
|
""" Base class for settings that can be turned on && off"""
|
|
|
|
|
|
|
|
def _post_init(self):
|
|
|
|
self.disabled = set()
|
|
|
|
self.enabled = set()
|
|
|
|
if not hasattr(self, 'choices'):
|
|
|
|
raise MissingArgumentException('missing argument: choices')
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def transform_form_items(self, items): # pylint: disable=missing-function-docstring
|
|
|
|
# pylint: disable=no-self-use
|
2016-04-08 16:38:05 +02:00
|
|
|
return items
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def transform_values(self, values): # pylint: disable=missing-function-docstring
|
|
|
|
# pylint: disable=no-self-use
|
2016-04-08 16:38:05 +02:00
|
|
|
return values
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def parse_cookie(self, data): # pylint: disable=missing-function-docstring
|
|
|
|
# pylint: disable=attribute-defined-outside-init
|
2016-04-08 16:38:05 +02:00
|
|
|
if data[DISABLED] != '':
|
|
|
|
self.disabled = set(data[DISABLED].split(','))
|
|
|
|
if data[ENABLED] != '':
|
|
|
|
self.enabled = set(data[ENABLED].split(','))
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def parse_form(self, items): # pylint: disable=missing-function-docstring
|
2020-10-23 16:22:55 +02:00
|
|
|
if self.locked:
|
|
|
|
return
|
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
items = self.transform_form_items(items)
|
2020-02-23 20:52:00 +01:00
|
|
|
self.disabled = set() # pylint: disable=attribute-defined-outside-init
|
|
|
|
self.enabled = set() # pylint: disable=attribute-defined-outside-init
|
|
|
|
for choice in self.choices: # pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
if choice['default_on']:
|
|
|
|
if choice['id'] in items:
|
|
|
|
self.disabled.add(choice['id'])
|
|
|
|
else:
|
|
|
|
if choice['id'] not in items:
|
|
|
|
self.enabled.add(choice['id'])
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def save(self, resp): # pylint: disable=arguments-differ
|
|
|
|
"""Save cookie in the HTTP reponse obect
|
|
|
|
"""
|
2016-04-08 16:38:05 +02:00
|
|
|
resp.set_cookie('disabled_{0}'.format(self.value), ','.join(self.disabled), max_age=COOKIE_MAX_AGE)
|
|
|
|
resp.set_cookie('enabled_{0}'.format(self.value), ','.join(self.enabled), max_age=COOKIE_MAX_AGE)
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def get_disabled(self): # pylint: disable=missing-function-docstring
|
2016-04-08 16:38:05 +02:00
|
|
|
disabled = self.disabled
|
2020-02-23 20:52:00 +01:00
|
|
|
for choice in self.choices: # pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
if not choice['default_on'] and choice['id'] not in self.enabled:
|
|
|
|
disabled.add(choice['id'])
|
|
|
|
return self.transform_values(disabled)
|
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def get_enabled(self): # pylint: disable=missing-function-docstring
|
2016-04-08 16:38:05 +02:00
|
|
|
enabled = self.enabled
|
2020-02-23 20:52:00 +01:00
|
|
|
for choice in self.choices: # pylint: disable=no-member
|
2016-04-08 16:38:05 +02:00
|
|
|
if choice['default_on'] and choice['id'] not in self.disabled:
|
|
|
|
enabled.add(choice['id'])
|
|
|
|
return self.transform_values(enabled)
|
|
|
|
|
|
|
|
|
|
|
|
class EnginesSetting(SwitchableSetting):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Engine settings"""
|
2016-07-10 16:44:27 +02:00
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
def _post_init(self):
|
2020-08-27 14:38:39 +02:00
|
|
|
super()._post_init()
|
2016-04-08 16:38:05 +02:00
|
|
|
transformed_choices = []
|
2020-02-23 20:52:00 +01:00
|
|
|
for engine_name, engine in self.choices.items(): # pylint: disable=no-member,access-member-before-definition
|
2016-04-08 16:38:05 +02:00
|
|
|
for category in engine.categories:
|
|
|
|
transformed_choice = dict()
|
|
|
|
transformed_choice['default_on'] = not engine.disabled
|
|
|
|
transformed_choice['id'] = '{}__{}'.format(engine_name, category)
|
|
|
|
transformed_choices.append(transformed_choice)
|
|
|
|
self.choices = transformed_choices
|
|
|
|
|
|
|
|
def transform_form_items(self, items):
|
|
|
|
return [item[len('engine_'):].replace('_', ' ').replace(' ', '__') for item in items]
|
|
|
|
|
|
|
|
def transform_values(self, values):
|
2016-05-04 16:14:04 +02:00
|
|
|
if len(values) == 1 and next(iter(values)) == '':
|
2016-04-08 16:38:05 +02:00
|
|
|
return list()
|
|
|
|
transformed_values = []
|
|
|
|
for value in values:
|
|
|
|
engine, category = value.split('__')
|
|
|
|
transformed_values.append((engine, category))
|
|
|
|
return transformed_values
|
|
|
|
|
|
|
|
|
|
|
|
class PluginsSetting(SwitchableSetting):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Plugin settings"""
|
2016-07-10 16:44:27 +02:00
|
|
|
|
2016-04-08 16:38:05 +02:00
|
|
|
def _post_init(self):
|
2020-08-27 14:38:39 +02:00
|
|
|
super()._post_init()
|
2016-04-08 16:38:05 +02:00
|
|
|
transformed_choices = []
|
2020-02-23 20:52:00 +01:00
|
|
|
for plugin in self.choices: # pylint: disable=access-member-before-definition
|
2016-04-08 16:38:05 +02:00
|
|
|
transformed_choice = dict()
|
|
|
|
transformed_choice['default_on'] = plugin.default_on
|
|
|
|
transformed_choice['id'] = plugin.id
|
|
|
|
transformed_choices.append(transformed_choice)
|
|
|
|
self.choices = transformed_choices
|
|
|
|
|
|
|
|
def transform_form_items(self, items):
|
|
|
|
return [item[len('plugin_'):] for item in items]
|
|
|
|
|
|
|
|
|
2020-08-12 09:42:27 +02:00
|
|
|
class Preferences:
|
2017-07-10 12:47:25 +02:00
|
|
|
"""Validates and saves preferences to cookies"""
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
def __init__(self, themes, categories, engines, plugins):
|
2020-08-27 14:38:39 +02:00
|
|
|
super().__init__()
|
2016-04-08 16:38:05 +02:00
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
self.key_value_settings = {
|
|
|
|
'categories': MultipleChoiceSetting(
|
2020-10-23 16:22:55 +02:00
|
|
|
['general'],
|
|
|
|
is_locked('categories'),
|
|
|
|
choices=categories + ['none']
|
2020-02-23 20:52:00 +01:00
|
|
|
),
|
|
|
|
'language': SearchLanguageSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['search'].get('default_lang', ''),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('language'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=list(LANGUAGE_CODES) + ['']
|
|
|
|
),
|
|
|
|
'locale': EnumStringSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['ui'].get('default_locale', ''),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('locale'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=list(settings['locales'].keys()) + ['']
|
|
|
|
),
|
|
|
|
'autocomplete': EnumStringSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['search'].get('autocomplete', ''),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('autocomplete'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=list(autocomplete.backends.keys()) + ['']
|
|
|
|
),
|
|
|
|
'image_proxy': MapSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['server'].get('image_proxy', False),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('image_proxy'),
|
2020-02-23 20:56:05 +01:00
|
|
|
map={
|
|
|
|
'': settings['server'].get('image_proxy', 0),
|
|
|
|
'0': False,
|
|
|
|
'1': True,
|
|
|
|
'True': True,
|
|
|
|
'False': False
|
|
|
|
}
|
2020-02-23 20:52:00 +01:00
|
|
|
),
|
|
|
|
'method': EnumStringSetting(
|
2020-08-09 22:57:31 +02:00
|
|
|
settings['server'].get('method', 'POST'),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('method'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=('GET', 'POST')
|
|
|
|
),
|
|
|
|
'safesearch': MapSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['search'].get('safe_search', 0),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('safesearch'),
|
2020-02-23 20:56:05 +01:00
|
|
|
map={
|
|
|
|
'0': 0,
|
|
|
|
'1': 1,
|
|
|
|
'2': 2
|
|
|
|
}
|
|
|
|
),
|
2020-02-23 20:52:00 +01:00
|
|
|
'theme': EnumStringSetting(
|
2020-02-23 20:56:05 +01:00
|
|
|
settings['ui'].get('default_theme', 'oscar'),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('theme'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=themes
|
|
|
|
),
|
|
|
|
'results_on_new_tab': MapSetting(
|
2020-09-01 18:41:48 +02:00
|
|
|
settings['ui'].get('results_on_new_tab', False),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('results_on_new_tab'),
|
2020-02-23 20:56:05 +01:00
|
|
|
map={
|
|
|
|
'0': False,
|
|
|
|
'1': True,
|
|
|
|
'False': False,
|
|
|
|
'True': True
|
|
|
|
}
|
2020-02-23 20:52:00 +01:00
|
|
|
),
|
|
|
|
'doi_resolver': MultipleChoiceSetting(
|
2021-04-04 13:36:33 +02:00
|
|
|
[settings['default_doi_resolver'], ],
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('doi_resolver'),
|
|
|
|
choices=DOI_RESOLVERS
|
2020-02-23 20:52:00 +01:00
|
|
|
),
|
|
|
|
'oscar-style': EnumStringSetting(
|
|
|
|
settings['ui'].get('theme_args', {}).get('oscar_style', 'logicodev'),
|
2020-10-23 16:22:55 +02:00
|
|
|
is_locked('oscar-style'),
|
2020-02-23 20:52:00 +01:00
|
|
|
choices=['', 'logicodev', 'logicodev-dark', 'pointhi']),
|
2020-11-22 18:00:21 +01:00
|
|
|
'advanced_search': MapSetting(
|
|
|
|
settings['ui'].get('advanced_search', False),
|
2020-11-23 19:13:29 +01:00
|
|
|
is_locked('advanced_search'),
|
2020-11-22 18:00:21 +01:00
|
|
|
map={
|
|
|
|
'0': False,
|
|
|
|
'1': True,
|
|
|
|
'False': False,
|
2020-11-23 19:13:29 +01:00
|
|
|
'True': True,
|
|
|
|
'on': True,
|
2020-11-22 18:00:21 +01:00
|
|
|
}
|
|
|
|
),
|
2020-02-23 20:52:00 +01:00
|
|
|
}
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
self.engines = EnginesSetting('engines', choices=engines)
|
|
|
|
self.plugins = PluginsSetting('plugins', choices=plugins)
|
2020-02-01 11:01:17 +01:00
|
|
|
self.tokens = SetSetting('tokens')
|
2016-06-26 01:44:42 +02:00
|
|
|
self.unknown_params = {}
|
2016-04-08 16:38:05 +02:00
|
|
|
|
2017-07-10 12:47:25 +02:00
|
|
|
def get_as_url_params(self):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Return preferences as URL parameters"""
|
2017-07-10 12:47:25 +02:00
|
|
|
settings_kv = {}
|
|
|
|
for k, v in self.key_value_settings.items():
|
2020-10-23 16:22:55 +02:00
|
|
|
if v.locked:
|
|
|
|
continue
|
2017-07-10 12:47:25 +02:00
|
|
|
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)
|
|
|
|
|
2020-02-01 11:01:17 +01:00
|
|
|
settings_kv['tokens'] = ','.join(self.tokens.values)
|
|
|
|
|
2020-08-06 17:42:46 +02:00
|
|
|
return urlsafe_b64encode(compress(urlencode(settings_kv).encode())).decode()
|
2017-10-25 23:56:37 +02:00
|
|
|
|
|
|
|
def parse_encoded_data(self, input_data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""parse (base64) preferences from request (``flask.request.form['preferences']``)"""
|
2020-08-06 17:42:46 +02:00
|
|
|
decoded_data = decompress(urlsafe_b64decode(input_data.encode()))
|
2019-07-17 09:42:40 +02:00
|
|
|
dict_data = {}
|
|
|
|
for x, y in parse_qs(decoded_data).items():
|
2020-08-06 17:42:46 +02:00
|
|
|
dict_data[x.decode()] = y[0].decode()
|
2019-07-17 09:42:40 +02:00
|
|
|
self.parse_dict(dict_data)
|
2017-07-10 12:47:25 +02:00
|
|
|
|
|
|
|
def parse_dict(self, input_data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""parse preferences from request (``flask.request.form``)"""
|
2016-11-30 18:43:03 +01:00
|
|
|
for user_setting_name, user_setting in input_data.items():
|
2016-04-08 16:38:05 +02:00
|
|
|
if user_setting_name in self.key_value_settings:
|
2020-10-23 16:22:55 +02:00
|
|
|
if self.key_value_settings[user_setting_name].locked:
|
|
|
|
continue
|
2016-04-08 16:38:05 +02:00
|
|
|
self.key_value_settings[user_setting_name].parse(user_setting)
|
|
|
|
elif user_setting_name == 'disabled_engines':
|
2016-04-09 17:43:00 +02:00
|
|
|
self.engines.parse_cookie((input_data.get('disabled_engines', ''),
|
|
|
|
input_data.get('enabled_engines', '')))
|
2016-04-08 16:38:05 +02:00
|
|
|
elif user_setting_name == 'disabled_plugins':
|
2016-04-09 17:43:00 +02:00
|
|
|
self.plugins.parse_cookie((input_data.get('disabled_plugins', ''),
|
|
|
|
input_data.get('enabled_plugins', '')))
|
2020-02-01 11:01:17 +01:00
|
|
|
elif user_setting_name == 'tokens':
|
|
|
|
self.tokens.parse(user_setting)
|
2017-12-11 00:20:28 +01:00
|
|
|
elif not any(user_setting_name.startswith(x) for x in [
|
|
|
|
'enabled_',
|
|
|
|
'disabled_',
|
|
|
|
'engine_',
|
|
|
|
'category_',
|
|
|
|
'plugin_']):
|
2017-12-05 22:30:20 +01:00
|
|
|
self.unknown_params[user_setting_name] = user_setting
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
def parse_form(self, input_data):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Parse formular (``<input>``) data from a ``flask.request.form``"""
|
2016-04-08 16:38:05 +02:00
|
|
|
disabled_engines = []
|
|
|
|
enabled_categories = []
|
|
|
|
disabled_plugins = []
|
2016-11-30 18:43:03 +01:00
|
|
|
for user_setting_name, user_setting in input_data.items():
|
2016-04-08 16:38:05 +02:00
|
|
|
if user_setting_name in self.key_value_settings:
|
|
|
|
self.key_value_settings[user_setting_name].parse(user_setting)
|
|
|
|
elif user_setting_name.startswith('engine_'):
|
|
|
|
disabled_engines.append(user_setting_name)
|
|
|
|
elif user_setting_name.startswith('category_'):
|
|
|
|
enabled_categories.append(user_setting_name[len('category_'):])
|
|
|
|
elif user_setting_name.startswith('plugin_'):
|
|
|
|
disabled_plugins.append(user_setting_name)
|
2020-02-01 11:01:17 +01:00
|
|
|
elif user_setting_name == 'tokens':
|
|
|
|
self.tokens.parse_form(user_setting)
|
2016-06-26 01:44:42 +02:00
|
|
|
else:
|
|
|
|
self.unknown_params[user_setting_name] = user_setting
|
2016-04-08 16:38:05 +02:00
|
|
|
self.key_value_settings['categories'].parse_form(enabled_categories)
|
|
|
|
self.engines.parse_form(disabled_engines)
|
|
|
|
self.plugins.parse_form(disabled_plugins)
|
|
|
|
|
|
|
|
# cannot be used in case of engines or plugins
|
|
|
|
def get_value(self, user_setting_name):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Returns the value for ``user_setting_name``
|
|
|
|
"""
|
|
|
|
ret_val = None
|
2016-04-08 16:38:05 +02:00
|
|
|
if user_setting_name in self.key_value_settings:
|
2020-02-23 20:52:00 +01:00
|
|
|
ret_val = self.key_value_settings[user_setting_name].get_value()
|
2017-12-05 22:30:20 +01:00
|
|
|
if user_setting_name in self.unknown_params:
|
2020-02-23 20:52:00 +01:00
|
|
|
ret_val = self.unknown_params[user_setting_name]
|
|
|
|
return ret_val
|
2016-04-08 16:38:05 +02:00
|
|
|
|
|
|
|
def save(self, resp):
|
2020-02-23 20:52:00 +01:00
|
|
|
"""Save cookie in the HTTP reponse obect
|
|
|
|
"""
|
2016-11-30 18:43:03 +01:00
|
|
|
for user_setting_name, user_setting in self.key_value_settings.items():
|
2020-10-23 16:22:55 +02:00
|
|
|
if self.key_value_settings[user_setting_name].locked:
|
|
|
|
continue
|
2016-04-08 16:38:05 +02:00
|
|
|
user_setting.save(user_setting_name, resp)
|
|
|
|
self.engines.save(resp)
|
|
|
|
self.plugins.save(resp)
|
2020-02-01 11:01:17 +01:00
|
|
|
self.tokens.save('tokens', resp)
|
2016-06-26 01:44:42 +02:00
|
|
|
for k, v in self.unknown_params.items():
|
|
|
|
resp.set_cookie(k, v, max_age=COOKIE_MAX_AGE)
|
2016-04-08 16:38:05 +02:00
|
|
|
return resp
|
2020-02-01 11:01:17 +01:00
|
|
|
|
2020-02-23 20:52:00 +01:00
|
|
|
def validate_token(self, engine): # pylint: disable=missing-function-docstring
|
2020-02-01 11:01:17 +01:00
|
|
|
valid = True
|
|
|
|
if hasattr(engine, 'tokens') and engine.tokens:
|
|
|
|
valid = False
|
|
|
|
for token in self.tokens.values:
|
|
|
|
if token in engine.tokens:
|
|
|
|
valid = True
|
|
|
|
break
|
|
|
|
|
|
|
|
return valid
|
2020-10-23 16:22:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_locked(setting_name):
|
|
|
|
"""Checks if a given setting name is locked by settings.yml
|
|
|
|
"""
|
|
|
|
if 'preferences' not in settings:
|
|
|
|
return False
|
|
|
|
if 'lock' not in settings['preferences']:
|
|
|
|
return False
|
|
|
|
return setting_name in settings['preferences']['lock']
|