summaryrefslogtreecommitdiff
path: root/searx/preferences.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2023-01-30 18:40:22 +0100
committerMarkus Heiser <markus.heiser@darmarit.de>2023-04-15 21:41:57 +0200
commitc03b0ea650e0a052e422881c6278a5c9a7cca60c (patch)
treef79c7d8c3ad6992c9361c53f8507ec7dad2a30c3 /searx/preferences.py
parent5a3f99ee1a89f8a858922f50d87e20a9aeedf051 (diff)
downloadsearxng-c03b0ea650e0a052e422881c6278a5c9a7cca60c.tar.gz
searxng-c03b0ea650e0a052e422881c6278a5c9a7cca60c.zip
[mod] add a Preferences.client property to store client prefs
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/preferences.py')
-rw-r--r--searx/preferences.py61
1 files changed, 59 insertions, 2 deletions
diff --git a/searx/preferences.py b/searx/preferences.py
index 3da6d5d16..8552305a7 100644
--- a/searx/preferences.py
+++ b/searx/preferences.py
@@ -8,9 +8,10 @@
from base64 import urlsafe_b64encode, urlsafe_b64decode
from zlib import compress, decompress
from urllib.parse import parse_qs, urlencode
-from typing import Iterable, Dict, List
+from typing import Iterable, Dict, List, Optional
import flask
+import babel
from searx import settings, autocomplete
from searx.enginelib import Engine
@@ -287,10 +288,65 @@ class PluginsSetting(BooleanChoices):
return [item[len('plugin_') :] for item in items]
+class ClientPref:
+ """Container to assemble client prefferences and settings."""
+
+ # hint: searx.webapp.get_client_settings should be moved into this class
+
+ locale: babel.Locale
+ """Locale prefered by the client."""
+
+ def __init__(self, locale: Optional[babel.Locale] = None):
+ self.locale = locale
+
+ @property
+ def locale_tag(self):
+ if self.locale is None:
+ return None
+ tag = self.locale.language
+ if self.locale.territory:
+ tag += '-' + self.locale.territory
+ return tag
+
+ @classmethod
+ def from_http_request(cls, http_request: flask.Request):
+ """Build ClientPref object from HTTP request.
+
+ - `Accept-Language used for locale setting
+ <https://www.w3.org/International/questions/qa-accept-lang-locales.en>`__
+
+ """
+ al_header = http_request.headers.get("Accept-Language")
+ if not al_header:
+ return cls(locale=None)
+
+ pairs = []
+ for l in al_header.split(','):
+ # fmt: off
+ lang, qvalue = [_.strip() for _ in (l.split(';') + ['q=1',])[:2]]
+ # fmt: on
+ try:
+ qvalue = float(qvalue.split('=')[-1])
+ locale = babel.Locale.parse(lang, sep='-')
+ except (ValueError, babel.core.UnknownLocaleError):
+ continue
+ pairs.append((locale, qvalue))
+ pairs.sort(reverse=True, key=lambda x: x[1])
+ return cls(locale=pairs[0][0])
+
+
class Preferences:
"""Validates and saves preferences to cookies"""
- def __init__(self, themes: List[str], categories: List[str], engines: Dict[str, Engine], plugins: Iterable[Plugin]):
+ def __init__(
+ self,
+ themes: List[str],
+ categories: List[str],
+ engines: Dict[str, Engine],
+ plugins: Iterable[Plugin],
+ client: Optional[ClientPref] = None,
+ ):
+
super().__init__()
self.key_value_settings: Dict[str, Setting] = {
@@ -414,6 +470,7 @@ class Preferences:
self.engines = EnginesSetting('engines', engines=engines.values())
self.plugins = PluginsSetting('plugins', plugins=plugins)
self.tokens = SetSetting('tokens')
+ self.client = client or ClientPref()
self.unknown_params: Dict[str, str] = {}
def get_as_url_params(self):