Beispiel #1
0
def _search_json(query, tree, query_text, offset, limit, config):
    """Try a "direct search" (for exact identifier matches, etc.). If we have a direct hit,
    then return {redirect: hit location}.If that doesn't work, fall back to a normal search
    and return the results as JSON."""

    # If we're asked to redirect and have a direct hit, then return the url to that.
    if request.values.get('redirect') == 'true':
        result = query.direct_result()
        if result:
            path, line = result
            # TODO: Does this escape query_text properly?
            params = {
                'tree': tree,
                'path': path,
                'from': query_text
            }
            return jsonify({'redirect': url_for('.browse', _anchor=line, **params)})
    try:
        count_and_results = query.results(offset, limit)
        # Convert to dicts for ease of manipulation in JS:
        results = [{'icon': icon,
                    'path': path,
                    'lines': [{'line_number': nb, 'line': l} for nb, l in lines]}
                   for icon, path, lines in count_and_results['results']]
    except BadTerm as exc:
        return jsonify({'error_html': exc.reason, 'error_level': 'warning'}), 400

    return jsonify({
        'www_root': config.www_root,
        'tree': tree,
        'results': results,
        'result_count': count_and_results['result_count'],
        'result_count_formatted': format_number(count_and_results['result_count']),
        'tree_tuples': _tree_tuples(query_text)})
Beispiel #2
0
def _search_json(query, tree, query_text, offset, limit, config):
    """Try a "direct search" (for exact identifier matches, etc.). If we have a direct hit,
    then return {redirect: hit location}. If that doesn't work, fall back to a normal
    search, and if that yields a single result and redirect is true then return
    {redirect: hit location}, otherwise return the results as JSON.

    'redirect=true' along with 'redirect_type={direct, single}' control the behavior
    of jumping to results:
        * 'redirect_type=direct' indicates a direct_result result and comes with
          a bubble giving the option to switch to all results instead.
        * 'redirect_type=single' indicates a unique search result and comes with
          a bubble indicating as much.
    We only redirect to a direct/unique result if the original query contained a
    'redirect=true' parameter, which the user can elicit by hitting enter on the query
    input."""

    # If we're asked to redirect and have a direct hit, then return the url to that.
    if request.values.get('redirect') == 'true':
        result = query.direct_result()
        if result:
            path, line = result
            # TODO: Does this escape query_text properly?
            params = {
                'tree': tree,
                'path': path,
                'q': query_text,
                'redirect_type': 'direct'
            }
            return jsonify({'redirect': url_for('.browse', _anchor=line, **params)})
    try:
        count_and_results = query.results(offset, limit)
        # If we're asked to redirect and there's a single result, redirect to the result.
        if (request.values.get('redirect') == 'true' and
            count_and_results['result_count'] == 1):
            _, path, line = next(count_and_results['results'])
            line = line[0][0] if line else None
            params = {
                'tree': tree,
                'path': path,
                'q': query_text,
                'redirect_type': 'single'
            }
            return jsonify({'redirect': url_for('.browse', _anchor=line, **params)})
        # Convert to dicts for ease of manipulation in JS:
        results = [{'icon': icon,
                    'path': file_path,
                    'lines': [{'line_number': nb, 'line': l} for nb, l in lines]}
                   for icon, file_path, lines in count_and_results['results']]
    except BadTerm as exc:
        return jsonify({'error_html': exc.reason, 'error_level': 'warning'}), 400

    return jsonify({
        'www_root': config.www_root,
        'tree': tree,
        'results': results,
        'result_count': count_and_results['result_count'],
        'result_count_formatted': format_number(count_and_results['result_count']),
        'tree_tuples': _tree_tuples('.search', q=query_text)})
Beispiel #3
0
def _search_json(query, tree, query_text, is_case_sensitive, offset, limit,
                 config):
    """Try a "direct search" (for exact identifier matches, etc.). If we have a direct hit,
    then return {redirect: hit location}.If that doesn't work, fall back to a normal search
    and return the results as JSON."""

    # If we're asked to redirect and have a direct hit, then return the url to that.
    if request.values.get('redirect') == 'true':
        result = query.direct_result()
        if result:
            path, line = result
            # TODO: Does this escape query_text properly?
            params = {'tree': tree, 'path': path, 'from': query_text}
            if is_case_sensitive:
                params['case'] = 'true'
            return jsonify(
                {'redirect': url_for('.browse', _anchor=line, **params)})
    try:
        count_and_results = query.results(offset, limit)
        # Convert to dicts for ease of manipulation in JS:
        results = [{
            'icon': icon,
            'path': path,
            'lines': [{
                'line_number': nb,
                'line': l
            } for nb, l in lines],
            'is_binary': is_binary
        } for icon, path, lines, is_binary in count_and_results['results']]
    except BadTerm as exc:
        return jsonify({
            'error_html': exc.reason,
            'error_level': 'warning'
        }), 400

    return jsonify({
        'www_root':
        config.www_root,
        'tree':
        tree,
        'results':
        results,
        'result_count':
        count_and_results['result_count'],
        'result_count_formatted':
        format_number(count_and_results['result_count']),
        'tree_tuples':
        _tree_tuples(query_text, is_case_sensitive)
    })