Beispiel #1
0
def control_slot_point(request):
    if request.method == 'POST':
        slot_point = request.POST["slot_point"]
        user = request.user
        user.point += int(slot_point)
        user.save()
        userpointhistory = UserPointHistory(point_history=user.point,
                                            user=user)
        userpointhistory.save()
    return redirect('games:slot')
Beispiel #2
0
def cart(request):
    user = request.user
    cart = request.session.get('cart', {})
    cart_products = dict()
    total_price = 0
    for product_id, num in cart.items():
        product = Product.objects.get(id=product_id)
        cart_products[product] = num
        total_price += product.price * num

    purchase_form = PurchaseForm(request.POST or None)
    if purchase_form.is_valid():
        if 'search_address' in request.POST:
            zip_code = request.POST['zip_code']
            address = get_address(zip_code)
            if not address:
                messages.warning(request, "could not get the adrress...")
                return redirect('app:cart')
            purchase_form = PurchaseForm(
                initial={'zip_code': zip_code, 'address': address})
        if 'buy_product' in request.POST:
            if not purchase_form.cleaned_data['address']:
                messages.warning(request, "Address needs.")
                return redirect('app:cart')
            if not bool(cart):
                messages.warning(request, "Cart is empty.")
                return redirect('app:cart')
            if total_price > user.point:
                messages.warning(request, "You do not have enough points...")
                return redirect('app:cart')
            for product_id, num in cart.items():
                if not Product.objects.filter(pk=product_id).exists():
                    del cart[product_id]
                product = Product.objects.get(pk=product_id)
                sale = Sale(product=product, user=user, amount=num,
                            price=product.price, total_price=num*product.price)
                sale.save()
            user.point -= total_price
            user.save()
            userpointhistory = UserPointHistory(
                point_history=user.point, user=user)
            userpointhistory.save()
            del request.session['cart']
            messages.success(request, "You purchased items!")
            return redirect('app:cart')
    ctx = {
        'purchase_form': purchase_form,
        'cart_products': cart_products,
        'total_price': total_price,
    }
    return render(request, 'app/cart.html', ctx)
Beispiel #3
0
def control_counter_point(request):
    if request.method == 'POST':
        counter_point = request.POST["counter_point"]
        user = request.user
        user.point += int(counter_point)
        user.save()
        userpointhistory = UserPointHistory(point_history=user.point,
                                            user=user)
        userpointhistory.save()
        if int(counter_point) > 0:
            messages.success(request, f"You won {counter_point}!")
        if int(counter_point) <= 0:
            messages.warning(request, f"You lost {counter_point}..")
    return redirect('games:counter')