def add_to_cart(request, slug, size): user = None if request.user.is_authenticated: user = request.user cart = request.session.get('cart') if cart is None: cart = [] tshirt = Tshirt.objects.get(slug = slug) size_temp = SizeVariant.objects.get(size = size , tshirt = tshirt) #we are storing the size and tshirt in size_temp variable flag = True for cart_obj in cart: t_id = cart_obj.get('tshirt') size_short = cart_obj.get('size') if t_id == tshirt.id and size == size_short: flag = False cart_obj['quantity'] = cart_obj['quantity']+1 if flag: cart_obj = { 'tshirt': tshirt.id, 'size': size, 'quantity': 1 } cart.append(cart_obj) if user is not None: existing = Cart.objects.filter(user = user, sizeVariant = size_temp) if len(existing) > 0: obj = existing[0] obj.quantity = obj.quantity+1 obj.save() else: c = Cart() c.user = user c.sizeVariant = size_temp c.quantity = 1 c.save() request.session['cart'] = cart return_url = request.GET.get('return_url') print(slug, size) return redirect(return_url )
def add_cart_to_database(user, size, tshirt): size = SizeVarient.objects.get(size=size, tshirt=tshirt) existing = Cart.objects.filter(user=user, sizevarient=size) if len(existing) > 0: obj = existing[0] obj.quantity = obj.quantity + 1 obj.save() else: c = Cart() c.user = user c.sizevarient = size c.quantity = 1 c.save()
def add_cart_to_database(user, size, tshirt): size = SizeVariant.objects.get(size=size, tshirt=tshirt) existing = Cart.objects.filter(user=user, sizeVariant=size) if len(existing) > 0: obj = existing[0] obj.quantity = obj.quantity + 1 obj.save() #saving for else: c = Cart() #creating an object for CART Table c.user = user #insert into cart table values for user and below for sizevariant c.sizeVariant = size c.quantity = 1 c.save() #folr saving cart object
def post(self, request): form = CustomerAuthForm(data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user: loginUser(request, user) session_cart = request.session.get('cart') if session_cart is None: session_cart = [] else: for c in session_cart: size = c.get('size') tshirt_id = c.get('tshirt') quantity = c.get('quantity') cart_obj = Cart() cart_obj.sizeVariant = SizeVariant.objects.get( size=size, tshirt=tshirt_id) cart_obj.quantity = quantity cart_obj.user = user cart_obj.save() # { size , tshirt , quantiti } cart = Cart.objects.filter(user=user) session_cart = [] for c in cart: obj = { 'size': c.sizeVariant.size, 'tshirt': c.sizeVariant.tshirt.id, 'quantity': c.quantity } session_cart.append(obj) request.session['cart'] = session_cart next_page = request.session.get('next_page') if next_page is None: next_page = 'homepage' return redirect(next_page) else: return render(request, template_name='store/login.html', context={'form': form})
def post(self, request): form = CustomerAuthenticationForm(data=request.POST) if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = authenticate( username=username, password=password) # authenticating user weather its or not if user: loginUser(request, user) # adding user in session session_cart = request.session.get('cart') if session_cart is None: session_cart = [] else: for c in session_cart: size = c.get('size') quantity = c.get('quantity') cloth_id = c.get('cloth') cart_obj = Cart() cart_obj.sizeVariant = Size_Variant.objects.get( size=size, cloth=cloth_id) cart_obj.quantity = quantity cart_obj.user = user cart_obj.save() cart = Cart.objects.filter(user=user) session_cart = [] for c in cart: obj = { 'size': c.sizeVariant.size, 'cloth': c.sizeVariant.cloth.id, 'quantity': c.quantity } session_cart.append(obj) request.session['cart'] = session_cart next_page = request.session.get('next_page') if next_page is None: next_page = "home" return redirect(next_page) else: context = {"form": form} return render(request, 'store/login.html', context=context)
def post(self,request): #this will handle POST request form=CustomerAuthForm(data=request.POST) if form.is_valid(): username=form.cleaned_data.get('username') password=form.cleaned_data.get('password') user=authenticate(username=username,password=password) if user: loginUser(request,user) #if this is not given it shows anaonymous user session_cart = request.session.get('cart') #to merge before login cart items to after login if session_cart is None: session_cart=[] else: for c in session_cart: size=c.get('size') tshirt_id=c.get('tshirt') quantity=c.get('quantity') cart_obj=Cart() #creating new object for Cart Model cart_obj.sizeVariant = SizeVariant.objects.get(size=size,tshirt =tshirt_id) cart_obj.quantity = quantity cart_obj.user=user cart_obj.save() # { size , tshirt,quantity} cart = Cart.objects.filter(user=user) session_cart=[] for c in cart: obj={ 'size':c.sizeVariant.size, 'tshirt' : c.sizeVariant.tshirt.id, 'quantity': c.quantity } session_cart.append(obj) request.session['cart'] = session_cart next_page = request.session.get('next_page') if next_page is None: next_page='homepage' return redirect(next_page) else: return render(request,template_name="store/login.html",context={ 'form':form})
def get(self, request, cloth_slug, size): user = None if request.user.is_authenticated: user = request.user cart = request.session.get('cart') if cart is None: cart = [] cloth = Cloth.objects.get(cloth_slug=cloth_slug) size_temp = Size_Variant.objects.get(size=size, cloth=cloth) flag = True for cart_obj in cart: c_id = cart_obj.get('cloth') size_short = cart_obj.get('size') if c_id == cloth.id and size == size_short: cart_obj['quantity'] = cart_obj['quantity'] + 1 flag = False if flag: cart_obj = {'cloth': cloth.id, 'size': size, 'quantity': 1} cart.append(cart_obj) if user != None: existing = Cart.objects.filter(user=user, sizeVariant=size_temp) if len(existing) > 0: obj = existing[0] obj.quantity = obj.quantity + 1 obj.save() else: c = Cart() c.user = user c.sizeVariant = size_temp c.quantity = 1 c.save() request.session['cart'] = cart return_url = request.GET.get('return_url') return redirect(return_url)
def login(request): if (request.method == 'GET'): form = CustomerAuthForm() next_page = request.GET.get('next') if next_page is not None: request.session['next_page'] = next_page context = {"form": form} return render(request, template_name="store/login.html", context=context) else: print("reqested data", request.POST) form = CustomerAuthForm(data=request.POST) if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = authenticate(username=username, password=password) if user: loginUser(request, user) session_cart = request.session.get('cart') print("checking cart data ->", session_cart) if session_cart is None: session_cart = [] else: for c in session_cart: size = c.get('size') quantity = c.get('quantity') tshirt_id = c.get('tshirt') cart_obj = Cart() cart_obj.sizeVariant = SizeVariant.objects.get( size=size, tshirt=tshirt_id) cart_obj.quantity = quantity cart_obj.user = user cart_obj.save() cart = Cart.objects.filter(user=user) print("cart data", cart) for c in cart: obj = { 'size': c.sizeVariant.size, 'tshirt': c.sizeVariant.tshirt.id, 'quantity': c.quantity } session_cart.append(obj) request.session['cart'] = session_cart print("Session data", request.session) next_page = request.session.get('next_page') print("POST next page is ", next_page) if next_page is None: next_page = "home" return redirect(next_page) else: context = {"form": form} return render(request, template_name="store/login.html", context=context)