示例#1
0
def add_to_cart_via_post(request):
	dictionary = baseDict(request)
	update_dict_for_main_page_redirect(dictionary, request)

	quantity = request.POST.get('quantity')

	if int(quantity) <= 0: # stop if the amount added is not positive
		dictionary.update({
			"error_message" : "The quantity you added is %s, please put a positive number !" % quantity,
			'nodes' : Category.objects.all(),
		})
		# return render_to_response('index.html', dictionary, context_instance = RequestContext(request))
		return render_to_response("products.html", dictionary, context_instance = RequestContext(request))

	# otherwise fetch the product and add it's amount to the cart
	product_slug = request.POST.get('slug')
	product = Product.objects.get(slug=product_slug)
	if (int(product.quantity) - int(quantity)) < 0:
		dictionary.update({
			'error_message' : '%s has %d items left in stock !' % (product.title, product.quantity),
			'nodes' : Category.objects.all(),
		})
		return render_to_response("products.html", dictionary, context_instance = RequestContext(request))

	cart = Cart(request)
	cart.add(product, product.price, quantity)


	# return render_to_response('index.html', dictionary, context_instance = RequestContext(request))
	return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
示例#2
0
def remove_from_cart(request, product_slug):
	dictionary = baseDict(request)
	update_dict_for_main_page_redirect(dictionary, request)

	product = Product.objects.get(slug=product_slug) # fetch the product we want to remove
	cart = Cart(request) # fetch the cart
	cart.remove(product) # remove the item

	return render_to_response('cart.html', dictionary, context_instance = RequestContext(request))
示例#3
0
def view_cart(request):
	dictionary = baseDict(request)
	update_dict_for_main_page_redirect(dictionary, request)

	totalPrice = 0 # the loop calculates the total price of the shopping cart
	for i in Cart(request):
		totalPrice += i.product.price
	dictionary.update({
		'totalPrice' : totalPrice,
	})


	return render_to_response('cart.html', dictionary, context_instance = RequestContext(request))
示例#4
0
def view_all(request):

    c = {}
    c.update(csrf(
        request))  # token is required, because there is an add to cart form

    products = Product.objects.all()

    dictionary = baseDict(request)
    cartItems(request, dictionary)

    dictionary.update({
        'products': products_paginated(request, products),
        'page_title': 'Products - Amper',
        'nodes': Category.objects.all(),
    })

    return render_to_response("products.html",
                              dictionary,
                              context_instance=RequestContext(request))
示例#5
0
def product(request, slug):
    product = get_object_or_404(Product, slug=str(slug), active=True)
    dictionary = baseDict(request)  # fetch the common data
    cartItems(
        request, dictionary
    )  # get the cart items in a form of a dictionary, so that templates can display if item has been added or not
    productCategory = Category.objects.get(slug=product.category.slug)

    dictionary.update({
        'product':
        product,
        'nodes':
        Category.objects.get(slug=product.category.slug, ),
        'page_title':
        str(product.title) + " - Amper",
    })

    return render_to_response("single_product.html",
                              dictionary,
                              context_instance=RequestContext(request))
示例#6
0
def rate_product(request, slug, rating):
    product = Product.objects.get(slug=slug)
    rating = Rating(product=product, rating=int(rating), user=request.user)
    rating.save()

    dictionary = baseDict(request)
    cartItems(
        request, dictionary
    )  # get the cart items in a form of a dictionary, so that templates can display if item has been added or not

    dictionary.update({
        'product':
        product,
        'nodes':
        Category.objects.get(slug=product.category.slug, ),
        'page_title':
        str(product.title) + " - Amper",
    })

    return render_to_response("single_product.html",
                              dictionary,
                              context_instance=RequestContext(request))
示例#7
0
def view_by_category(request, category="-111"):

    dictionary = baseDict(request)

    children = Product.objects.filter(
        active=True, category__parent__slug=category
    )  # so that all subcategory products can be fetched and displayed
    direct = Product.objects.filter(
        active=True,
        category__slug=category)  # fetches the current product branch
    products = list(chain(children, direct))  # this line joins them

    cartItems(request, dictionary)

    dictionary.update({
        'category': category.title(),
        'products': products_paginated(request, products),
        'nodes': Category.objects.all(),
        'page_title': category.title() + ' - Amper',
    })

    return render_to_response("products.html",
                              dictionary,
                              context_instance=RequestContext(request))