From 1de781a143270c8f1f98a04b688b8546ad341ec5 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Tue, 3 Feb 2015 18:37:38 +0100 Subject: [PATCH] [enh] category specific engine deactivation - closes #205 --- searx/query.py | 2 +- searx/search.py | 6 ++--- searx/templates/courgette/preferences.html | 2 +- searx/templates/default/preferences.html | 2 +- searx/templates/oscar/preferences.html | 2 +- searx/utils.py | 20 +++++++++++++--- searx/webapp.py | 27 +++++++++------------- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/searx/query.py b/searx/query.py index 567c49e92..e79e760a3 100644 --- a/searx/query.py +++ b/searx/query.py @@ -107,7 +107,7 @@ class Query(object): self.engines.extend({'category': prefix, 'name': engine.name} for engine in categories[prefix] - if engine not in self.blocked_engines) + if (engine.name, prefix) not in self.blocked_engines) if query_part[0] == '!': self.specific = True diff --git a/searx/search.py b/searx/search.py index c16346f45..1b16ee02f 100644 --- a/searx/search.py +++ b/searx/search.py @@ -411,9 +411,9 @@ class Search(object): # declared under the specific categories for categ in self.categories: self.engines.extend({'category': categ, - 'name': x.name} - for x in categories[categ] - if x.name not in self.blocked_engines) + 'name': engine.name} + for engine in categories[categ] + if (engine.name, categ) not in self.blocked_engines) # do search-request def search(self, request): diff --git a/searx/templates/courgette/preferences.html b/searx/templates/courgette/preferences.html index 64da3fd1c..198bc7ead 100644 --- a/searx/templates/courgette/preferences.html +++ b/searx/templates/courgette/preferences.html @@ -99,7 +99,7 @@ {{ search_engine.name }} ({{ shortcuts[search_engine.name] }}) {{ _(categ) }} - + diff --git a/searx/templates/default/preferences.html b/searx/templates/default/preferences.html index 1fa8d88b7..112849c3d 100644 --- a/searx/templates/default/preferences.html +++ b/searx/templates/default/preferences.html @@ -87,7 +87,7 @@ {{ search_engine.name }} ({{ shortcuts[search_engine.name] }}) {{ _(categ) }} - + diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index f6f14501e..41656463c 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -128,7 +128,7 @@
{{ search_engine.name }} ({{ shortcuts[search_engine.name] }})
- +
diff --git a/searx/utils.py b/searx/utils.py index c0afc94cb..a42458829 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -231,7 +231,21 @@ def prettify_url(url): def get_blocked_engines(engines, cookies): if 'blocked_engines' not in cookies: - return [engine for engine in engines if engines[engine].disabled] + return [(engine, category) for engine in engines for category in engines.category if engines[engine].disabled] - return [engine for engine in cookies.get('blocked_engines', '').split(',') - if engine in engines] + blocked_engine_strings = cookies.get('blocked_engines', '').split(',') + blocked_engines = [] + + if not blocked_engine_strings: + return blocked_engines + + for engine_string in blocked_engine_strings: + if engine_string.find('__') > -1: + engine, category = engine_string.split('__', 1) + if engine in engines and category in engines[engine].categories: + blocked_engines.append((engine, category)) + elif engine_string in engines: + for category in engines[engine_string].categories: + blocked_engines.append((engine_string, category)) + + return blocked_engines diff --git a/searx/webapp.py b/searx/webapp.py index d92302b69..79af5c905 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -28,7 +28,6 @@ import os import hashlib from datetime import datetime, timedelta -from itertools import chain from urllib import urlencode from flask import ( Flask, request, render_template, url_for, Response, make_response, @@ -234,11 +233,9 @@ def render(template_name, override_theme=None, **kwargs): if autocomplete not in autocomplete_backends: autocomplete = None - nonblocked_categories = (engines[e].categories - for e in engines - if e not in blocked_engines) - - nonblocked_categories = set(chain.from_iterable(nonblocked_categories)) + nonblocked_categories = set(category for engine_name in engines + for category in engines[engine_name].categories + if (engine_name, category) not in blocked_engines) if 'categories' not in kwargs: kwargs['categories'] = ['general'] @@ -492,21 +489,19 @@ def preferences(): elif pd_name == 'method': method = pd elif pd_name.startswith('engine_'): - engine_name = pd_name.replace('engine_', '', 1) - if engine_name in engines: - blocked_engines.append(engine_name) + if pd_name.find('__') > -1: + engine_name, category = pd_name.replace('engine_', '', 1).split('__', 1) + if engine_name in engines and category in engines[engine_name].categories: + blocked_engines.append((engine_name, category)) elif pd_name == 'theme': theme = pd if pd in themes else default_theme else: resp.set_cookie(pd_name, pd, max_age=cookie_max_age) - user_blocked_engines = request.cookies.get('blocked_engines', '').split(',') # noqa - - if sorted(blocked_engines) != sorted(user_blocked_engines): - resp.set_cookie( - 'blocked_engines', ','.join(blocked_engines), - max_age=cookie_max_age - ) + resp.set_cookie( + 'blocked_engines', ','.join('__'.join(e) for e in blocked_engines), + max_age=cookie_max_age + ) if locale: resp.set_cookie(