コード例 #1
0
def update_collection(user, name, auth):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    if not is_user_logged_in(session, auth, user.display_name):
        return error_request_denied()

    collection = get_collection_by_name(session, name)

    if not collection:
        return error_not_found("Collection")

    body = bottle.request.json

    name_missing = "name" not in body
    description_missing = "description" not in body

    if name_missing or description_missing:
        bottle.response.status = 400
        return {"error": "Missing required parameters"}

    collection_name = body["name"]
    collection_description = body["description"]

    existing_collection = get_collection_by_name(session, collection_name)

    if existing_collection:
        return {"error": "A collection already exists with this name."}

    collection.name = collection_name
    collection.description = collection_description

    session.commit()
コード例 #2
0
def get_item_image(user, collection, item, image):
    user_name = user
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    collection_name = collection
    collection = get_collection_by_name(session, collection)

    if not collection:
        return error_not_found("Collection")

    if collection.user != user:
        return error_not_found("Image")

    item = get_item_by_name(session, user_name, collection_name, item)

    if not item:
        return error_not_found("Item")

    item_image = get_image_by_id(session, image)

    return {
        "caption": item_image.caption,
        "description": item_image.description
    }
コード例 #3
0
def add_collection(user, auth):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    if not is_user_logged_in(session, auth, user.display_name):
        return error_request_denied()

    body = bottle.request.json

    name_missing = "name" not in body
    description_missing = "description" not in body

    if name_missing or description_missing:
        bottle.response.status = 400
        return {"error": "Missing required parameters."}

    name = body["name"]
    description = body["description"]

    already_exists = get_collection_by_name(session, name)

    if already_exists:
        bottle.response.status = 400
        return {"error": "There is already a collection with that name."}

    new_collection = Collection(user=user, name=name, description=description)

    session.add(new_collection)
    session.commit()

    return {"id": new_collection.id}
コード例 #4
0
def delete_item(user, collection, item):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    collection = get_collection_by_name(session, collection)

    if not collection:
        return error_not_found("Collection")

    item = get_item_by_name(session, user.display_name, collection.name, item)

    if not item:
        return error_not_found("Item")

    session.delete(item)
コード例 #5
0
def delete_collection(user, collection, auth):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    if not is_user_logged_in(session, auth, user.display_name):
        return error_request_denied()

    collection_record = get_collection_by_name(session, collection)
    collection_not_found = not collection_record

    if collection_not_found:
        return error_not_found("Collection")

    session.delete(collection_record)
    session.commit()
コード例 #6
0
def get_collection(user, name, auth):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    if not is_user_logged_in(session, auth, user.display_name):
        return error_request_denied()

    collection = get_collection_by_name(session, name)

    if not collection:
        return error_not_found("Collection")

    return {
        "id": collection.id,
        "name": collection.name,
        "description": collection.description
    }
コード例 #7
0
def add_item(user, collection):
    user = get_user_by_name(session, user)

    if not user:
        return error_user_not_found()

    collection = get_collection_by_name(session, collection)

    if not collection:
        return error_not_found("Collection")

    body = bottle.request.json

    name_missing = "name" not in body
    description_missing = "description" not in body

    if name_missing or description_missing:
        bottle.response.status = 400
        return {"error": "Missing required parameters."}

    name = body["name"]
    description = body["description"]

    existing_item = get_item_by_name(session, user.display_name,
                                     collection.name, name)

    if existing_item:
        bottle.response.status = 400
        return {
            "error":
            "There is already an item in the collection with that name."
        }

    session.add(Item(collection=collection, name=name,
                     description=description))
    session.commit()