Example #1
0
def show_product(request, product_slug):
    product = get_object_or_404(Product, slug=product_slug)
    #categories=product.categories.filter(is_active=True)
    categories = Category.objects.all()
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
        # if test cookie worked, get rid of it
        if request.session.test_cookie_worked():
            request.session.delete_test_cookie()
        url = urlresolvers.reverse('show_cart')

        return HttpResponseRedirect(url)
    # it is a GET, create the unbound form. Note request as a kwarg
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    context = {'categories': categories,'product': product, 'form':form}
    return render(request, 'product.html', context)
Example #2
0
def show_product(request, product_slug):
    p = get_object_or_404(Product, slug = product_slug)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    active_categories = Category.objects.filter(is_active=True)

    if request.method == 'POST':
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        # if form data valid
        if form.is_valid():
            # add product to cart and redirect
            cart.add_to_cart(request)
            # if test cookie worked get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')
        form.fields['product_slug'].widget.attrs['value'] = product_slug
        request.session.set_test_cookie()
        return render(request, 'catalog/product.html', locals())
Example #3
0
def show_product(request, product_slug, template_name='catalog/product.html'):
	p = get_object_or_404(Product, slug=product_slug)
	categories = p.categories.filter(is_active=True)
	page_title = p.name
	meta_keywords = p.meta_keywords
	meta_description = p.meta_description

	# evaluate the HTTP method
	if request.method == 'POST':
		postdata = request.POST.copy()
		form = ProductAddToCartForm(request, postdata)
		# cart_item = CartItem()
		# my_form = ProductAddToCartForm(request, request.POST, instance=cart_item)
		# check if posted data is valid 
		if form.is_valid():
			# add to cart and redirect to cart page
			cart.add_to_cart(request)

			# if test cookie worked, get rid of it
			if request.session.test_cookie_worked():
				request.session.delete_test_cookie()

			url = urlresolvers.reverse('cart:show_cart')
			return HttpResponseRedirect(url)
		# return render(request,'catalog/forerror.html',{'form':form})
	else:
		form = ProductAddToCartForm(request=request,label_suffix=':')

	# assign the hidden input the product slug
	form.fields['product_slug'].widget.attrs['value'] = p.slug

	# set the test cookie on our first GET request
	request.session.set_test_cookie()
	return render(request, template_name,{'page_title':page_title,'categories':categories,'p':p,'form':form})
Example #4
0
def show_product(request, product_slug):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.all()
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    if request.method == 'POST':
        # add to create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        #it's a GET, create the unbound form. Note request as kwarg
        form = ProductAddToCartForm(request=request,label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # test cookie on our first GET request
    request.session.set_test_cookie()
    return render(request,'catalog/product.html', locals())
Example #5
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    product = get_object_or_404(Product, slug=product_slug)
    categories = Category.objects.all()
    cats = product.categories.all()
    page_title = product.name
    meta_keywords = product.meta_keywords
    meta_description = product.meta_description
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:

        form = ProductAddToCartForm(request=request,
                                    initial={'quantity': 1},
                                    label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render_to_response("catalog/product.html",
                              locals(),
                              context_instance=RequestContext(request))
Example #6
0
def show_category(request,
                  category_slug,
                  template_name="catalog/category.html"):
    c = get_object_or_404(Category, slug=category_slug)
    products = c.product_set.all()
    page_title = c.name
    meta_keywords = c.meta_keywords
    meta_description = c.meta_description
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
                url = urlresolvers.reverse('show_cart')
                return HttpResponseRedirect(url)
    else:
        form = ProductAddToCartForm(request=request,
                                    initial={'quantity': 1},
                                    label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    return render_to_response(template_name,
                              locals(),
                              context_instance=RequestContext(request))
Example #7
0
def home(request):
    pros = Product.objects.all()
    paginator = Paginator(pros, 10)
    page = request.GET.get('page', '1')
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)
    categories = Category.objects.all()
    page_title = "Shopcart - Home"
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:

        form = ProductAddToCartForm(request=request, initial={'quantity':1}, label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render_to_response('index.html', ({'products' : products, 'categories':categories}), context_instance=RequestContext(request)
)
Example #8
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    product = get_object_or_404(Product, slug=product_slug)
    categories = Category.objects.all()
    cats = product.categories.all()
    page_title = product.name
    meta_keywords = product.meta_keywords
    meta_description = product.meta_description
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:

        form = ProductAddToCartForm(request=request, initial={'quantity':1}, label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render_to_response("catalog/product.html", locals(),
context_instance=RequestContext(request))
Example #9
0
def add_to_cart(request):
    """ adds a product to cart """
    # add to cart...create the bound form
    postdata = request.POST.copy()
    form = ProductAddToCartForm(request, postdata)
    #check if posted data is valid
    if form.is_valid():
        #add to cart and redirect to cart page
        cart.add_to_cart(request)
    # if test cookie worked, get rid of it
    if request.session.test_cookie_worked():
        request.session.delete_test_cookie()
    print redirect('cart.views.show_cart')
    return redirect('cart.views.show_cart')
Example #10
0
 def get(self, request, id, template_name="flavors.html"):
     flavor_id = int(
         id)  # TypeError and ValueError handled by the decorator
     volume_id = request.GET.get('volume_id', False)
     volume_id = int(volume_id)
     back_url = request.GET.get('back', "/")
     back_url = unquote(back_url.encode('utf-8'))
     conc_id = request.GET.get('conc_id', False) and int(
         request.GET['conc_id']) or False
     flavor = get_object_or_404(ProductFlavors, pk=flavor_id)
     current_volume = get_object_or_404(ProductAttributeValue, pk=volume_id)
     price = old_price = 0
     name = flavor.name
     assert volume_id and (volume_id in request.volumes_data.keys()
                           ), "You are not allowed to acces this page"
     form = ProductAddToCartForm(request=request, label_suffix=':')
     # set the test cookie on our first GET request
     request.session.set_test_cookie()
     products = get_product_variants(flavor, volume_id)
     price = flavor.get_price(request.user, current_volume)
     old_price = current_volume.old_price or 0
     if request.user.is_authenticated:
         review_count = request.user.odoo_user.partner_id.review_ids.filter(
             flavor_id=flavor.id).count()
     return render(request, template_name, locals())
Example #11
0
def add_product_to_cart(request):
    data = request.POST.copy()
    product_id = data.get('product_id')
    product = get_object_or_404(Product, id=product_id)
    form = ProductAddToCartForm(product, request, data=data)

    if form.is_valid():
        form.add_to_cart()
        context = {
            'instance': form.get_product_instance(form.cleaned_data),
            'items_in_cart': cartutils.cart_distinct_item_count(request),
            'subtotal': cartutils.cart_subtotal(request),
            'quantity': form.cleaned_data.get('quantity', None),
        }
        return render_to_response('post_add_to_cart_summary.html', context, context_instance=RequestContext(request))
    else:
        raise Exception("Error in add-to-cart form submitted via ajax")
Example #12
0
    def post(self, request):
        ingredients = self.ingredients
        burger = get_current_burger(request)
        cart = get_current_cart(request)
        cart_items = cart.cartitem_set.all()

        #for adding the ingredient to the burger and show it in the cart
        form = ProductAddToCartForm(request=request)

        # set the test cookie
        request.session.set_test_cookie()

        postdata = request.POST.copy()

        # remove the burger from the cart
        if postdata['submit'] == _('Remove'):
            cart.remove_from_cart(request)
            burger = get_current_burger(request)

        # remove the ingredient from the burger
        if postdata['submit'] == _('Delete'):
            burger.remove_ingredient(postdata['ingredient'])

        # update the specific burger
        if postdata['submit'] == _('Update'):
            request.session['burger'] = postdata['burger']
            burger = get_current_burger(request, postdata['burger'])

            # add the ingredient to the cart
        if postdata['submit'] == _("Add"):
            form = ProductAddToCartForm(request, postdata)

            if form.is_valid():
                cart.add_to_cart(request)

                # if test cookie worked, get rid of it
                if request.session.test_cookie_worked():
                    request.session.delete_test_cookie()

        # retrieve total amount of the order
        order_subtotal = cart.cart_subtotal(request)
        request.session['cart_subtotal'] = str(order_subtotal)

        return render_to_response(self.template_name, locals(),context_instance=RequestContext(request))
Example #13
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    """ view for each product page """
    product_cache_key = request.path
    # try to get product from cache
    p = cache.get(product_cache_key)
    # if a cache miss, fall back on db query
    if not p:
        p = get_object_or_404(Product, slug=product_slug)
        cache.set(product_cache_key, p, CACHE_TIMEOUT)

    categories = p.categories.all()
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description

    if request.method == 'POST':

        # create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        # check if posted data is valid

        if form.is_valid():
            # add to cart and redirect to cart page

            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            return redirect('show-cart')
    else:
        # create the unbound form. Notice the request as a keyword argument
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set test cookie to make sure cookies are enabled
    request.session.set_test_cookie()

    stats.log_product_view(request, p)
    # product review additions, CH 10
    product_reviews = ProductReview.approved.filter(
        product=p).order_by('-date')
    review_form = ProductReviewForm()
    return render(request, template_name, locals())
Example #14
0
def show_product(request, product_slug, template_name="catalog/product.html"):
	p = get_object_or_404(Product, slug=product_slug)
	categories = p.categories.filter(is_active=True)
	p_detail = SellerProduct.objects.filter(product=p)
	price = p.price
	old_price = p.old_price
	quantity = 0
	for detail in p_detail:
		#retriving total quantity of a product
		quantity += detail.quantity
	name = p.name
	brand = p.brand
	sku = p.sku
	image = p.image
	description = p.description
	
	meta_keywords = p.meta_keywords
	meta_description = p.meta_description
	# need to evaluate HTTP method
	if request.method == 'POST':
		#add item to the cart
		postdata = request.POST.copy()
		form = ProductAddToCartForm(request, postdata)
		# check if posted data is valid
		if form.is_valid():
			#add to cart and redirect to cart page
			cart.add_to_cart(request)
			# if test cookie worked, get rid of it
			if request.session.test_cookie_worked():
				request.session.delete_test_cookie()
			url = urlresolvers.reverse('show_cart')
			return HttpResponseRedirect(url)
	else:
		# its GET request 
		form = ProductAddToCartForm(request=request, label_suffix=':')
	# assign the hidden input the product slug
	form.fields['product_slug'].widget.attrs['value'] = product_slug
	# set the test cookie on our first GET request
	request.session.set_test_cookie()
	return render_to_response(template_name, locals(),
		context_instance=RequestContext(request))
Example #15
0
def product_page(request, product_slug, template_name='catelog/product.html'):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    if request.method == 'POST':
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        if form.is_valid():
            cart.add_to_cart(request)
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    request.session.set_test_cookie()
    return render_to_response(template_name, locals(),
            context_instance = RequestContext(request))
Example #16
0
def show_product(request, product_slug):
    context = custom_processor(request)
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    form = ProductAddToCartForm()
    context.update({
        'p': p,
        'categories': categories,
        'page_title': page_title,
        'meta_keywords': meta_keywords,
        'meta_description': meta_description,
        'form': form
    })

    if request.method == 'POST':
        print "POST method from client!!!!"
        # add to cart...create the bound form
        post_data = request.POST.copy()
        print post_data
        form = ProductAddToCartForm(request, post_data)
        print form.is_valid()

        if form.is_valid():
            # add to cart and redirect to cart page
            carts.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()

            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        # it is a GET, create the unbound form. Note request as a kwarg
        form = ProductAddToCartForm(request=request, label_suffix=':')
        # assign the hidden input the product slug
        form.fields['product_slug'].widget.attrs['value'] = product_slug
        # set the test cookie on our first GET request
        request.session.set_test_cookie()
        return render(request, 'catalog/product.html', context)

    return render(request, "catalog/product.html", context)
Example #17
0
def show_product(request, product_slug, template_name='catalog/product.html'):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description

    # evaluate the HTTP method
    if request.method == 'POST':
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        # cart_item = CartItem()
        # my_form = ProductAddToCartForm(request, request.POST, instance=cart_item)
        # check if posted data is valid
        if form.is_valid():
            # add to cart and redirect to cart page
            cart.add_to_cart(request)

            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()

            url = urlresolvers.reverse('cart:show_cart')
            return HttpResponseRedirect(url)
        # return render(request,'catalog/forerror.html',{'form':form})
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')

    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = p.slug

    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render(request, template_name, {
        'page_title': page_title,
        'categories': categories,
        'p': p,
        'form': form
    })
Example #18
0
def home(request):
    pros = Product.objects.all()
    paginator = Paginator(pros, 10)
    page = request.GET.get('page', '1')
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)
    categories = Category.objects.all()
    page_title = "Shopcart - Home"
    if request.method == 'POST':
        # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:

        form = ProductAddToCartForm(request=request,
                                    initial={'quantity': 1},
                                    label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render_to_response('index.html', ({
        'products': products,
        'categories': categories
    }),
                              context_instance=RequestContext(request))
Example #19
0
def show_category(request, category_slug, template_name="catalog/category.html"):
    c = get_object_or_404(Category, slug=category_slug)
    products = c.product_set.all()
    page_title = c.name
    meta_keywords = c.meta_keywords
    meta_description = c.meta_description
    if request.method == 'POST':
    # add to cart...create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
                url = urlresolvers.reverse('show_cart')
                return HttpResponseRedirect(url)
    else:
        form = ProductAddToCartForm(request=request, initial={'quantity':1}, label_suffix=':')
        form.fields['quantity'].widget = forms.HiddenInput()
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))
Example #20
0
def show_product(request, slug, template_name="catalog/product.html"):
    p = get_object_or_404(Product, slug=slug)
    if request.method == 'POST':
        # add to cart...create the bound form
        post_data = request.POST.copy()
        form = ProductAddToCartForm(request, post_data)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            add_to_cart(request)
             # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        # it’s a GET, create the unbound form. Note request as a kwarg
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['slug'].widget.attrs['value'] = slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    content = {"categories":p.categories.filter(is_active=True),"page_title":p.name,"meta_keywords":p.meta_keywords,"meta_description":p.meta_description,'p':p, 'form':form }
    return render(request,template_name,content)
Example #21
0
def show_product(request, product_slug):
    """ View that returns a specific product """
    product = get_object_or_404(Product, slug=product_slug)

    if request.method == 'POST' and 'add-cart' in request.POST:
        add_to_cart(request)

    elif request.method == 'POST' and 'leave-review' in request.POST:
        add_comment_to_product(request, product_slug)

    form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    comment_form = CommentForm()

    categories = Category.objects.all()
    context = {'categories': categories, 'product': product, 'form':form,
               'comment_form':comment_form}
    return render(request, 'product.html', context)
Example #22
0
def show_product(request, product_slug, template_name="catalog/product.html"):
	# getdata = request.GET.copy()
	currency_symbol = request.session.get('currency_symbol','$');
	currency_rate = request.session.get('currency_rate',1);
	getdata = request.GET.copy().urlencode()
	currency_symbol = request.session.get('currency_symbol','$')
	p = get_object_or_404(Product, slug=product_slug)
	p.price/=Decimal(currency_rate)
	p.price = math.floor(p.price*100)/100
	
	search_value = request.GET.get('search_value')
	if search_value:
		search_result = Product.objects.filter(
			Q(name__contains=search_value) | Q(slug__contains=search_value) | Q(brand__contains=search_value)
			)
		products = search_result
		product_list = search_result
		mode="search"
		active_product = product_slug
	else:
		mode = 'view'
		subcategories = p.subcategories.filter(is_active=True)

		subc = {}
		for sc in subcategories:
			categories = sc.categories
			subc = sc
		for c in categories.all():
			category_slug = c.slug
		c = get_object_or_404(Category, slug=category_slug)

		subcategory = c.subcategory_set.all()

		path = [{'name' : c.name, "slug" : c.get_absolute_url},{'name' : subc.name, "slug" : subc.get_absolute_url},{'name' : p.name, "slug" : p.get_absolute_url}];
		active_category = c.slug
		active_subcategory = subc.slug

	

	page_title = p.name
	meta_keywords = p.meta_keywords
	meta_description = p.meta_description
	form = ProductAddToCartForm(request = request, label_suffix=':')
	form.fields['product_slug'].widget.attrs['value'] = product_slug
	
	# if request.method == 'GET':
	# 	getdata = request.GET.copy()
	# 	mode = getdata.get('mode','')
	# 	if mode == 'search':
	# 		search_value = mode = getdata.get('search_value','')
	# 		search_result = Product.objects.filter(Q(name__contains=search_value) | Q(slug__contains=search_value) | Q(brand__contains=search_value))
	# 		product_list = search_result
	# 		mode = 'search'
	# 		active_product = p.slug
	# 		active_category = ''
	# 		path = [{'name' : p.name, "slug" : p.get_absolute_url}];

	if request.method == 'POST':
		postdata = request.POST.copy()
		form = ProductAddToCartForm(request,postdata)
		if form.is_valid():
			cart.add_to_cart(request)
			added_product = get_object_or_404(Product, slug=postdata.get('product_slug',''))
			action_mode = "add"
			# if request.session.test_cookie_worked():
			# 	request.session.delete_test_cookie()
			# url = urlresolvers.reverse('show_cart')
			# return HttpResponseRedirect(url)
			return render_to_response(template_name, locals(),context_instance=RequestContext(request))
		else:
			print(form)
			form = ProductAddToCartForm(request = request, label_suffix=':')
		form.fields['product_slug'].widget.attrs['value'] = product_slug
		request.session.set_test_cookie()
		
	return render_to_response(template_name, locals(),context_instance=RequestContext(request))
Example #23
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    """
        This method displays the Product's information.
        The product is found using the slug attribut.
        This function will be update to query the product based on the 
        product ID.
        It possible for the user to add the displayed product into the cart,
        because of that we have to check whether the user used the GET or POST 
        HTTP method.
        If the user used GET then we just display the product details.
        If the user used POST then if want to add that product into the cart,
        we have to retrieve the product attribut that has been sent through
        a form, get the product and add it into the cart and return a json response.

        For static raison , we are counting the number of time this product
        has been displayed.

        As of now,the product page is served by the Class Based View ProductDetailView(defined above)
    """
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    p.view_count = p.view_count + 1
    p.save()
    page_title = p.name + ' | ' + settings.SITE_NAME
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    # HTTP methods evaluation:
    if request.method == 'POST':
        # add to cart. create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        # check if the postdata is valid
        if form.is_valid():
            # action = postdata.get('action')
            print("Add to Cart form is Valid")
            # add to cart and redirect to cart page
            user_cart = CartService.get_user_cart(request)
            # get product slug from postdata, return blank if empty
            product_slug = postdata.get('product_slug')
            # get quantity added, return 1 if empty
            quantity = postdata.get('quantity', 1)

            p = get_object_or_404(Product, slug=product_slug)
            # TO-DO Check if the product was added into the Cart
            user_cart.add_to_cart(product=p, quantity=int(quantity))
            # return JsonResponse({'status': 'ok'})
            # if test cookie worked, get rid of it

            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('cart:show_cart')
            return HttpResponseRedirect(url)

        else:
            #print("Add to Cart form is inValid")
            return JsonResponse({'status': 'error'})
    else:
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product_slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET
    request.session.set_test_cookie()
    return render(request, template_name, locals())