def test_price_basket_buy_three_get_one_free_applicable(): b = Basket() b.add(item_name="peas", count=3) c = {"peas": 2.0} o = [buy_x_get_y_free("peas", x=3, y=1)] sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 6.0 assert discount == 2.0 assert total == 4.0
def test_price_basket_with_two_identical_items(): b = Basket() b.add(item_name="peas", count=2) c = {"peas": 2.01} o = {} sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 4.02 assert discount == 0 assert total == 4.02
def test_price_basket_with_a_single_discunted_item(): b = Basket() b.add(item_name="peas", count=1) c = {"peas": 2.02} o = [discounted_item("peas", 0.5)] sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 2.02 assert discount == 1.01 assert total == 1.01
def test_price_basket_with_single_item(): b = Basket() b.add(item_name="peas") c = {"peas": 2.01} o = {} sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 2.01 assert discount == 0 assert total == 2.01
def test_price_basket_cheapest_of_n_feee_with_two_qalifying_groups(): b = Basket() b.add(item_name="peas", count=3) b.add(item_name="shampoo", count=3) c = {"peas": 2.0, "shampoo": 1.50} o = [cheapest_x_of_n_items_free(items={"peas", "shampoo"}, n=3, x=1)] sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 10.50 assert discount == 3.50
def add_basket(request): basket = Basket(request) if request.POST.get('action') == 'post': product_id = int(request.POST.get('productid')) product_qty = int(request.POST.get('productqty')) product = get_object_or_404(Product, id=product_id) basket.add(product=product, qty=product_qty) basketqty = basket.__len__() response = JsonResponse({'qty': basketqty}) return response
def test_price_basket_cheapest_of_n_feee_0(): b = Basket() b.add(item_name="peas", count=2) b.add(item_name="shampoo", count=1) c = {"peas": 2.0, "shampoo": 1.50} o = [cheapest_x_of_n_items_free(items={"peas", "shampoo"}, n=3, x=1)] sub_total, discount, total = price_basket(basket=b, catalogue=c, offers=o) assert sub_total == 5.50 assert discount == 1.50 assert total == 4.0
def form_valid(self, form): result = super().form_valid(form) user = self.request.user profile = Profile.objects.get(user=user) my_basket = Basket( self.request) # this is the offline basket (before logging in) if Order.objects.filter(client=profile, active_basket=True).exists(): active_order = Order.objects.get(client=profile, active_basket=True) else: if len(my_basket) > 0: active_order = Order.objects.create( client=profile, active_basket=True, ) else: return result order_items = active_order.items.all() my_basket_copy = {} for key in my_basket.basket: my_basket_copy[key] = my_basket.basket[key].copy() for item in order_items: # update the session basket from the database my_basket.add(item.product, item.quantity) for product_id in my_basket_copy: # update the database with the copy of the offline basket my_product = Product.objects.get(id=int(product_id)) if OrderItem.objects.filter(product=my_product, order=active_order).exists(): item = OrderItem.objects.get(product=my_product, order=active_order) item.quantity += my_basket_copy[product_id]['qty'] item.save() else: OrderItem.objects.create( order=active_order, product=my_product, price=my_product.price, quantity=my_basket_copy[product_id]['qty']) return result