def remove_from_cart(request, slug): item = get_object_or_404(Products, slug=slug) order_qs = Order.objects.filter( user=request.user, ordered=False ) if order_qs.exists(): order = order_qs[0] # check if the order item is in the order if order.items.filter(item__slug=item.slug).exists(): order_item = OrderItem.objects.filter( item=item, user=request.user, ordered=False )[0] order.items.remove(order_item) order_item.delete() messages.Info(request, "This item was removed from your cart.") return redirect("order-summary") else: messages.Info(request, "This item was not in your cart") return redirect() else: messages.Info(request, "You do not have an active order") return redirect("core:product", slug=slug)
def add_to_cart(request, slug): item = get_object_or_404(Products, slug=slug) order_item, created = OrderItem.objects.get_or_create( item=item, user=request.user, ordered=False ) order_qs = Order.objects.filter(user=request.user, ordered=False) if order_qs.exists(): order = order_qs[0] # check if the order item is in the order if order.items.filter(item__slug=item.slug).exists(): order_item.quantity += 1 order_item.save() messages.Info(request, "This item quantity was updated.") return redirect("order-summary") else: order.items.add(order_item) messages.Info(request, "This item was added to your cart.") return redirect("order-summary") else: ordered_date = timezone.now() order = Order.objects.create( user=request.user, ordered_date=ordered_date) order.items.add(order_item) print("1111111111111111111") messages.Info(request, "This item was added to your cart.") return redirect("order-summary")
def remove_single_item_from_cart(request, slug): item = get_object_or_404(Products, slug=slug) order_qs = Order.objects.filter( user=request.user, ordered=False ) if order_qs.exists(): order = order_qs[0] # check if the order item is in the order if order.items.filter(item__slug=item.slug).exists(): order_item = OrderItem.objects.filter( item=item, user=request.user, ordered=False )[0] if order_item.quantity > 1: order_item.quantity -= 1 order_item.save() else: order.items.remove(order_item) messages.Info(request, "This item quantity was updated.") return redirect("order-summary") else: messages.Info(request, "This item was not in your cart") return redirect("product", slug=slug) else: messages.Info(request, "You do not have an active order") return redirect("product", slug=slug)
def catadd(request): if request.session.has_key("key"): if request.method == 'POST': catName = request.POST['names'] img = request.POST['itemImgOutput'] format, imgstr = img.split(';base64,') ext = format.split('/')[-1] data = ContentFile(base64.b64decode(imgstr), name=catName + '.' + ext) if Categories.objects.filter(categoryName=catName).exists(): messages.Info(request, 'category exists') # return redirect('addCategory') return JsonResponse('false1', safe=False) elif Categories.objects.filter(categoryPic=img).exists(): messages.Info(request, 'choose another Pic') # return redirect('addCategory') return JsonResponse('false2', safe=False) else: addCat = Categories.objects.create(categoryName=catName, categoryPic=data) addCat.save() # return redirect('categories') return JsonResponse('true', safe=False) else: return redirect('addCategory') else: return redirect('Alogin')
def contact(request): form = ContactForm(request.POST or None) if request.method == "POST": print("posted") if form.is_valid(): print() print("form is valid") print() subject = form.cleaned_data.get('subject') message =f"Message from:{form.cleaned_data.get('email')}:\n{form.cleaned_data.get('snd_message')}" recipient = ADMIN_MAIL send_mail(subject, message, EMAIL_HOST_USER, (recipient,), fail_silently=False) messages.Info(request, "Thank you for reaching out to us. You would be responded to as soon as possible") return redirect("core:home") return render(request, "core/contact.html", {'form':form})