Example #1
0
def get_user_by_username_handler(event, *args):
    from functions.db.connector import get_user_by_username

    username = event.get('pathParameters').get('username')
    user = get_user_by_username(username)

    return make_response(200, user.to_son().to_dict())
Example #2
0
def delete_user_handler(event, *args):
    from functions.db.connector import delete_user, get_user_by_username

    username = event.get('pathParameters').get('username')

    user_to_delete = get_user_by_username(username)
    delete_user(user_to_delete)

    return make_response(200, dict())
Example #3
0
def logout_handler(event, *args):
    from functions.authentication import check_for_token, get_username_from_jwt
    from functions.db.connector import remove_jwt_from_session, get_user_by_username

    headers = event["headers"]
    token = headers.get('authorizationToken')

    if check_for_token(token):
        username = get_username_from_jwt(token)
        user = get_user_by_username(username)
        remove_jwt_from_session(user)
        return make_response(200, "Successfully logged out")
    else:
        return make_response(401, "Authentication failed")
Example #4
0
def update_user_handler(event, *args):
    from functions.db.connector import update_user, get_user_by_username

    body = json.loads(event["body"])
    username = body.get('username')
    name = body.get('name')
    surname = body.get('surname')
    email = body.get('email')
    password = body.get('password')

    user = get_user_by_username(username)
    updated_user = update_user(user, name, surname, email, password)

    return make_response(200, updated_user.to_son().to_dict())
Example #5
0
def add_api_key_to_user_handler(event, *args):
    from functions.db.connector import add_api_key_to_user, get_user_by_username
    from functions.authentication import get_username_from_jwt
    headers = event["headers"]
    token = headers.get('authorizationToken')
    user = get_user_by_username(get_username_from_jwt(token))

    body = json.loads(event["body"])

    api_key = body.get('db_name')
    db_name = body.get('api_key')

    add_api_key_to_user(user, body)

    return make_response(201, dict())
Example #6
0
def get_reviews_for_user(event, *args):
    """Handles requests to get all reviews a user is part of

    Args:
        url: users/{username}/reviews

    Returns:
        list of reviews
    """
    username = event.get('pathParameters').get('username')
    user = connector.get_user_by_username(username)

    reviews = connector.get_reviews(user)

    resp_body = {"reviews": reviews}
    return make_response(status_code=201, body=resp_body)
Example #7
0
def login_handler(event, *args):
    from functions.db.connector import get_user_by_username, check_if_password_is_correct, add_jwt_to_session
    from functions.authentication import get_jwt_for_user

    body = json.loads(event["body"])
    username = body.get('username')
    password = body.get('password')
    user = get_user_by_username(username)
    password_correct = check_if_password_is_correct(user, password)

    if password_correct:
        token = get_jwt_for_user(user)
        add_jwt_to_session(user, token)
        return make_response(200, token)
    else:
        return make_response(401, "Authentication failed")
Example #8
0
def add_review(event, *args):
    """POST Method: create a new review
        "name" is mandatory in body
    """
    from functions.db.connector import add_review

    body = json.loads(event["body"])

    owner_name = body.get('owner_name')
    owner = connector.get_user_by_username(owner_name)

    name = body.get('name')
    description = body.get('description')

    review = add_review(name, description, owner=owner)

    return make_response(201, review.to_son().to_dict())
Example #9
0
def add_collaborator_to_review(event, *args):
    """Handles requests to add collaborators to a review

    Args:
        url: review/{review_id}/collaborator?username

    Returns:
        updated review
    """
    review_id = event.get('pathParameters').get('review_id')
    review = connector.get_review_by_id(review_id)

    username = event.get('queryStringParameters').get('username')
    user = connector.get_user_by_username(username)

    updated_result = connector.add_collaborator_to_review(review, user)

    resp_body = {"updated_result": updated_result.to_son().to_dict()}
    return make_response(status_code=201, body=resp_body)