def allProduct(request):
    user=request.user
    if not user.is_anonymous():
        ########navigation category
        category_list=UserView.getCategory()
        ####Code for showing all the products
        product_list=Products.objects.all()
        totalProducts=[]
        for each in product_list:
            if each.product_id not in totalProducts:
                totalProducts.append((each.product_id,each.baseUnitPrice))
        Dict={
            'user':request.user,
            'product_list':totalProducts,
            'category_list':category_list
            }
        return render_to_response('customer/customer.html',Dict,context_instance=RequestContext(request))
    else:
        category_list=UserView.getCategory()
        ####Code for showing all the products
        product_list=Products.objects.all()
        totalProducts=[]
        for each in product_list:
            if each.product_id not in totalProducts:
                totalProducts.append((each.product_id,each.baseUnitPrice))
        Dict={
            'user':request.user,
            'product_list':totalProducts,
            'category_list':category_list
            }
        #return HttpResponse('non login page is to be shown')
        return render_to_response('siteown/NonLoginProduct.html',Dict,context_instance=RequestContext(request))
def productView(request,product_id):
    user=request.user
    if not user.is_anonymous():
        category_list=UserView.getCategory()
        ##Getting Product for the specific product ID
        product=Products.objects.get(product_id=product_id)
        
        #Get the product_count
        inventory_list=Inventory.objects.filter(product_id=product_id)
        inventory_data=[]
        for each in inventory_list:
            if (each.sizeVersion,each.stock_quantity) not in inventory_data:
                inventory_data.append((each.sizeVersion,each.stock_quantity))
        print inventory_data
        Dict={
              'user':user,
              'category_list':category_list,
              'product_id':product.product_id,
              'price':product.baseUnitPrice,
              'rating':product.rating,
              'company':product.brand,
              'description':product.short_description,
              'inventory_data':inventory_data
            }
        return render_to_response('customer/productView.html',Dict,context_instance=RequestContext(request))
    else:
        category_list=UserView.getCategory()
        ##Getting Product for the specific product ID
        product=Products.objects.get(product_id=product_id)
        
        #Get the product_count
        inventory_list=Inventory.objects.filter(product_id=product_id)
        inventory_data=[]
        for each in inventory_list:
            if (each.sizeVersion,each.stock_quantity) not in inventory_data:
                inventory_data.append((each.sizeVersion,each.stock_quantity))
        print inventory_data
        Dict={
              'user':user,
              'category_list':category_list,
              'product_id':product.product_id,
              'price':product.baseUnitPrice,
              'rating':product.rating,
              'company':product.brand,
              'description':product.short_description,
              'inventory_data':inventory_data
            }
        return render_to_response('siteown/NonLoginProductView.html',Dict,context_instance=RequestContext(request))
def subcategory3Search(request,category,subcategory_1,subcategory_2,subcategory_3):
    user=request.user
    category=category.strip()
    category_list=UserView.getCategory()
    ######Get Category ID
    category_id=Category.objects.filter(category_name=category,subcategory_1=subcategory_1,subcategory_2=subcategory_2,subcategory_3=subcategory_3)
    category_value=[]
    if len(category_id) > 0:
        for each in category_id:
            if each.id not in category_value:
                category_value.append(each.id)
    else:
        category_value=[]
    print category_value
    totalProducts=[]
    for id in category_value:        
        product_list=Products.objects.filter(category_id=id)
        for each in product_list:
            if each.product_id not in totalProducts:
                totalProducts.append((each.product_id,each.baseUnitPrice))
    Dict={
            'user':request.user,
            'product_list':totalProducts,
            'category_list':category_list
            }
    if not user.is_anonymous():        
        return render_to_response('customer/customer.html',Dict,context_instance=RequestContext(request))
    else:
        Dict={
            'product_list':totalProducts,
            'category_list':category_list
            }
        return render_to_response('siteown/NonLoginProduct.html',Dict,context_instance=RequestContext(request))
def seeCart(request,user_name):
    user=request.user
    if not user.is_anonymous():
        print request.session
        if 'cart' in request.session.keys():
            category_list=UserView.getCategory()
            ###########Get ALL the cart items
            total_data=[] 
            for eachitem in request.session['cart']:
                temp=[]
                temp.append(eachitem['product_id'])
                temp.append(eachitem['name'])
                temp.append(eachitem['size_version'])
                temp.append(eachitem['quantity'])
                temp.append(eachitem['unit_price'])
                temp.append(str(int(eachitem['quantity'])*Decimal(eachitem['unit_price'])))
                temp=tuple(temp)
                total_data.append(temp)
            #previous_url=request.META['HTTP_REFERER']
            Dict={
                  'category_list':category_list,
                  'product_list':total_data,
             #     'previous_url':previous_url
                  }
            return render_to_response("customer/CustomerCart.html",Dict,context_instance=RequestContext(request))
        else:
            return HttpResponse("No object is selected")
def productReview(request,product_id):
    category_list=UserView.getCategory()
    review_list=[]
    temp_review=Review.objects.filter(product_id=product_id).order_by('-commented_on')
    for each in temp_review:
        if each not in review_list:
            review_list.append((each.user_name,each.user_comment,(each.commented_on).date(),(each.commented_on).time()))
    Dict={
          'category_list':category_list,
          'product_id':product_id,
          'review':review_list
          }
    return render_to_response('customer/productReview.html',Dict,context_instance=RequestContext(request))
def home(request):
    user=request.user
    #GET FIVE PRODUCTS
    if not user.is_anonymous():
        print user
        if user.is_staff:
            return HttpResponseRedirect(reverse('users.views.admin'))
        else:
            return HttpResponseRedirect(reverse('users.views.customer'))
    else:
        category_list=UserView.getCategory()
        products=Products.objects.all().order_by('-arrival')
    list=[]
    for each in range(1,5):
        if (products[each].product_name,products[each].product_id) not in list:
            list.append((products[each].product_name,products[each].product_id))
    
    Dict={
         'category_list':category_list,
         'top_list':list
    }
    return render_to_response('siteown/home.html',Dict,context_instance=RequestContext(request))