Exemple #1
0
def products_view(request, *args, **kwargs):
    thisCart = u_cart(request)

    productsRows, categories = getProductsCatrows()

    myContext = {"title": "Товары", "categories": categories}

    myContext["products"] = productsRows

    print(productsRows['овощи'][0].price)

    return render(request, "products.html", myContext)
Exemple #2
0
def product_detail_view(request, *args, **kwargs):
    thisCart = u_cart(request)
    queryDict = request.GET
    requestedArticle = int(request.GET['article'])
    requestedObject = Product.objects.get(article=requestedArticle)
    requestedObject.amount -= getReservation(requestedArticle)

    myContext = {
        "title": f"Купить {requestedObject.title}",
        "pr": requestedObject
    }

    # requests.get(f"https://api.telegram.org/bot1225466990:AAHeSxZ66mt1sOD_0ojhUf4EpbxoVK06TAY/sendMessage?chat_id=@dchadm&text=Кто-то%20заказал:%20{requestedObject.title}")

    return render(request, "product_details.html", myContext)
Exemple #3
0
def complete_order(request, *args, **kwargs):
    queryDict = request.POST

    if not request.method == "POST":
        return HttpResponse(
            f"Wrong request method '{request.method}'! Expected 'POST'")

    try:
        cartJSON = u_cart(request).json
        cartData = json.loads(cartJSON)
        if len(cartData) < 1:
            return HttpResponse("Вы не можете оформить один заказ дважды!")

        name = queryDict['name']
        phone = queryDict['phone']
        email = queryDict['email']
    except:
        return HttpResponse("Not enough request data!")

    try:
        comment = queryDict['comment']
    except:
        comment = "—"
        print("No comment! =)")

    # Create OrderObject:
    OrderObject = Order(json=cartJSON, name=name, email=email)

    # Clear the user's cart:
    try:
        clearCart(request)
    except Exception as exc:
        return HttpResponse(
            f"Can not clear your cart because of the above exception:\n{exc}")

    OrderObject.save()

    request.session['name'] = f",<br>{name}!"

    print(f"{request.get_host()}/remove_order?oid={OrderObject.id}")

    sendOrderNotification(
        name, cartData, OrderObject.id, phone, email, comment,
        f"{request.get_host()}/remove_order?oid={OrderObject.id}",
        f"{request.get_host()}/recieve_order?oid={OrderObject.id}")
    sendThanksForOrderEmail(OrderObject)

    return HttpResponseRedirect(f'/thankyou/?oid={OrderObject.id}')
Exemple #4
0
def make_order_view(request, *args, **kwargs):
    print(request.session._session_key)
    thisCart = json.loads(u_cart(request).json)
    productsRows, categories = getProductsCatrows()
    cartProducts = Product.objects.filter(isAvailable=True,
                                          article__in=thisCart)

    totalPrice = 0
    for cpr in cartProducts:
        totalPrice += calcPrice.calc_final_price(cpr, thisCart)

    myContext = {
        'title': 'Оформление заказа',
        'form': OrderForm(request.POST),
        "cartProducts": cartProducts,
        "thisCart": thisCart,
        "totalPrice": totalPrice,
        "cartID": request.session._session_key
    }

    return render(request, 'make_order.html', myContext)
Exemple #5
0
def add_to_cart(request, *args, **kwargs):
    thisCart = u_cart(request)
    queryDict = request.GET

    if not (request.method == "GET") or not ('article' in queryDict):
        return HttpResponse("Иди куда подальще!")

    try:
        requestedArticle = int(request.GET['article'])
        requestedAmount = int(request.GET['amount'])
        requestedObject = Product.objects.get(article=requestedArticle)
        if requestedAmount > requestedObject.amount:
            return HttpResponse("Нет такого количества товаров!")
    except:
        return HttpResponse("Bad request!")

    newAmount = requestedObject.amount - requestedAmount
    whetherAvailable = True
    if newAmount == 0:
        whetherAvailable = False

    return HttpResponse(requestedObject.title)