Example #1
0
def addtocart(request, id):
    url = request.META.get('HTTP_REFERER')  # get last url
    current_user = request.user  #Access User Session Information

    ##########ÜRÜN SEPETTE VAR MI KONTROLÜ###########
    checkbook = ShopCart.objects.filter(book_id=id)
    if checkbook:
        control = 1  #ürün sepette var
    else:
        control = 0  #ürün sepette yok

    if request.method == 'POST':  #form post edildiyse   ÜRÜN SAYFASINDAN SEPETE EKLENDİYSE
        form = ShopCartForm(request.POST)
        if form.is_valid():

            if control == 1:  #ürün varsa güncelle

                data = ShopCart.objects.get(book_id=id)
                data.quantity += form.cleaned_data['quantity']
                data.save()
            else:  #ürün yoksa ekle

                data = ShopCart()  #model ile bağlantı kur
                data.user_id = current_user.id
                data.book_id = id
                data.quantity = form.cleaned_data['quantity']
                data.save()  #veritabanına kaydet

        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()  # count item in shop cart
        messages.success(request, "Kitap sepete eklendi.")
        return HttpResponseRedirect(url)

    else:  #ÜRÜN ANASAYFADAN SEPETE EKLENDİYSE
        if control == 1:  # ürün varsa güncelle

            data = ShopCart.objects.get(book_id=id)
            data.quantity += 1
            data.save()

        else:  # ürün yoksa ekle

            data = ShopCart()  # model ile bağlantı kur
            data.user_id = current_user.id
            data.book_id = id
            data.quantity = 1
            data.save()  # veritabanına kaydet

        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()  # count item in shop cart
        messages.success(request, "Kitap sepete eklendi.")
        return HttpResponseRedirect(url)

    messages.warning(request, "Bir şeyler ters gitti!")
    return HttpResponseRedirect(url)
def addtocart(request, id):
    url = request.META.get('HTTP_REFERER')
    current_user = request.user
    checkbook = ShopCart.objects.filter(book_id=id)
    if checkbook:
        control = 1  #ürün sepette var
    else:
        control = 0  #ürün sepette yok
    if request.method == 'POST':  # Form post edildiyse, ürün detay sayfasından geldiyse
        form = ShopCartForm(request.POST)
        if form.is_valid():
            if control == 1:  # ürün varsa güncelle
                data = ShopCart.objects.get(
                    book_id=id)  # get 1 eleman çağırır,filtre liste cağırır
                data.quantity += form.cleaned_data['quantity']
                data.save()  # ver,tabanına kaydet
            else:  # ürün yoksa ekle
                data = ShopCart()  # model ile bağlantı kur
                data.user_id = current_user.id
                data.book_id = id
                data.quantity = form.cleaned_data['quantity']
                data.save()  # veritabanına kaydet
            request.session['cart_items'] = ShopCart.objects.filter(
                user_id=current_user.id).count()
            messages.success(
                request, "Ürün basari ile sepete eklenmiştir.Teşekkür Ederiz")
            return HttpResponseRedirect(url)

    else:  #ürün direk sepette ekle butonuna basıldıysa
        if control == 1:  # ürün varsa güncelle
            data = ShopCart.objects.get(book_id=id)
            data.quantity += 1
            data.save()
        else:  # ürün yoksa ekle
            data = ShopCart()
            data.user_id = current_user.id
            data.book_id = id
            data.quantity = 1
            data.save()
        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()
        messages.success(request,
                         "Ürün basari ile sepete eklenmiştir.Teşekkür Ederiz")
        return HttpResponseRedirect(url)

    messages.warning(request, form.errors)
    return HttpResponseRedirect(url)
Example #3
0
def addtocart(request,id):
    url = request.META.get('HTTP_REFERER')
    current_user = request.user
    checkbook = ShopCart.objects.filter(book_id=id)
    if checkbook:
        control = 1 # The product is in the cart
    else:
        control = 0 # not
    if request.method == 'POST': # if there is a post
        form = ShopCartForm(request.POST)
        if form.is_valid():
            if control == 1:  # Update  shopcart
                data = ShopCart.objects.get(book_id=id)
                data.quantity += form.cleaned_data['quantity']
                data.save()
            else:
                data = ShopCart()
                data.user_id = current_user.id
                data.book_id = id
                data.quantity = form.cleaned_data['quantity']
                data.save()
            request.session['cart_items'] = ShopCart.objects.filter(user_id=current_user.id).count()
            messages.success(request, 'product added to cart')
            return HttpResponseRedirect(url)
    else: # if there is no post
        if control == 1:  # Update  shopcart
            data = ShopCart.objects.get(book_id=id)
            data.quantity += 1
            data.save()
        else:
            data = ShopCart()
            data.user_id = current_user.id
            data.book_id = id
            data.quantity = 1
            data.save()
        request.session['cart_items'] = ShopCart.objects.filter(user_id=current_user.id).count()
        messages.success(request, 'product added to cart')
        return HttpResponseRedirect(url)

    messages.warning(request,'adding product failed')
    return HttpResponseRedirect(url)