Example #1
0
 def test_close_cart(self):
     with f.app_context():
         cart.my_cart = None
         cart.create()
         cart.add_to_cart('French hen')
         cart.close_cart()
         assert cart.my_cart is None
Example #2
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 #3
0
def add_product_to_cart(request):
    ebentua = request.GET.get('product_id', None)
    if not ebentua:
        return HttpResponseRedirect("/")
    cart.add_to_cart(request, ebentua)

    return HttpResponseRedirect(reverse('csirteerak_show_cart'))
Example #4
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 #5
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 #6
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 #7
0
    def test_checkout_cart(self):
        with f.app_context():
            cart.my_cart = None
            cart.create()

            cart.add_to_cart('Turtle dove')
            cart.add_to_cart('French hen')
            cart.checkout_cart()
            assert cart.my_cart is None
Example #8
0
    def test_remove_item_not_in_cart(self):
        with f.app_context():
            cart.my_cart = None
            cart.create()

            cart.add_to_cart('Turtle dove')
            cart.remove_from_cart('Turtle dove')
            with pytest.raises(NotAcceptable):
                cart.remove_from_cart('Turtle dove')
            with pytest.raises(NotAcceptable):
                cart.remove_from_cart('Chachalaca')
Example #9
0
def product_page(request, product_name):
	categories = Product.objects.values_list('category_name', flat = True ).distinct()
	if request.method == "POST":
		postdata = request.POST.copy()
		form = OrderForm(postdata)
		if form.is_valid():
			add_to_cart(request)
			return redirect('show_cart_page')
	else:
		product = Product.objects.get(product_name = product_name)
		form = OrderForm()
	return render(request, "frontend/productpage.html", locals() )
Example #10
0
def product_list_by_category(request, category):
	categories = Product.objects.values_list('category_name', flat = True ).distinct()
	products = Product.objects.filter(category_name = category)
	if request.method == "POST":
		postdata = request.POST.copy()
		form = OrderForm(postdata)
		if form.is_valid():
			add_to_cart(request)
			return render(request, "frontend/products_by_category.html", locals())
			
	else:
		
		return render(request, "frontend/products_by_category.html", locals())
Example #11
0
    def test_add(self):
        with f.app_context():
            cart.my_cart = None
            cart.create()
            cart.add_to_cart('French hen')
            cart.add_to_cart('French hen')
            cart.add_to_cart('Turtle dove')
            assert cart.my_cart.items == [
                {'product': HEN, 'quantity': 2},
                {'product': DOVE, 'quantity': 1}
            ]

            cart.add_to_cart('French hen')
            with pytest.raises(NotAcceptable):
                cart.add_to_cart('French hen')
Example #12
0
def promo4_rifas(request, cart_items):
    if request.user.is_authenticated():
        promo4 = Promo4.objects.filter(winner_user=request.user,
                                       active=True).first()
        if promo4:
            request.session['promo4_id'] = str(promo4.id)

            total = Decimal(0.00)
            cart_items_products = cart.get_product_from_cart_item(cart_items)
            for product in promo4.products.all():
                if product not in cart_items_products:
                    cart.add_to_cart(request, product)
                total += product.price
            percent = promo4.discount
            discount = total * percent / 100
            promotions = "Ud. ha sido el ganador de una rifa, y ahora puede comprar estos productos " \
                         "por un descuento del %d%%" % percent
            return discount, promotions, 'rifas'
    return 0.00, False, 'no'
Example #13
0
def promo2_buy_two_take_one(request, cart_items):
    category, product = promo2()
    style = "<span style='color: #426f42; text-decoration: underline; font-weight: bold;'>"
    promo2_popup = u"Llévate gratis este producto: {style}{product}</span> " \
                   u"si compras dos de esta categoria: {style}{category}</span>".format(style=style,
                                                                                        product=product,
                                                                                        category=category)

    # obtener productos de los cart items
    product_ids = cart_items.values('product')
    product_ids = [product_id['product'] for product_id in product_ids]
    products = Product.active.filter(id__in=product_ids)
    # obtener las categorias de estos prod
    categories = Category.active.filter(product__in=products)
    cant_prod_of_this_cat = list(categories).count(category)
    if cant_prod_of_this_cat >= 2:
        if product not in products:
            cart.add_to_cart(request, product)
        discount = product.price
        promotions = promo2_popup
        return discount, promotions, "buy_two_take_one"
    return 0.00, False, 'no'
Example #14
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 #15
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 #16
0
 def test_view_cart(self):
     with f.app_context():
         cart.my_cart = None
         cart.create()
         assert cart.view_cart() == {
             'total': Decimal('0'),
             'items': []
         }
         cart.add_to_cart('Turtle dove')
         cart.add_to_cart('Turtle dove')
         cart.add_to_cart('French hen')
         assert cart.view_cart() == {
             'total': Decimal('34.67'),
             'items': [
                 {'product': DOVE, 'quantity': 2},
                 {'product': HEN, 'quantity': 1}
             ]
         }
Example #17
0
    def test_remove_items(self):
        with f.app_context():
            cart.my_cart = None
            cart.create()

            cart.add_to_cart('Turtle dove')
            cart.add_to_cart('Turtle dove')
            cart.add_to_cart('French hen')
            cart.remove_from_cart('Turtle dove')
            cartstruct = cart.view_cart()
            assert cartstruct['total'] == Decimal('22.42')
            assert cartstruct['items'] == [
                {'product': DOVE, 'quantity': 1},
                {'product': HEN, 'quantity': 1}
            ]

            cart.remove_from_cart('Turtle dove')
            cart.remove_from_cart('French hen')
            cartstruct = cart.view_cart()
            assert cartstruct['total'] == Decimal('0')
            assert cartstruct['items'] == []
def add_product_to_cart(request, product_slug):
    cart.add_to_cart(request)
    return HttpResponseRedirect('/cart/')
Example #19
0
def cart_view(request, template_name="cart/cart.html"):
    """Представление для отображения корзины"""

    postdata = request.POST.copy()

    if request.method == 'POST':

        if postdata.has_key('add'):
            cart.add_to_cart(request)
            order = cart.get_cart(request)
            # res = {'order': order, }
            # html = render_to_string('cart/summary.html', res, context_instance=RequestContext(request))
            # return JsonResponse({
            #     'success': True,
            #     'action_add': True,
            #     'panel_html': html,
            # })
        if postdata.has_key('remove'):
            cart.remove_from_cart(request)

        if postdata.has_key('update'):
            cart.update_cart(request)

        if postdata.has_key('update') or postdata.has_key('remove') or postdata.has_key('add'):
            order = cart.get_cart(request)
            formset = OrderFormset(instance=order)
            res = {
                'order_form': DefaultOrderForm(request.POST),
                'formset': formset,
                'order': order,
                'keep': BASKET_OPTIONS_USE_KEEP,
                'delete': True,
            }
            html = render_to_string('cart/cart_table.html', res, context_instance=RequestContext(request))
            panel_html = render_to_string('cart/summary.html', res, context_instance=RequestContext(request))
            return JsonResponse({
                'success': True,
                # 'action_remove': True,
                'html': html,
                'panel_html': panel_html,
            })

        # if postdata.has_key('checkout'):
        #     checkout_url = checkout.get_checkout_url(request)
        #     return HttpResponseRedirect(checkout_url)
    # Получаем список всех товаров в корзине из cookies
    #cart_item_count = cart.cart_item_count(request)
    # cart_items = cart.get_cart_items(request)
    # cart_subtotal = cart.cart_subtotal(request)
    # return render_to_response(template_name, locals(),
    #                           context_instance=RequestContext(request))

    form = DefaultOrderForm(postdata or None)
    order = cart.get_cart(request)

    if form.is_valid() and order.goods > 0:
        contact = form.save(commit=False)
        contact.order = order
        contact.save()

        remote_address = request.META.get('REMOTE_ADDR')
        order.ip_address=remote_address
        order.status = 2
        order.save()

        return HttpResponseRedirect(reverse('cart:cart_confirm'))

    # else:
    #     return HttpResponseRedirect(reverse('cart:show_cart'))

    # order = cart.get_cart(request)
    formset = OrderFormset(instance=order)
    # form = DefaultOrderForm(postdata or None)
    return {
        'order_form': form,
        'formset': formset,
        'order': order,
        'keep': BASKET_OPTIONS_USE_KEEP,
        'delete': True,
    }
Example #20
0
 def test_add_unknown_item(self):
     with f.app_context():
         cart.my_cart = None
         cart.create()
         with pytest.raises(NotFound):
             cart.add_to_cart('Noisy crow')