logo

searx

Unnamed repository; edit this file 'description' to name the repository.
commit: 1de781a143270c8f1f98a04b688b8546ad341ec5
parent: 268e0516eee10f97891aa8a136fd910460f71660
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Tue,  3 Feb 2015 18:37:38 +0100

[enh] category specific engine deactivation - closes #205

Diffstat:

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 @@ -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 @@ -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 @@ -99,7 +99,7 @@ <td>{{ search_engine.name }} ({{ shortcuts[search_engine.name] }})</td> <td>{{ _(categ) }}</td> <td class="engine_checkbox"> - <input type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}"{% if search_engine.name in blocked_engines %} checked="checked"{% endif %} /> + <input type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}__{{ categ }}"{% if (search_engine.name, categ) in blocked_engines %} checked="checked"{% endif %} /> <label class="allow" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Allow') }}</label> <label class="deny" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Block') }}</label> </td> diff --git a/searx/templates/default/preferences.html b/searx/templates/default/preferences.html @@ -87,7 +87,7 @@ <td>{{ search_engine.name }} ({{ shortcuts[search_engine.name] }})</td> <td>{{ _(categ) }}</td> <td class="engine_checkbox"> - <input type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}"{% if search_engine.name in blocked_engines %} checked="checked"{% endif %} /> + <input type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}__{{ categ }}"{% if (search_engine.name, categ) in blocked_engines %} checked="checked"{% endif %} /> <label class="allow" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Allow') }}</label> <label class="deny" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Block') }}</label> </td> diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html @@ -128,7 +128,7 @@ <div class="col-xs-6 col-sm-4 col-md-4">{{ search_engine.name }} ({{ shortcuts[search_engine.name] }})</div> <div class="col-xs-6 col-sm-4 col-md-4"> <div class="checkbox"> - <input class="hidden" type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}"{% if search_engine.name in blocked_engines %} checked="checked"{% endif %} /> + <input class="hidden" type="checkbox" id="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}" name="engine_{{ search_engine.name }}__{{ categ }}"{% if (search_engine.name, categ) in blocked_engines %} checked="checked"{% endif %} /> <label class="btn btn-success label_hide_if_checked" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Block') }}</label> <label class="btn btn-danger label_hide_if_not_checked" for="engine_{{ categ|replace(' ', '_') }}_{{ search_engine.name|replace(' ', '_') }}">{{ _('Allow') }}</label> </div> diff --git 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 @@ -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(