Example #1
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}
Example #2
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()
Example #3
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()
Example #4
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()

    items = get_collection_items(session, name)

    return {
        'data': [{
            "id": item.id,
            "name": item.name,
            "description": item.description
        } for item in items]
    }
Example #5
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
    }
Example #6
0
def example(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()

    result = {}

    for collection in user.collections:
        result[collection.name] = {
            "id": collection.id,
            "name": collection.name,
            "description": collection.description
        }

    return result