コード例 #1
0
ファイル: webapp.py プロジェクト: asciimoo/searx
def preferences():
    """Render preferences page && save user preferences"""

    # save preferences
    if request.method == 'POST':
        resp = make_response(redirect(urljoin(settings['server']['base_url'], url_for('index'))))
        try:
            request.preferences.parse_form(request.form)
        except ValidationException:
            request.errors.append(gettext('Invalid settings, please edit your preferences'))
            return resp
        return request.preferences.save(resp)

    # render preferences
    image_proxy = request.preferences.get_value('image_proxy')
    lang = request.preferences.get_value('language')
    disabled_engines = request.preferences.engines.get_disabled()
    allowed_plugins = request.preferences.plugins.get_enabled()

    # stats for preferences page
    stats = {}

    for c in categories:
        for e in categories[c]:
            stats[e.name] = {'time': None,
                             'warn_timeout': False,
                             'warn_time': False}
            if e.timeout > settings['outgoing']['request_timeout']:
                stats[e.name]['warn_timeout'] = True
            stats[e.name]['supports_selected_language'] = _is_selected_language_supported(e, request.preferences)

    # get first element [0], the engine time,
    # and then the second element [1] : the time (the first one is the label)
    for engine_stat in get_engines_stats()[0][1]:
        stats[engine_stat.get('name')]['time'] = round(engine_stat.get('avg'), 3)
        if engine_stat.get('avg') > settings['outgoing']['request_timeout']:
            stats[engine_stat.get('name')]['warn_time'] = True
    # end of stats

    return render('preferences.html',
                  locales=settings['locales'],
                  current_locale=get_locale(),
                  image_proxy=image_proxy,
                  engines_by_category=categories,
                  stats=stats,
                  answerers=[{'info': a.self_info(), 'keywords': a.keywords} for a in answerers],
                  disabled_engines=disabled_engines,
                  autocomplete_backends=autocomplete_backends,
                  shortcuts={y: x for x, y in engine_shortcuts.items()},
                  themes=themes,
                  plugins=plugins,
                  doi_resolvers=settings['doi_resolvers'],
                  current_doi_resolver=get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')),
                  allowed_plugins=allowed_plugins,
                  theme=get_current_theme_name(),
                  preferences_url_params=request.preferences.get_as_url_params(),
                  base_url=get_base_url(),
                  preferences=True)
コード例 #2
0
def preferences():
    """Render preferences page && save user preferences"""

    # save preferences
    if request.method == 'POST':
        resp = make_response(redirect(urljoin(settings['server']['base_url'], url_for('index'))))
        try:
            request.preferences.parse_form(request.form)
        except ValidationException:
            request.errors.append(gettext('Invalid settings, please edit your preferences'))
            return resp
        return request.preferences.save(resp)

    # render preferences
    image_proxy = request.preferences.get_value('image_proxy')
    lang = request.preferences.get_value('language')
    disabled_engines = request.preferences.engines.get_disabled()
    allowed_plugins = request.preferences.plugins.get_enabled()

    # stats for preferences page
    stats = {}

    for c in categories:
        for e in categories[c]:
            stats[e.name] = {'time': None,
                             'warn_timeout': False,
                             'warn_time': False}
            if e.timeout > settings['outgoing']['request_timeout']:
                stats[e.name]['warn_timeout'] = True
            stats[e.name]['supports_selected_language'] = _is_selected_language_supported(e, request.preferences)

    # get first element [0], the engine time,
    # and then the second element [1] : the time (the first one is the label)
    for engine_stat in get_engines_stats()[0][1]:
        stats[engine_stat.get('name')]['time'] = round(engine_stat.get('avg'), 3)
        if engine_stat.get('avg') > settings['outgoing']['request_timeout']:
            stats[engine_stat.get('name')]['warn_time'] = True
    # end of stats

    return render('preferences.html',
                  locales=settings['locales'],
                  current_locale=get_locale(),
                  image_proxy=image_proxy,
                  engines_by_category=categories,
                  stats=stats,
                  answerers=[{'info': a.self_info(), 'keywords': a.keywords} for a in answerers],
                  disabled_engines=disabled_engines,
                  autocomplete_backends=autocomplete_backends,
                  shortcuts={y: x for x, y in engine_shortcuts.items()},
                  themes=themes,
                  plugins=plugins,
                  doi_resolvers=settings['doi_resolvers'],
                  current_doi_resolver=get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')),
                  allowed_plugins=allowed_plugins,
                  theme=get_current_theme_name(),
                  preferences_url_params=request.preferences.get_as_url_params(),
                  base_url=get_base_url(),
                  preferences=True)
コード例 #3
0
def preferences():
    """Render preferences page && save user preferences"""

    # save preferences
    if request.method == "POST":
        resp = make_response(
            redirect(urljoin(settings["server"]["base_url"],
                             url_for("index"))))
        try:
            request.preferences.parse_form(request.form)
        except ValidationException:
            request.errors.append(
                gettext("Invalid settings, please edit your preferences"))
            return resp
        return request.preferences.save(resp)

    # render preferences
    image_proxy = request.preferences.get_value("image_proxy")
    disabled_engines = request.preferences.engines.get_disabled()
    allowed_plugins = request.preferences.plugins.get_enabled()

    # stats for preferences page
    stats = {}

    engines_by_category = {}
    for c in categories:
        engines_by_category[c] = []
        for e in categories[c]:
            if not request.preferences.validate_token(e):
                continue

            stats[e.name] = {
                "time": None,
                "warn_timeout": False,
                "warn_time": False
            }
            if e.timeout > settings["outgoing"]["request_timeout"]:
                stats[e.name]["warn_timeout"] = True
            stats[e.name][
                "supports_selected_language"] = _is_selected_language_supported(
                    e, request.preferences)

            engines_by_category[c].append(e)

    # get first element [0], the engine time,
    # and then the second element [1] : the time (the first one is the label)
    for engine_stat in get_engines_stats(request.preferences)[0][1]:
        stats[engine_stat.get("name")]["time"] = round(engine_stat.get("avg"),
                                                       3)
        if engine_stat.get("avg") > settings["outgoing"]["request_timeout"]:
            stats[engine_stat.get("name")]["warn_time"] = True
    # end of stats

    locked_preferences = list()
    if "preferences" in settings and "lock" in settings["preferences"]:
        locked_preferences = settings["preferences"]["lock"]

    return render(
        "preferences.html",
        selected_categories=get_selected_categories(request.preferences,
                                                    request.form),
        all_categories=_get_ordered_categories(),
        locales=settings["locales"],
        current_locale=request.preferences.get_value("locale"),
        image_proxy=image_proxy,
        engines_by_category=engines_by_category,
        stats=stats,
        answerers=[{
            "info": a.self_info(),
            "keywords": a.keywords
        } for a in answerers],
        disabled_engines=disabled_engines,
        autocomplete_backends=autocomplete_backends,
        shortcuts={y: x
                   for x, y in engine_shortcuts.items()},
        themes=themes,
        plugins=plugins,
        doi_resolvers=settings["doi_resolvers"],
        current_doi_resolver=get_doi_resolver(
            request.args, request.preferences.get_value("doi_resolver")),
        allowed_plugins=allowed_plugins,
        theme=get_current_theme_name(),
        preferences_url_params=request.preferences.get_as_url_params(),
        base_url=get_base_url(),
        locked_preferences=locked_preferences,
        preferences=True,
    )
コード例 #4
0
def preferences():
    """Render preferences page && save user preferences"""

    # pylint: disable=too-many-locals, too-many-return-statements, too-many-branches
    # pylint: disable=too-many-statements

    # save preferences
    if request.method == 'POST':
        resp = make_response(redirect(url_for('index', _external=True)))
        try:
            request.preferences.parse_form(request.form)
        except ValidationException:
            request.errors.append(
                gettext('Invalid settings, please edit your preferences'))
            return resp
        return request.preferences.save(resp)

    # render preferences
    image_proxy = request.preferences.get_value('image_proxy')  # pylint: disable=redefined-outer-name
    disabled_engines = request.preferences.engines.get_disabled()
    allowed_plugins = request.preferences.plugins.get_enabled()

    # stats for preferences page
    filtered_engines = dict(
        filter(lambda kv: (kv[0], request.preferences.validate_token(kv[1])),
               engines.items()))

    engines_by_category = {}
    for c in categories:
        engines_by_category[c] = [
            e for e in categories[c] if e.name in filtered_engines
        ]
        # sort the engines alphabetically since the order in settings.yml is meaningless.
        list.sort(engines_by_category[c], key=lambda e: e.name)

    # get first element [0], the engine time,
    # and then the second element [1] : the time (the first one is the label)
    stats = {}  # pylint: disable=redefined-outer-name
    max_rate95 = 0
    for _, e in filtered_engines.items():
        h = histogram('engine', e.name, 'time', 'total')
        median = round(h.percentage(50), 1) if h.count > 0 else None
        rate80 = round(h.percentage(80), 1) if h.count > 0 else None
        rate95 = round(h.percentage(95), 1) if h.count > 0 else None

        max_rate95 = max(max_rate95, rate95 or 0)

        result_count_sum = histogram('engine', e.name, 'result', 'count').sum
        successful_count = counter('engine', e.name, 'search', 'count',
                                   'successful')
        result_count = int(result_count_sum /
                           float(successful_count)) if successful_count else 0

        stats[e.name] = {
            'time':
            median,
            'rate80':
            rate80,
            'rate95':
            rate95,
            'warn_timeout':
            e.timeout > settings['outgoing']['request_timeout'],
            'supports_selected_language':
            _is_selected_language_supported(e, request.preferences),
            'result_count':
            result_count,
        }
    # end of stats

    # reliabilities
    reliabilities = {}
    engine_errors = get_engine_errors(filtered_engines)
    checker_results = checker_get_result()
    checker_results = checker_results['engines'] \
        if checker_results['status'] == 'ok' and 'engines' in checker_results else {}
    for _, e in filtered_engines.items():
        checker_result = checker_results.get(e.name, {})
        checker_success = checker_result.get('success', True)
        errors = engine_errors.get(e.name) or []
        if counter('engine', e.name, 'search', 'count', 'sent') == 0:
            # no request
            reliablity = None
        elif checker_success and not errors:
            reliablity = 100
        elif 'simple' in checker_result.get('errors', {}):
            # the basic (simple) test doesn't work: the engine is broken accoding to the checker
            # even if there is no exception
            reliablity = 0
        else:
            reliablity = 100 - sum([
                error['percentage']
                for error in errors if not error.get('secondary')
            ])

        reliabilities[e.name] = {
            'reliablity': reliablity,
            'errors': [],
            'checker': checker_results.get(e.name, {}).get('errors',
                                                           {}).keys(),
        }
        # keep the order of the list checker_results[e.name]['errors'] and deduplicate.
        # the first element has the highest percentage rate.
        reliabilities_errors = []
        for error in errors:
            error_user_text = None
            if error.get('secondary') or 'exception_classname' not in error:
                continue
            error_user_text = exception_classname_to_text.get(
                error.get('exception_classname'))
            if not error:
                error_user_text = exception_classname_to_text[None]
            if error_user_text not in reliabilities_errors:
                reliabilities_errors.append(error_user_text)
        reliabilities[e.name]['errors'] = reliabilities_errors

    # supports
    supports = {}
    for _, e in filtered_engines.items():
        supports_selected_language = _is_selected_language_supported(
            e, request.preferences)
        safesearch = e.safesearch
        time_range_support = e.time_range_support
        for checker_test_name in checker_results.get(e.name,
                                                     {}).get('errors', {}):
            if supports_selected_language and checker_test_name.startswith(
                    'lang_'):
                supports_selected_language = '?'
            elif safesearch and checker_test_name == 'safesearch':
                safesearch = '?'
            elif time_range_support and checker_test_name == 'time_range':
                time_range_support = '?'
        supports[e.name] = {
            'supports_selected_language': supports_selected_language,
            'safesearch': safesearch,
            'time_range_support': time_range_support,
        }

    return render(
        'preferences.html',
        selected_categories=get_selected_categories(request.preferences,
                                                    request.form),
        locales=settings['locales'],
        current_locale=request.preferences.get_value("locale"),
        image_proxy=image_proxy,
        engines_by_category=engines_by_category,
        stats=stats,
        max_rate95=max_rate95,
        reliabilities=reliabilities,
        supports=supports,
        answerers=[{
            'info': a.self_info(),
            'keywords': a.keywords
        } for a in answerers],
        disabled_engines=disabled_engines,
        autocomplete_backends=autocomplete_backends,
        shortcuts={y: x
                   for x, y in engine_shortcuts.items()},
        themes=themes,
        plugins=plugins,
        doi_resolvers=settings['doi_resolvers'],
        current_doi_resolver=get_doi_resolver(
            request.args, request.preferences.get_value('doi_resolver')),
        allowed_plugins=allowed_plugins,
        theme=get_current_theme_name(),
        preferences_url_params=request.preferences.get_as_url_params(),
        locked_preferences=settings['preferences']['lock'],
        preferences=True)