Esempio n. 1
0
def product_results(request, current_search):
    
    #variable setups
    products = Products(
        api_key = "SEM34D405CE2A6F4715E79457D08A21B4CEE",
        api_secret = "NmI0ZDkwZmJhMGVkNGU1NWI5Y2ZmYWNkMjgzNzUyZDg"
    )
    current_user = User.objects.get(username=request.user)
    
    form = ProductSearchForm(initial={'Search': current_search})
        
    #get user's last search and feed to Semantics3, render results    
    query = products.products_field("name", current_search)
    response = products.get_products()
        
    results = response["results"]
    
    #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']
            
            request.session["search"] = this_search
            
            current_search = Search(query=this_search, user=current_user.id)
            current_search.save()
            
            return HttpResponseRedirect('/shopping/results/'+this_search)
    
    CONTEXT = {'results': results, 'search': current_search, 'form': form}
    return render(request, 'product_search/shopping_results.html', CONTEXT)
Esempio n. 2
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 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.id)
            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}
    return render(request, 'product_search/shopping_search.html', CONTEXT)