예제 #1
0
def search(request, template_name="lfs/search/search_results.html"):
    """Returns the search result according to given query (via get request)
    ordered by the globally set sorting.
    """
    q = request.GET.get("q", "")

    # Products
    # TODO add a view in manage to select the attributs of Product modele : manufacturer__name, sku_manufacturer, ...
    query = Q(active=True) & get_query(q, ['name', 'description'])
    products = Product.objects.filter(query)
    if not products:
	    # Creates a SearchNotFound for the current session and/or user.
        if request.session.session_key is None:
            request.session.save()
        searchnotfound = SearchNotFound(session=request.session.session_key)
        if request.user.is_authenticated():
        	searchnotfound.user = request.user
	    searchnotfound.query = q
        searchnotfound.save()

    # Sorting
    sorting = request.session.get("sorting")
    if sorting:
        products = products.order_by(sorting)

    total = products.count()

    return render_to_response(template_name, RequestContext(request, {
        "products": products,
        "q": q,
        "total": total,
    }))
예제 #2
0
def livesearch(request, template_name="lfs/search/livesearch_results.html"):
    """
    """
    q = request.GET.get("q", "")

    if q == "":
        result = json.dumps({
            "state": "failure",
        })
    else:
        # Products
        query = Q(active=True) & get_query(q, ['name', 'description'])
	temp = Product.objects.filter(query)
        if not temp:
	        # Creates a SearchNotFound for the current session and/or user.
            if request.session.session_key is None:
                request.session.save()
            searchnotfound = SearchNotFound(session=request.session.session_key)
            if request.user.is_authenticated():
        	    searchnotfound.user = request.user
		    searchnotfound.query = q[0:99]
            searchnotfound.save()

        total = temp.count()
        products = temp[0:5]

        products = render_to_string(template_name, RequestContext(request, {
            "products": products,
            "q": q,
            "total": total,
        }))

        result = json.dumps({
            "state": "success",
            "products": products,
        })
    return HttpResponse(result, content_type='application/json')