Example #1
0
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)
Example #2
0
 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)
Example #3
0
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)
Example #4
0
 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})
Example #5
0
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"))
Example #6
0
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})
Example #7
0
 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)
Example #8
0
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)
Example #9
0
 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})
Example #10
0
 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"))
Example #11
0
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)
Example #12
0
 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'))