Example #1
0
def search(request):
    # Inputs
    page = int(request.GET.get('page', 1))
    query = request.GET.get('query')
    max_results = request.GET.get('max_results', 50)

    # Outputs
    query_error = None
    possible_matches = {}
    hit_max = {}

    if query is None:
        query_error = _("Error: query not specified.")
        matches = []
        pages = 0

#    elif len(query) < 3:
#        query_error = _("Error: query too short.")

    else:
        query = query.lower()
        # search for topic, video or exercise with matching title

        matches, exact, pages = search_topic_nodes(query=query, language=request.language, page=page, items_per_page=max_results)

        if exact:
            # Redirect to an exact match
            return HttpResponseRedirect(reverse('learn') + matches[0]['path'])

    # Subdivide into categories.

    possible_matches = dict([(category, filter(lambda x: x.get("kind") == category, matches)) for category in set([x.get("kind") for x in matches])])

    previous_params = request.GET.copy()
    previous_params['page'] = page - 1

    previous_url = "?" + previous_params.urlencode()

    next_params = request.GET.copy()
    next_params['page'] = page + 1

    next_url = "?" + next_params.urlencode()

    return {
        'title': _("Search results for '%(query)s'") % {"query": (query if query else "")},
        'query_error': query_error,
        'results': possible_matches,
        'hit_max': hit_max,
        'more': pages > page,
        'page': page,
        'previous_url': previous_url,
        'next_url': next_url,
        'query': query,
        'max_results': max_results,
    }
Example #2
0
def search(request):
    # Inputs
    page = int(request.GET.get('page', 1))
    query = request.GET.get('query')
    max_results = request.GET.get('max_results', 50)

    # Outputs
    query_error = None
    possible_matches = {}
    hit_max = {}

    if query is None:
        query_error = _("Error: query not specified.")
        matches = []
        pages = 0

#    elif len(query) < 3:
#        query_error = _("Error: query too short.")

    else:
        query = query.lower()
        # search for topic, video or exercise with matching title

        matches, exact, pages = search_topic_nodes(query=query, language=request.language, page=page, items_per_page=max_results)

        if exact:
            # Redirect to an exact match
            return HttpResponseRedirect(reverse('learn') + matches[0]['path'])

    # Subdivide into categories.

    possible_matches = dict([(category, filter(lambda x: x.get("kind") == category, matches)) for category in set([x.get("kind") for x in matches])])

    previous_params = request.GET.copy()
    previous_params['page'] = page - 1

    previous_url = "?" + previous_params.urlencode()

    next_params = request.GET.copy()
    next_params['page'] = page + 1

    next_url = "?" + next_params.urlencode()

    return {
        'title': _("Search results for '%(query)s'") % {"query": (query if query else "")},
        'query_error': query_error,
        'results': possible_matches,
        'hit_max': hit_max,
        'more': pages > page,
        'page': page,
        'previous_url': previous_url,
        'next_url': next_url,
        'query': query,
        'max_results': max_results,
    }
Example #3
0
def search_api(request, channel):
    query = request.GET.get("term")
    if query is None:
        return JsonResponseMessageError("No search term specified", status=404)

    query = query.lower()
    # search for topic, video or exercise with matching title

    matches, exact, pages = search_topic_nodes(query=query, channel=channel, language=request.language, page=1, items_per_page=15, exact=False)

    if not matches:
        messages.warning(request, _("Search completed, no content was found for your search. Try something else."))

    return JsonResponse(matches)
Example #4
0
def search_api(request, channel):
    query = request.GET.get("term")
    if query is None:
        return JsonResponseMessageError("No search term specified", status=404)

    query = query.lower()
    # search for topic, video or exercise with matching title

    matches, exact, pages = search_topic_nodes(query=query,
                                               channel=channel,
                                               language=request.language,
                                               page=1,
                                               items_per_page=15,
                                               exact=False)

    if not matches:
        messages.warning(
            request,
            _("Search completed, no content was found for your search. Try something else."
              ))

    return JsonResponse(matches)