def post(self, request): serializer = CartSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def check_add_to_cart(self, response, url): self.assertEqual(response.status_code, status.HTTP_201_CREATED) cart = self.user.cart_set.first() self.assertEqual( response.data, CartSerializer(cart, context=self.get_context(url)).data) self.assertEqual(cart.products.count(), 1)
def test_list_not_exists_cart(self): url = reverse('api:cart-list') self.client.force_login(user=self.user) response = self.client.get(url) carts = Cart.objects.filter(user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(carts.exists()) self.assertEqual(response.data, CartSerializer(carts.first()).data)
def test_list_exists_cart(self): cart = Cart.objects.create(user=self.user) self.client.force_login(user=self.user) url = reverse('api:cart-list') response = self.client.get(url) carts = Cart.objects.filter(user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(carts.count(), 1) self.assertEqual(response.data, CartSerializer(cart).data)
def get(self, request: Request): cart_filters = {} if request.user.is_anonymous: cart_filters['user_agent'] = request.META.get('HTTP_USER_AGENT') cart_filters['for_anonymous_user'] = True else: cart_filters['user'] = request.user cart = get_user_cart(**cart_filters) cart_serialized = CartSerializer(cart) return Response(data=cart_serialized.data)
def cart_detail(request, pk): try: cart_item = Cart.objects.get(id=pk) except Cart.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = CartSerializer(cart_item) return Response(serializer.data) if request.method == 'DELETE': cart_item.delete() return Response({'deleted': True})
def cart_list(request): if request.method == 'GET': cart_list = Cart.objects.all() serializer = CartSerializer(cart_list, many=True) return Response(serializer.data) if request.method == 'POST': serializer = CartSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def cart_list(request, pk=None): if request.method == 'GET': cart_list = Cart.objects.all() serializer = CartSerializer(cart_list, many=True) return Response(serializer.data) if request.method == 'POST': serializer = CartSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) if request.method == 'DELETE': if pk is None: return Response('error no pk matched') cart_item=Cart.objects.get(id=pk) cart_item.delete() return Response('DELETED')
def cart_list(request, pk=None): if request.method == 'GET': cart_objects = Cart.objects.filter(user=request.user) # product_list = [] # for i in cart_list: # product_list.append(Product.objects.get(id=i.product.id)) serializer = CartSerializer(cart_objects, many=True) return Response(serializer.data) if request.method == 'POST': serializer = CartSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) if request.method == 'DELETE': if pk is None: return Response('Nothing to delete') cart_item = Cart.objects.get(id=pk) cart_item.delete() return Response('item was deleted')
def favorite_detail(request, pk): try: favorite_item = Favorite.objects.get(id=pk) except Cart.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = CartSerializer(favorite_item) return Response(serializer.data) if request.method == 'PUT': serializer = FavoriteSerializer(instance=favorite_item, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) if request.method == 'DELETE': favorite_item.delete() return Response({'deleted': True})
def get(self, request): products = Cart.objects.all() serializer = CartSerializer(products, many=True) return Response(serializer.data)