Exemplo n.º 1
0
def post(current_user, bucket_id):
    """
    Storing an item into a Bucket
    :param current_user: User
    :param bucket_id: Bucket Id
    :return: Http Response
    """
    if not request.content_type == 'application/json':
        return response('failed', 'Content-type must be application/json', 401)

    data = request.get_json()
    item_name = data.get('name')
    if not item_name:
        return response('failed', 'No name or value attribute found', 401)

    # Get the user Bucket
    bucket = get_user_bucket(current_user, bucket_id)
    if bucket is None:
        return response('failed', 'User has no Bucket with Id ' + bucket_id,
                        202)

    # Save the Bucket Item into the Database
    item = BucketItem(item_name.lower(), data.get('description', None),
                      bucket.id)
    item.save()
    return response_with_bucket_item('success', item, 200)
Exemplo n.º 2
0
def get_item(current_user, bucket_id, item_id):
    """
    An item can be returned from the Bucket if the item and Bucket exist and below to the user.
    The Bucket and Item Ids must be valid.
    :param current_user: User
    :param bucket_id: Bucket Id
    :param item_id: Item Id
    :return:
    """
    # Check item id is an integer
    try:
        int(item_id)
    except ValueError:
        return response('failed', 'Provide a valid item Id', 202)

    # Get the user Bucket
    bucket = get_user_bucket(current_user, bucket_id)
    if bucket is None:
        return response('failed', 'User has no Bucket with Id ' + bucket_id, 202)

    # Delete the item from the bucket
    item = bucket.items.filter_by(id=item_id).first()
    if not item:
        abort(404)
    return response_with_bucket_item('success', item, 200)
Exemplo n.º 3
0
def edit_item(current_user, bucket_id, item_id):
    """
    Edit an item with a valid Id. The request content-type must be json and also the Bucket
    in which the item belongs must be among the user`s Buckets.
    The name of the item must be present in the payload but the description is optional.
    :param current_user: User
    :param bucket_id: Bucket Id
    :param item_id: Item Id
    :return: Response of Edit Item
    """
    if not request.content_type == 'application/json':
        return response('failed', 'Content-type must be application/json', 401)

    try:
        int(item_id)
    except ValueError:
        return response('failed', 'Provide a valid item Id', 202)

    # Get the user Bucket
    bucket = get_user_bucket(current_user, bucket_id)
    if bucket is None:
        return response('failed', 'User has no Bucket with Id ' + bucket_id,
                        202)

    # Get the item
    item = bucket.items.filter_by(id=item_id).first()
    if not item:
        abort(404)

    # Check for Json data
    request_json_data = request.get_json()
    item_new_name = request_json_data.get('name')
    item_new_description = request_json_data.get('description', None)

    if not request_json_data:
        return response('failed', 'No attributes specified in the request',
                        401)

    if not item_new_name:
        return response('failed', 'No name or value attribute found', 401)

    # Update the item record
    item.update(item_new_name, item_new_description)
    return response_with_bucket_item('success', item, 200)