Example #1
0
def add(request):
    print("Work")
    basket_session = basket.Basket(request)
    if request.POST.get('action') == 'post':
        user_id = request.user.id
        order_key = request.POST.get('order_key')
        baskettotal = basket_session.get_total_price()

        if Order.objects.filter(order_key=order_key).exists():
            pass

        else:
            order = Order.objects.create(user_id=user_id,
                                         full_name='name',
                                         address1='add1',
                                         address2='add2',
                                         total_paid=baskettotal,
                                         order_key=order_key)

            order_id = order.pk

            for item in basket_session:
                OrderItem.objects.create(order_id=order_id,
                                         product=item['product'],
                                         price=item['price'],
                                         qty=item['qty'])

        response = JsonResponse({'success': 'success'})
        return response
Example #2
0
def test_discount(products, promotions):
    b = basket.Basket(products, promotions)
    assert b.add('apples')
    assert len(b.items) == 1
    assert b.subtotal == 100
    b.calculate_discounts()
    assert b.total == 90
    assert len(b.discounted_items) == 1
    assert b.discounted_items[0] is b.items[0]
Example #3
0
def test_add_item_no_promotions(products, promotions_empty):
    b = basket.Basket(products, promotions_empty)
    assert b.products is products
    assert b.promotions is promotions_empty
    assert b.add('apples')
    b.calculate_discounts()
    assert b.subtotal == 100
    assert b.total == 100
    assert not len(b.discounted_items)
Example #4
0
def main(request):
    basket_session = basket.Basket(request)
    total = str(basket_session.get_total_price())
    total = total.replace('.', '')
    total = int(total)
    if basket_session.current_basket_session:
        stripe.api_key = "sk_test_51ITS4wLj3bOPswXRIdrgWqrXTmykXjDcW1g8QIW8xtV3zi9hNolGzrlkCMMbLJtkOCAP8YMboyCpouIH1QbMpYRK00N7MyT0w3"
    
        intent = stripe.PaymentIntent.create(
            amount=total,
            currency='usd',
            metadata={'userid': request.user.id}
        )

        return render(request, 'payment/main.htm', {'client_secret': intent.client_secret})
    else:
        return redirect('/')
def price():
    items = request.args.getlist('item', '')

    # Load available goods and offers
    products = load_products('products.json')
    promotions = load_promotions('promotions.json')

    # Make a basket and fill
    shopping_basket = basket.Basket(products, promotions)
    for item in items:
        if not shopping_basket.add(item):
            logger.log(f'Item \'{item}\' not in stock')

    # collect the results
    result = f'Subtotal: £{shopping_basket.subtotal/100:.2f}'
    shopping_basket.calculate_discounts()
    if shopping_basket.discounted_items:
        for item in shopping_basket.discounted_items:
            result += '/n' + item.discount_message
    else:
        result += '/n' + '(No offers available)'
    result += '/n' + f'Total: £{shopping_basket.total/100:.2f}'

    return jsonify(result)
Example #6
0
def main(argv=None):
    """Program entry point.

    Parses any arguments, invokes `load_products` and `load_promotions` (to
    load the available goods and offers) and constructs a `basket` instance,
    giving it the available products in stock (`products`) and any prevailing
    offers (`promotions`).  For each product item specified on the command line,
    we add it to the basket. When all items have been added we query `basket`
    for a sub-total, discounts that could be applied and total price, which is
    output.

    :param list argv: Command line arguments.
    """
    # Parse arguments
    args = parse_args(argv)
    logger.enabled = args.verbose

    # Load available goods and offers
    products = load_products(args.products)
    promotions = load_promotions(args.promotions)

    # Make a basket and fill
    shopping_basket = basket.Basket(products, promotions)
    for item in args.items:
        if not shopping_basket.add(item):
            logger.log(f'Item \'{item}\' not in stock')

    # Print the results
    print(f'Subtotal: £{shopping_basket.subtotal/100:.2f}')
    shopping_basket.calculate_discounts()
    if shopping_basket.discounted_items:
        for item in shopping_basket.discounted_items:
            print(item.discount_message)
    else:
        print('(No offers available)')
    print(f'Total: £{shopping_basket.total/100:.2f}')
Example #7
0
def order_success(request):
    basket_session = basket.Basket(request)
    basket_session.clear()
    return render(request ,"payment/thanks.htm")
Example #8
0
def test_add_item_no_products(products_empty, promotions):
    b = basket.Basket(products_empty, promotions)
    assert b.products is products_empty
    assert b.promotions is promotions
    assert b.add('apples') is False
Example #9
0
def test_add_item(products, promotions):
    b = basket.Basket(products, promotions)
    assert b.add('apples')
    assert len(b.items) == 1
    assert b.subtotal == 100
    assert b.total == 100
Example #10
0
def test_basket(products, promotions):
    b = basket.Basket(products, promotions)
    assert b.products is products
    assert b.products is products
    assert b.items == []
    assert b.discounts == []