Exemple #1
0
def checkout(request):
    currentuser = request.user
    carts = Cart.objects.filter(user_id=currentuser.id)
    customer = Customer.objects.get(user_id=currentuser.id)
    myuser = User.objects.get(id=currentuser.id)

    total = 0
    qty = 0
    shipping = 14.99
    for cart in carts:
        total = total + cart.amount
        qty = qty + cart.qty

    grand_total = total + shipping

    if request.method == "POST":
        firstname = request.POST['firstname']
        lastname = request.POST['lastname']
        phone = request.POST['phone']
        house_no = request.POST['house_no']
        street = request.POST['street']
        city = request.POST['city']
        pin = request.POST['pin']
        code = "OD-" + get_random_string(10).upper()

        order = Order(user_id=currentuser.id,
                      first_name=firstname,
                      last_name=lastname,
                      phone=phone,
                      house_no=house_no,
                      street=street,
                      city=city,
                      pin=pin,
                      total=grand_total,
                      code=code)
        order.save()

        for cart in carts:
            orderpr = OrderProduct(order_id=order.id,
                                   user_id=currentuser.id,
                                   product_id=cart.product_id,
                                   qty=cart.qty,
                                   price=cart.product.price,
                                   amount=cart.amount)
            orderpr.save()

        Cart.objects.filter(user_id=currentuser.id).delete()
        messages.success(request, "Zamówienie zostało złożone.")
        return redirect('ShopHome')

    details = {
        'myuser': myuser,
        'customer': customer,
        'carts': carts,
        'qty': qty,
        'total': total,
        'grand_total': grand_total,
    }

    return render(request, 'checkout.html', details)
 def test_generate_symvar(self):
     """Order generates symvar from nonempty database"""
     mommy.make('eshop.Order')
     mommy.make('eshop.Order')
     order = Order()
     order.generate_symvar()
     self.assertEqual(order.symvar, '20173')
 def test_notify(
     self,
     mock_send_mail,
 ):
     """Order notify sends e-mail"""
     order = Order()
     order.notify('*****@*****.**', 'test-subject', 'test-body')
     mock_send_mail.assert_called_with(
         'test-subject',
         'test-body',
         'objednavky@localhost',
         ['*****@*****.**'],
     ),
 def test_notify_current_status_canceled(
     self,
     mock_notify_accepted,
     mock_notify_on_route,
     mock_notify_canceled,
 ):
     """Order notifies buyer when order gets canceled"""
     order = Order()
     order.status = 5
     order.notify_current_status()
     mock_notify_accepted.assert_not_called()
     mock_notify_on_route.assert_not_called()
     mock_notify_canceled.assert_called_once()
 def test_notify_current_status_empty(
     self,
     mock_notify_accepted,
     mock_notify_on_route,
     mock_notify_canceled,
 ):
     """Order sends no notification by default"""
     order = Order()
     order.status = None
     order.notify_current_status()
     mock_notify_accepted.assert_not_called()
     mock_notify_on_route.assert_not_called()
     mock_notify_canceled.assert_not_called()
 def test_save_generates_symvar(
     self,
     mock_generate_symvar,
     mock_mark_drawings_as_sold,
     mock_notify_current_status,
 ):
     """Order generates symvar when missing"""
     order = Order(
         price=500,
         delivery_id=10,
         payment_id=10,
     )
     order.save()
     mock_generate_symvar.assert_called_once()
 def test_save_skips_generating_symvar(
     self,
     mock_generate_symvar,
     mock_mark_drawings_as_sold,
     mock_notify_current_status,
 ):
     """Order skips generating symvar when already present"""
     order = Order(
         price=500,
         delivery_id=10,
         payment_id=10,
         symvar='ax22123',
     )
     order.save()
     mock_generate_symvar.assert_not_called()
 def test_update_paid_status_paid(
     self,
     mock_get_total_amount_received,
     mock_notify_underpaid,
     mock_notify_paid,
     mock_save,
 ):
     """
         Order update_paid_status saves order as paid when buyer paid
         exactly total price and notifies buyer.
     """
     order = Order(
         price=500,
         paid=False,
         over_paid=False,
     )
     order.update_paid_status()
     self.assertEqual(order.paid, True)
     self.assertEqual(order.over_paid, False)
     mock_get_total_amount_received.assert_called_once()
     mock_save.assert_called_once()
     mock_notify_paid.assert_called()
     mock_notify_underpaid.assert_not_called()
 def test_save_not_paid_not_sold(
     self,
     mock_generate_symvar,
     mock_mark_drawings_as_sold,
     mock_notify_current_status,
 ):
     """Order does not mark drawings as sold when not paid"""
     order = Order(
         price=500,
         delivery_id=10,
         payment_id=10,
         symvar='ax22123',
     )
     order.initial_paid = True
     order.paid = False
     order.save()
     mock_mark_drawings_as_sold.assert_not_called()
 def test_save_status_change_notify(
     self,
     mock_generate_symvar,
     mock_mark_drawings_as_sold,
     mock_notify_current_status,
 ):
     """Order does not notfiy buyer when there was no status change"""
     order = Order(
         price=500,
         delivery_id=10,
         payment_id=10,
         symvar='ax22123',
     )
     order.initial_status = 1
     order.status = 2
     order.save()
     mock_notify_current_status.assert_called_once()
 def test_generate_symvar_empty_db(self):
     """Order generates symvar when database is empty"""
     order = Order()
     order.generate_symvar()
     self.assertEqual(order.symvar, '20171')
 def test_string_representation(self):
     """Test that Order turns to string properly"""
     entry = Order(symvar="29341")
     self.assertEqual(str(entry), 'Objednávka 29341')