def add_to_cart(request): serializer = CartAddSerializer(data=request.data) if serializer.is_valid(): user_id = get_current_user(request) serializer.save(user_id=user_id) return Response(get_response_code("added_to_cart")) return Response(serializer.data)
def post(self, request): user_id = get_current_user(request) serializer = OrderSerializer(data=request.data) if serializer.is_valid(): serializer.save(user_id=user_id) return Response(get_response_code('order_placed')) return Response(serializer.errors)
def add_to_wishlist(request): serializer = WishListSerializer(data=request.data) if serializer.is_valid(): user_id = get_current_user(request) serializer.save(user_id= user_id) return Response(get_response_code('added_to_wishlist')) return Response(serializer.errors)
def get(self, request): user_id = get_current_user(request) orders = OrderModel.objects.filter(user_id) serializer = ResponseSerializer(orders, many=True) total = sum([ item.price if item.quantity == 1 else item.price * item.quantity for item in orders ]) return Response({'order': serializer.data, 'total': total})
def logout(request): user_id = get_current_user(request) redis_instance = get_redis_instance() try: redis_instance.delete(user_id) except DataError: raise BookStoreError(get_response_code("login_required")) except Exception: return Response(get_response_code("generic_response")) return Response(get_response_code("logout"))
def get_view(request): user_id = get_current_user(request) items = CartModel.objects.all(user_id) total = sum( [ 0 if type(item.quantity) == str else item.price * item.quantity for item in items ] ) serializer = CartSerializer(items, many=True) return Response({"cart": serializer.data, "total": total})
def get(self, request, id=None): user_id = get_current_user(request) if id: try: wishlist = WishListModel.objects.get(id, user_id) products = Product.objects.get(str(wishlist.product_id)) except IndexError: raise BookStoreError(get_response_code('invalid_product_id')) else: wishlist = WishListModel.objects.all(params=user_id) products = Product.objects.filter(wishlist) serializer = ProductSerializer(products, many = True) return Response(serializer.data)
def order(request): user_id = get_current_user(request) address = request.data["address"] items = CartModel.objects.all(user_id) if len(items) == 0: raise BookStoreError(get_response_code("item_not_in_cart")) total = sum( [ 0 if type(item.quantity) == str else item.price * item.quantity for item in items ] ) id = get_latest_order_id() response = OrderManager.insert(items, total, address, id) return Response(response)
def get(self, request, id=None): user_id = get_current_user(request) if id: try: items = CartModel.objects.get(id, user_id) except IndexError: return Response(get_response_code("invalid_product_id")) except Exception: return Response(get_response_code("generic_response")) total = sum( [ 0 if type(item.quantity) == str else item.price * item.quantity for item in items ] ) serializer = CartSerializer(items, many=True) return Response({"cart": serializer.data, "total": total})
def delete(self, request, id=None): user_id = get_current_user(request) result = CartModel.objects.delete(id, user_id) if result == 0: return Response(get_response_code("item_not_in_cart")) return Response(get_response_code("removed_cart_item"))
def get_view(request): user_id = get_current_user(request) wishlist = WishListModel.objects.all(params=user_id) products = Product.objects.filter(wishlist) serializer = ProductSerializer(products, many = True) return Response(serializer.data)
def delete(self, request, id): user_id = get_current_user(request) result = WishListModel.objects.delete(id, user_id) if result == 0: return Response(get_response_code('wishlist_delete_does_exists')) return Response(get_response_code('deleted_wishlist_item'))