Пример #1
0
def show_users_cart(cart_id):
    try:
        cart = Cart.get(id=cart_id)
        print(cart.food_items)

        # throws an exception if the user is not the user of the cart
        try:
            if not cart.user_is_owner(current_user.id):
                raise ResourceAccessDenied()
        except ResourceAccessDenied as e:
            return e.get_json_response()

        cart_dict = model_to_dict(cart)
        del cart_dict['user']['password']

        return jsonify(data=cart_dict,
                       status={
                           'code': 200,
                           'message': 'Successfully found resource.'
                       })

    except DoesNotExist:
        return jsonify(data={},
                       status={
                           'code': 404,
                           'message': 'Resource does not exist.'
                       })
Пример #2
0
def delete_users_cart(cart_id):
    try:
        cart = Cart.get(id=cart_id)

        # throws an exception if the user is not the user of the queried cart
        try:
            if not cart.user_is_owner(current_user.id):
                raise ResourceAccessDenied()
        except ResourceAccessDenied as e:
            return e.get_json_response()

        # deletes the cart the user is verified as the carts user
        cart.delete_instance()

        return jsonify(data={},
                       status={
                           'code': 204,
                           'message': 'Resource deleted successfully.'
                       })
    except DoesNotExist:
        return jsonify(data={},
                       status={
                           'code': 404,
                           'message': 'Resource does not exist.'
                       })
def create_food_item():
    data = request.get_json()

    # tries to get the users cart, exception thrown if users cart doesnt exist
    try:
        cart = Cart.get(user=current_user.id)
    except DoesNotExist:
        return jsonify(
            data={},
            status={
                'code': 404,
                'message': 'Resource does not exist.'
            }
        )

    # adds the users cart to the dictionary because FoodItem needs a cart id to be created
    data['cart'] = cart.id

    food_item = FoodItem.create(**data)    
    food_item_dict = model_to_dict(food_item)

    return jsonify(
        data=food_item_dict,
        status={
            'code': 201,
            'message': 'Successfully created resource.'
        }
    )