def get(self, request): """ Cart detail view """ cart = get_cart(request) serializer = serializers.CartSerializer(cart) return Response(serializer.data)
def dispatch(self, request, *args, **kwargs): self.cart = get_cart(self.request) if not self.cart: return redirect('cart:index') self.forms = self.get_order_forms() return super().dispatch(request, *args, **kwargs)
def get_object(self, id): """ Cart update view """ try: cart = get_cart(self.request) return cart.items.get(pk=id) except CartItem.DoesNotExist: raise Http404
def cart_counter(context): """ Return total count of items in bag. :param context: context :return: number """ request = context.get('request') cart = get_cart(request) qty = None if cart: qty = cart.get_total_quantity_of_items() return {'cart_items_total_qty': qty or 0}
def test_resolve_cart_for_logged_in_user(self): session = self.client.session session['user_cart'] = 'testing_session' request = self.client.get(reverse('cart:index')) request.user = self._create_testing_user() request.session = session testing_cart = self._create_testing_cart() testing_cart.session_key = request.session['user_cart'] testing_cart.user = request.user testing_cart.save() cart = get_cart(request) self.assertEqual(request.status_code, 200) self.assertEqual(cart, testing_cart)
def test_access_checkout_url_with_empty_cart(self): """ Test when accessing /checkout url without items in cart will redirect back to cart """ session = self.client.session request = self.client.get(reverse('checkout:index')) request.session = session request.user = self.anonymous_user cart = get_cart(request) self.assertEqual(cart, None, "Cart should not exists") self.assertRedirects( request, reverse('cart:index'), 302, 200, "It should be redirected to cart index." )
def test_amending_quantity_on_existing_item(self): session = self.client.session request = self.client.post(reverse( 'cart:add', kwargs={'product_id': self.test_product.id}), data={'quantity': 1}, follow=True) request.session = session request.user = AnonymousUser() quantity = 1 cart = get_cart(request) cart_item, cart_item_created = CartItem.objects.update_or_create( cart=cart, product=self.test_product) if cart_item_created == False: cart_item.quantity += quantity cart_item.save() self.assertEqual(cart_item.quantity, 2)
def form_valid(self, form): cart = get_cart(self.request, create=True) user = authenticate(email=self.request.POST['email'], password=self.request.POST['password']) if user is not None and user.is_active: self.request.session[ 'user_cart'] = self.request.session.session_key login(self.request, user) if cart is not None: cart.user = Profile.objects.get(id=user.id) cart.save() messages.add_message(self.request, messages.SUCCESS, 'You were successfully logged in.') return super(AuthenticationForm, self).form_valid(form) else: response = super(AuthenticationForm, self).form_invalid(form) messages.add_message(self.request, messages.WARNING, 'Wrong email or password. Please try again') return response