def remove_from_cart(request, idea_id):
    idea = Idea.objects.get(id=idea_id)
    cart = Cart(request)
    cart.remove(idea)
    return render(request, 'cart.html', context={
                'info_message': "Successfully!", 'cart': cart
})
Beispiel #2
0
def order_create(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order,
                                         product=item['product'],
                                         price=item['price'],
                                         quantity=item['quantity'])
                ammount = item['price']
            # clear the cart
            param_dict = {
                'MID': 'lENwsL85644795729805',
                'ORDER_ID': str(order.id),
                'TXN_AMOUNT': str(ammount),
                'CUST_ID': order.email,
                'INDUSTRY_TYPE_ID': 'Retail',
                'WEBSITE': 'WEBSTAGING',
                'CHANNEL_ID': 'WEB',
                'CALLBACK_URL': 'http://127.0.0.1:8000/orders/handlepayment/',
            }
            param_dict['CHECKSUMHASH'] = Checksum.generate_checksum(
                param_dict, MERCHANT_KEY)
            cart.clear()
            return render(request, 'orders/order/paytm.html',
                          {'param_dict': param_dict})
            # return render(request, 'orders/order/created.html', {'order': order})
    else:
        form = OrderCreateForm()
    return render(request, 'orders/order/create.html', {
        'cart': cart,
        'form': form
    })
def add_to_cart(request, idea_id):
	idea = Idea.objects.get(id=idea_id)
	quantity = request.POST.get('quantity')
	cart = Cart(request)
	cart.add(idea, idea.price, quantity)
	return render(request, 'cart.html', context={
                'info_message': "Successfully!", 'cart': cart
})
Beispiel #4
0
def add_to_cart(request, idea_id):
    idea = Idea.objects.get(id=idea_id)
    cart = Cart(request)
    cart.add(idea, idea.price)
    return render(request,
                  'cart.html',
                  context={
                      'info_message': "Idea added successfully!",
                      'cart': cart
                  })
Beispiel #5
0
def cart_add(request, item_id):
    """

    """
    cart = Cart(request)
    item = get_object_or_404(ItemModel, id=item_id)
    form = CartAddItemForm(request.POST)
    if form.is_valid():
        data = form.cleaned_data  # {"quantity": 1}
        cart.add(item=item,
                 quantity=data.get("quantity"),
                 override_quantity=data.get("override"))
        messages.success(request, "Item agregado exitosamente!")
    return redirect("carts:cart_detail")  # Redirect cartDetail
Beispiel #6
0
def checkout(request):
    cart = Cart(request)
    books_in_cart = cart.get_books()
    user = request.user
    if len(books_in_cart) == 0:
        return redirect('home')

    for book in books_in_cart:
        purchase = Purchase(user=user, book=book)
        purchase.save()

    cart.remove_many(books_in_cart)

    ctx = {'books': books_in_cart}
    return render(request, 'purchase/finish.html', ctx)
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({"authenticated": False}, status=403)

        cart = Cart(request)
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save(commit=False)
            if cart.coupon:
                order.coupon = cart.coupon
                order.discount = cart.get_discount_total()
            order.save()
            for item in cart:
                OrderItem.objects.create(order=order,
                                         product=item['product'],
                                         price=item['price'],
                                         quantity=item['quantity'])
            cart.clear()
            data = {"order_id": order.id}
            return JsonResponse(data)
        else:
            return JsonResponse({}, status=401)
Beispiel #8
0
def order_create(request):
    cart = Cart(request)

    if request.method == "POST":
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()  # Order
            for obj in cart:
                OrderItemModel.objects.create(order=order,
                                              item=obj.get('item'),
                                              price=obj.get('price'),
                                              quantity=obj.get('quantity'))

            # clear cart
            cart.clear()
            return render(request, "", {"order": order})  # order successful
    else:
        form = OrderCreateForm()
    return render(request, "orders/create_order.html", {
        "cart": cart,
        "form": form
    })
Beispiel #9
0
def checkout(request):
    cart = Cart(request)
    publishKey = settings.STRIPE_PUBLISHABLE_KEY
    customer_id = request.user.userstripe.stripe_id
    if request.method == 'POST':
        token = request.POST['stripeToken']
        # Charge the user's card:
        try:
            customer = stripe.Customer.retrieve(customer_id)
            customer.sources.create(source=token)
            charge = stripe.Charge.create(
                amount=int(cart.summary()) * 100,
                currency="usd",
                description="Example charge",
                customer=customer,
            )
            for item in cart:
                idea = Idea.objects.get(id=item.product.id)
                user = request.user
                idea.owner = user
                idea.on_sale = False
                idea.save()

            cart.clear()
            return HttpResponseRedirect(reverse('ideas'))
        except stripe.error.CardError as e:
            #The card has been declined
            pass

    context = {
        'publishKey': publishKey,
        'cart': cart,
        'info_message': "Purchase successful"
    }
    template = 'checkout.html'
    return render(request, template, context)
Beispiel #10
0
 def get_cart(self):
     return Cart(self.request)
Beispiel #11
0
def confirm(request):
    cart = Cart(request)
    ctx = {'cart': cart}
    return render(request, 'purchase/confirm.html', ctx)
def cart(request):
	cart = Cart(request)
	context = {'cart':cart}
	template = 'cart.html'
	return render(request,template,context)
Beispiel #13
0
def cart_detail(request):
    cart = Cart(request=request)
    return render(request, "carts/detail.html", {"cart": cart})
Beispiel #14
0
def cart_remove(request, item_id):
    cart = Cart(request)
    item = get_object_or_404(ItemModel, id=item_id)
    cart.remove(item=item)
    messages.success(request, "Item removido exitosamente del carrito!")
    return redirect("carts:cart_detail")  # Redirect cartDetail
Beispiel #15
0
def create_order(request):
    cart = Cart(request)
    items = map_cart_items(cart)
    order_items = []
    if request.method == "POST":
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            with transaction.atomic():  # Bloque controlado por transaction
                order = form.save(commit=False)  # Guarda una instancia previa al usar el commit=False de order.
                order.code = generate_unique_code()
                if cart.coupon:
                    order.coupon = cart.coupon
                    order.discount = cart.coupon.discount
                    order.subtotal = cart.get_total_subtotal()  # Suma Subtotales
                    order.base12 = order.subtotal  # Suma de items base12
                    order.iva = cart.get_total_subtotal() * Decimal(0.12)  # calcular impuesto
                    order.discount_value = round(Decimal(cart.get_discount()), 2)
                    order.total = round(cart.get_total_price_after_discount() + Decimal(order.fee))
                else:
                    order.subtotal = cart.get_total_subtotal()
                    order.base12 = order.subtotal
                    order.iva = round(cart.get_total_subtotal() * Decimal(0.12))  # TODO: Dinamico
                    order.total = round(order.subtotal + order.iva + Decimal(order.fee), 2)
                order.save()  # Insert en BD.

                for item in cart:
                    # order_items = [OrderItem1, OrderItem2, ....]
                    order_items.append(
                        OrderItem(
                            order=order,
                            product=item[1].get('product'),  # Instancia de objecto producto
                            price=item[1].get('price'),
                            quantity=item[1].get('quantity')
                        )
                    )
                    # OrderItem.objects.create(
                    # 	order=order,
                    # 	product=item[1].get('product'),	# Instancia de objecto producto
                    # 	price=item[1].get('price'),
                    # 	quantity=item[1].get('quantity')
                    # )
                OrderItem.objects.bulk_create(order_items)
                # Borrar los items asociados a ese carrito
                cart.clear()

                # Metodo de notificacion via correo del cliente
                send_mail(
                    "Commerce| Compra Exitosa",
                    f"Su orden con codigo {order.code} ha generada exitosamente.",
                    "*****@*****.**",
                    [order.billing_email],
                    fail_silently=False,
                )
                return render(request, "order/order_created.html", {'order': order})
    else:
        form = OrderCreateForm()
    return render(
        request, "order/order_form.html",
        {
            'cart': cart, 'form': form, 'items': items
        }
    )
Beispiel #16
0
def cart(request):
    cart_instance = Cart(request)
    return {'cart': cart_instance}