def _create_user(self, phone_number, password, **extra_fields): """ Creates and save a User with the phone number and password """ if not phone_number: raise ValueError("Phone number is missing") user = self.model(phone_number=phone_number, **extra_fields) user.set_password(password) user.save(using=self._db) # Create a Profile object for the user profile = Profile(user=user) profile.save() # Create a Cart object for the user cart = Cart(user=user) cart.save() # Create a UserAddress object for the user address = UserAddress(user=user) address.save() return user
def cart(request): txt = "" # Find cart in DB session_id = request.session.session_key if not session_id: request.session.create() session_id = request.session.session_key try: cart = Cart.objects.get(session_id=session_id) except Cart.DoesNotExist: cart = Cart() cart.session_id = session_id cart.save() # Find items of the cart items = Items.objects.filter(cart=cart) for item in items: txt += item.product.slug + ": " + str(item.quantity) + "<br>" context = { 'items': items, 'categories': Category.objects.all().order_by('name'), } return render(request, 'store/cart.html', context)
def add_to_cart(request, product_id): # Open cart or Create cart session_id = request.session.session_key if not session_id: request.session.create() session_id = request.session.session_key try: cart = Cart.objects.get(session_id=session_id) except Cart.DoesNotExist: cart = Cart() cart.session_id = session_id cart.save() # get item from DB product = Product.objects.get(id=product_id) # add item to cart OR increase quantity if already in cart try: item = Items.objects.get(product=product, cart=cart) item.quantity += 1 item.save() except Items.DoesNotExist: item = Items() item.cart = cart item.quantity = 1 item.product = product item.save() # redirect to cart return redirect('/store/cart')
def decqty(request, id): assert isinstance(request, HttpRequest) product = Product.objects.filter(id=id).first() cart = Cart(request=request) cart.add(product, -1) return redirect('store:cart')
def removeitem(request, id): assert isinstance(request, HttpRequest) cart = Cart(request=request) product = Product.objects.filter(id=id).first() cart.remove(product) return redirect('store:cart')
def set_product_count_in_cart(product: models.Product, cart: models.Cart, count: int) -> Optional[models.Cart]: if count == 0: return remove_products_from_cart([product], cart) relation = models.ProductCartRelation.objects.get(product=product, cart=cart) relation.count = count relation.save() cart.refresh_from_db() return cart
def addtocart(request, id): assert isinstance(request, HttpRequest) product = Product.objects.filter(id=id).first() if not product: return HttpResponseNotFound() cart = Cart(request=request) cart.add(product) return redirect('store:cart')
def add_products_to_cart(products: List[models.Product], cart: models.Cart) -> models.Cart: for product in products: relation, is_created = models.ProductCartRelation.objects.get_or_create( product=product, cart=cart) if not is_created: relation.count += 1 relation.save() cart.refresh_from_db() return cart
def remove_products_from_cart(products: List[models.Product], cart: models.Cart) -> Optional[models.Cart]: for product in products: try: relation = models.ProductCartRelation.objects.get(product=product, cart=cart) except models.ProductCartRelation.DoesNotExist: raise ValidationError(f'Product {product.slug} is not in the cart', code=400) else: relation.delete() # relation.save() cart.refresh_from_db() return cart
def get(self, request): customer = Customer.get_customer_by_email(request.user.email) cart_size = Cart.get_all_products_by_user(request.user.id) return render(request, 'profile.html', { 'customer': customer, 'cart_size': len(cart_size) })
def get(self, request): product_id = request.GET['product_id'] product = Product.get_product_by_id(product_id) cart_size = Cart.get_all_products_by_user(request.user.id) return render(request, 'product_details.html', { 'product': product, 'cart_size': len(cart_size) })
def add_to_cart(request, slug): user = None item_id = None if request.user.is_authenticated: user = request.user cart = request.session.get('cart') if cart is None: cart = [] item = Product.objects.get(slug=slug) flag = True for cart_obj in cart: item_id = cart_obj.get('item') if item_id == item.id: flag = False cart_obj['quentity'] = cart_obj['quentity'] + 1 if flag: cart_obj = {'item': item.id, 'quentity': 1} cart.append(cart_obj) if user is not None: product_obj = Product.objects.get(slug=slug) existing = Cart.objects.filter(user=user, product=product_obj) if len(existing) > 0: obj = existing[0] obj.quentity = obj.quentity + 1 obj.save() else: c = Cart() c.user = user c.product = Product.objects.get(slug=slug) c.quentity = 1 c.save() request.session['cart'] = cart return_url = request.GET.get('return_url') return redirect(return_url)
def order(request, access_code): # TODO: Zamówienia usera widoczne tylko dla niego (logowanie potrzebne) # TODO: Zamówienia bez usera po podaniu kodu (logowanie nie potrzebne) try: order = Order.objects.get(access_code=access_code) except Order.DoesNotExist: HttpResponse('Cant find this object') # Zamówione produkty ordered_products = Ordered.objects.filter(order=order) cart = Cart() # Dodanie produktów do klasy (wrappera) - Cart for p in ordered_products: cart.add_to_cart(Product.objects.get(id=p.product.id), p.amount) context = {'order': order, 'cart': cart} return render(request, 'transactions/order.html', context)
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 order(request): assert isinstance(request, HttpRequest) if request.method == 'POST': form = OrderForm(user=request.user, data=request.POST) cart = Cart(request=request) if form.is_valid(): form.save(cart=cart) cart.clear() return redirect('store:ordercreated') return render(request, 'store/order.html', { 'title': 'Dane do wysylki', 'form': form, }) else: form = OrderForm(user=request.user) return render(request, 'store/order.html', { 'title': 'Dane do wysylki', 'form': form, })
def post(self, request): size = request.POST.get('size1') product = request.POST.get('product') product_data = Product.get_product_by_id(product) if not size: messages.error(request, "Please select the size.") return render(request, 'product_details.html', {'product': product_data}) cart2 = Cart.get_cart_by_product(product_data, request.user.id, size) remove = request.POST.get('remove') if cart2: quantity2 = Cart.get_product_quantity_by_product( product_data, request.user.id, size) if quantity2: if remove: if quantity2 == 1: Cart.delete_by_product(product_data, request.user.id, size=size) return redirect('product_details') else: quantity2 = quantity2 - 1 else: quantity2 = quantity2 + 1 else: quantity2 = 1 Cart.update_product_quantity_in_cart(cart2, quantity2) else: user = User.objects.get(email=request.user.email) cart_object = Cart(user=user, product=product_data, quantity=1, size=size) cart_object.save() return redirect('cart')
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 setUp(self): self.product1 = Product.objects.create(name='product1', category='category1', description='', price=10.00) self.product2 = Product.objects.create(name='product2', category='category2', description='', price=15.00) self.product3 = Product.objects.create(name='product3', category='category1', description='', price=2.35) self.cart = Cart() self.cart.lines = [ CartLine(self.product1, 1), CartLine(self.product2, 2), CartLine(self.product3, 1), ]
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 login(request): if request.method == "GET": form = UserLoginForm() return render(request, template_name='store/login.html', context={'form': form}) else: form = UserLoginForm(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: userlogin(request, user) session_cart = request.session.get('cart') if session_cart is None: session_cart = [] else: for c in session_cart: item = c.get('item') quentity = c.get('quentity') cart_obj = Cart() cart_obj.product = Product.objects.get(id=item) cart_obj.quentity = quentity cart_obj.user = user cart_obj.save() cart = Cart.objects.filter(user=user) session_cart = [] for c in cart: obj = {'item': c.product.id, 'quentity': c.quentity} session_cart.append(obj) request.session['cart'] = session_cart return redirect("home") 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 itemsincart(request): assert isinstance(request, HttpRequest) cart = Cart(request=request) return JsonResponse(cart.count(), safe=False)
def test_create_new(self): prod = Product.objects.create(name="Milk", quantity=10, price=1) Cart.create_new(key='1', quantity=2, prod_id=prod.id) cart = Cart.objects.first() self.assertEqual(cart.products.first(), prod)
def create(self, request): cart = Cart.create() serializer = CartSerializer(cart) return Response(serializer.data, 201)
def test_Cart_add_product_to_cart(self): cart = Cart() cart.add_to_cart(self.product1, 1) self.assertEquals(len(cart.lines), 1)
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)
def test_create_new_modifies_product_quantity(self): prod = Product.objects.create(name="Milk", quantity=10, price=1) ordered_quantity = 3 Cart.create_new(key='1', quantity=ordered_quantity, prod_id=prod.id) cart = Cart.objects.first() self.assertEqual(cart.products.first().quantity, prod.quantity-ordered_quantity)
def save(self, key): return Cart.create_new(key=key, quantity=int(self.data['quantity']), prod_id=int(self.data['prod_id']))