Exemple #1
0
def product_search(request):

    # variable setups
    current_user = User.objects.get(username=request.user)
    form = ProductSearchForm

    # get top 5 searches and make them into a list
    search_aggregate = Search.objects.annotate(search_term=Count("query")).order_by("search_term")[:5]
    pop_searches = []

    for i in search_aggregate:
        pop_searches.append(i.query)

    # get user wish lists
    wish_lists = current_user.wishlist_set.all()

    # get search query, add to database and redirect
    if request.method == "GET":
        form = ProductSearchForm(request.GET)
        if form.is_valid():

            this_search = form.cleaned_data["search"]

            current_search = Search(query=this_search, user=current_user)
            current_search.save()

            return HttpResponseRedirect("/shopping/results/" + this_search)

    # get the 5 most popular searches and put them in a list for the popular searches category

    CONTEXT = {"form": form, "pop_searches": pop_searches, "wishlists": wish_lists}
    return render(request, "shopping/shopping_search.html", CONTEXT)
Exemple #2
0
def product_results(
    request, current_search, template="shopping/shopping_results.html", page_template="shopping/products.html"
):

    # variable setups
    products = Products(
        api_key="SEM34D405CE2A6F4715E79457D08A21B4CEE", api_secret="NmI0ZDkwZmJhMGVkNGU1NWI5Y2ZmYWNkMjgzNzUyZDg"
    )
    current_user = User.objects.get(username=request.user)
    form = ProductSearchForm

    # get user's last search and feed to Semantics3, render results
    query = products.products_field("name", current_search)
    products.get_products()

    # iterate over products, add to results list (position 0 in results)
    results = []

    for i in products.iter():
        results.append(i)

        if len(results) == 10:
            break

    # get lowest prices for each item, add to separate list (position 1 in results)
    lowest_prices = []

    for i in results:
        prices = []
        for store in i["sitedetails"]:
            prices.append("%.02f" % float(store["latestoffers"][0]["price"]))

        prices = sorted(prices)
        lowest_prices.append(prices[0])

    results_master = zip(results, lowest_prices)

    # Django Endless Pagination (Twitter Style Reload)
    if request.is_ajax():
        request_page = utils.get_page_number_from_request(request)
        offset = (request_page - 1) * 10

        products.products_field("offset", offset)
        products.get_products()

        # iterate over products, apply an offset based on page number, add to results list (position 0 in results_master)
        results = []

        for i in products.iter():
            results.append(i)

            if len(results) == 10 + offset:
                break

        # get lowest prices for each item, add to separate list (position 1 in results_master)
        lowest_prices = []

        for i in results:
            prices = []
            for store in i["sitedetails"]:
                prices.append("%.02f" % float(store["latestoffers"][0]["price"]))

            prices = sorted(prices)
            lowest_prices.append(prices[0])

        results_master = zip(results, lowest_prices)
        template = page_template

    # get search query, add to database and session, redirect
    if request.method == "GET":
        form = ProductSearchForm(request.GET)
        if form.is_valid():

            this_search = form.cleaned_data["search"]

            current_search = Search(query=this_search, user=current_user)
            current_search.save()

            return HttpResponseRedirect("/shopping/results/" + this_search)

    if request.method == "POST":
        return HttpResponseRedirect("/overview/")

    CONTEXT = {
        "results": results_master,
        "search": current_search,
        "form": form,
        "page_template": page_template,
        "lowest_prices": lowest_prices,
    }
    return render(request, template, CONTEXT)