def test_too_big_quantity(self): 'Is AddToCartForm works with not correct quantity value' form = AddToCartForm({'quantity': 15}, cart=self.cart, product=stock_product) self.assertFalse(form.is_valid()) self.assertFalse(self.cart)
def post(self, request): add_to_cart_form = AddToCartForm(request.POST) if add_to_cart_form.is_valid(): add_item_to_cart(request.cart, get_object_or_404(Item, pk=add_to_cart_form.cleaned_data['item_id']), add_to_cart_form.cleaned_data['amount']) return redirect('item_list')
def add_to_cart_old(request): cart, created = CartModel.objects.get_or_create(user=request.user) context = {'success': False} logger.debug("ajax-add-to-cart") if request.method == 'POST': postdata = request.POST.copy() form = AddToCartForm(postdata) if form.is_valid(): product = form.cleaned_data['product'] result, cart = cart_service.add_to_cart(cart, product) if result: context['success'] = True context['status'] = True #return redirect(product.get_absolute_url()) else: context['error'] = 'Form is invalid' context['status'] = False messages.error(request, message="Invalid Form") #if request.is_ajax(): # return JsonResponse(context,status=HTTPStatus.BAD_REQUEST) else: context['error'] = 'Bad Request' context['status'] = False return redirect('catalog:home')
def test_quantity(self): 'Is AddToCartForm works with correct quantity value on empty cart' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) self.assertFalse(self.cart) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 5, 'Bad quantity')
def test_clean_quantity_product(self): 'Is AddToCartForm works with not stocked product' cart = Cart() self.post['quantity'] = 10000 form = AddToCartForm(self.post, cart=cart, product=digital_product) self.assertTrue(form.is_valid(), 'Form doesn\'t valitate') self.assertFalse(cart, 'Cart isn\'t empty') form.save() self.assertTrue(cart, 'Cart is empty')
def test_max_quantity(self): 'Is AddToCartForm works with correct product stock value' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 10, '%s is the bad quantity value' % (product_quantity, ))
def add_to_cart(): if request.form: try: product = get_or_404(Product, request.form['product_id']) except KeyError: return redirect(request.referrer) shoppingcart = ShoppingCart.for_session_cart(request.cart) form = AddToCartForm(request.form, product=product, cart=shoppingcart) if form.validate(): flash('Added {0} {1} to the cart'.format( form.quantity.data, product.name)) form.save() return redirect(url_for('.index')) return redirect(request.referrer)
def add_to_cart(request): order = get_order(request) if not order: order = Order.objects.create() request.session['order_id'] = order.id form = AddToCartForm(request.POST) if form.is_valid(): product = form.cleaned_data['product'] order_position, created = OrderPosition.objects.get_or_create( product=product, order=order, ) order_position.count += 1 order_position.save() print(form.errors) return redirect('cart')
def add_product(request, product_id): product = Product.objects.filter(pk=product_id) if product.exists(): cart = Cart(request) form = AddToCartForm(request.POST) if form.is_valid(): cd = form.cleaned_data cart.add(product=product[0], quantity=cd['quantity'], is_update=cd['is_update']) print(cart.cart.values()) url = request.META['HTTP_REFERER'] return redirect(url)
def test_max_quantity(self): 'Is AddToCartForm works with correct product stock value' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 10, '%s is the bad quantity value' % (product_quantity,))
def product_detail(request): error = False if 'itemName' in request.GET: itemName = request.GET['itemName'] if not itemName: error = True else: items = Item.objects.filter(name__icontains=itemName) add_cart_form = AddToCartForm() context = { 'items': items, 'add_cart_form': add_cart_form, } return render(request, 'products/product_detail.html', context) else: return render(request, 'products/search_result.html', {'error': error})
def book_info(request, book_name, slug): # Attempt to retrieve the book requested based on the provided # name and slug book = get_object_or_404(Book, book_name=book_name, slug=slug) # The form for Adding a product To the Cart (Add To Cart = ATC) ATC_product_form = AddToCartForm() # WISHLIST CODE: Gets the lists that the user has. myLists = [] if request.user.is_authenticated: myLists = getLists(request) # If we retrieved the book successfully, get its author # so we can reference their attributes in the HTML page if book: author_name = book.book_author author = get_object_or_404(Author, author_name=author_name) if request.user.is_authenticated: try: User = get_object_or_404(Profile, user=request.user) purchase = Purchase.objects.filter( book=book, User=User, has_purchased=True) #Check if user has purchased book if purchase: #If purchase exists, pass it through and let user leave a review return render( request, 'bookDetails/book/detail.html', { 'book': book, 'author': author, 'ATC_book_form': ATC_product_form, 'myLists': myLists, 'purchase': purchase }) except Purchase.DoesNotExist: #If doesn't exist, don't pass it purchase = None return render( request, 'bookDetails/book/detail.html', { 'book': book, 'author': author, 'ATC_book_form': ATC_product_form, 'myLists': myLists })
def render(self, context): if self.initial: initial = template.Variable(self.initial).resolve(context) else: initial = {} if self.single: single = template.Variable(self.single).resolve(context) else: single = False instance = self.instance.resolve(context) initial['product_type'] = ContentType.objects.get_for_model( instance).id initial['product_id'] = instance.id context[self.varname] = AddToCartForm(initial=initial, single=single) return ''
def cart_detail(request): # 장바구니에 담겨 있는 제품 목록 띄우기, 제품 수량 수정, 지우기, 장바구니 비우기 버튼 구현 cart = Cart(request) for item in cart: item['quantity_form'] = AddToCartForm(initial={ 'quantity': item['quantity'], 'is_update': True }) continue_url = '/' # 현재 페이지 주소 얻기 # 1) request.build_absolute_uri('?') : 쿼리스트링 없이 # 2) request.build_absolute_uri() : 쿼리스트링까지 얻어오기 current_url = request.build_absolute_uri('?') if 'HTTP_REFERER' in request.META and current_url != request.META[ 'HTTP_REFERER']: continue_url = request.META['HTTP_REFERER'] return render(request, 'cart/cart_detail.html', { 'cart': cart, 'continue_url': continue_url })
def movie_detail(request, id, slug): movie = get_object_or_404(Movie, id=id, slug=slug) form=AddToCartForm() context = {'movie': movie, 'form':form} return render(request, 'movie_detail.html', context)
def get_context_data(self, **kwargs): form = AddToCartForm() kwargs.update({'form': form}) return super().get_context_data(**kwargs)