Exemple #1
0
def login(event, context):
    try:
        login_request = LoginRequestModel.from_request(
            json.loads(event['body']))
        user = ud.get_by_email(login_request.email)
        # this throws an exception if the passwords don't match
        hash_utils.check_password(login_request.password, user.password)
        return build_response_with_body(
            200, {
                'id': user.id,
                'first_name': user.first_name,
                'last_name': user.last_name,
                'email': user.email,
                'token': auth_utils.generate_auth_token(user.id).decode()
            })

    except errors.ApiError as e:
        print('boo')
        print(e)
        if e.api_error.issue == 'NOT_FOUND':
            print("wtffff")
            return errors.build_response_from_api_error(
                errors.ApiError(errors.invalid_user_name_or_password))
        else:
            return errors.build_response_from_api_error(e)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #2
0
def get_by_id(_id):
    try:
        logging.debug('getting quiz by id')
        response = table.get_item(
            Key={
                'id': _id
            }
        )
        logging.debug('got quiz by id')
        item = validation.validate_item_exists(response)
        logging.debug('got item')
        quiz = QuizModel.from_dynamo(item)
        logging.debug(quiz.to_dynamo_dict())
        logging.debug('getting questions for quiz')
        questions = question_dao.get_by_quiz_id_quietly(quiz.id)
        quiz.questions = questions
        return quiz

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #3
0
def login(event, context):
    try:
        login_request = LoginRequestModel.from_request(
            json.loads(event['body']))

        if login_request.email == '*****@*****.**' and login_request.password == 'password':
            return build_response_without_body(204)

        if login_request.email == '*****@*****.**' and login_request.password == 'n3tify_test_account':
            return build_response_without_body(204)

        return errors.build_response_from_api_error(
            errors.ApiError(errors.invalid_user_name_or_password))

    except errors.ApiError as e:
        print(e)
        if e.api_error.issue == 'NOT_FOUND':
            return errors.build_response_from_api_error(
                errors.ApiError(errors.invalid_user_name_or_password))
        else:
            return errors.build_response_from_api_error(e)

    except Exception as e:
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #4
0
def update(user):
    try:
        table.update_item(
            Key={
                'id': user.id
            },
            UpdateExpression='set #fn=:fn, #ln=:ln, #e=:e, #p=:p, #u=:u',
            ExpressionAttributeValues={
                ':fn': user.first_name,
                ':ln': user.last_name,
                ':e': user.email,
                ':p': user.phone_number,
                ':u': generate_timestamp()
            },
            ExpressionAttributeNames={
                '#fn': 'first_name',
                '#ln': 'last_name',
                '#e': 'email',
                '#p': 'phone_number',
                '#u': 'update_time'
            }
        )
        return get_by_id(user.id)
    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #5
0
def update(question):
    try:
        print('*$', question.to_dynamo_dict())
        table.update_item(Key={'id': question.id},
                          UpdateExpression='set #qt=:qt, #q=:q, #a=:a',
                          ExpressionAttributeValues={
                              ':qt':
                              question.question_type,
                              ':q':
                              question.question,
                              ':a': [
                                  answer.to_dynamo_dict()
                                  for answer in question.answers
                                  if question.answers
                              ]
                          },
                          ExpressionAttributeNames={
                              '#qt': 'question_type',
                              '#q': 'question',
                              '#a': 'answers'
                          })
        return get_by_id(question.id)
    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        if e.api_error.issue == 'MISSING_EXPECTED_ATTRIBUTE':
            raise errors.ApiError(errors.not_found, e)

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #6
0
def update(quiz):
    try:
        table.update_item(
            Key={
                'id': quiz.id
            },
            UpdateExpression='set #n=:n, #d=:d',
            ExpressionAttributeValues={
                ':n': quiz.name,
                ':d': quiz.description
            },
            ExpressionAttributeNames={
                '#n': 'name',
                '#d': 'description'
            }
        )
        return get_by_id(quiz.id)
    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #7
0
def validate_request_id_header_is_present(request):
    try:
        request_id = str(request.headers['Request-Id'])
        if len(request_id) < 1:
            raise errors.ApiError(errors.invalid_request_id)
        return request_id
    except KeyError:
        raise errors.ApiError(errors.invalid_request_id)
Exemple #8
0
def validate_authorization_header_is_present(headers):
    try:
        token = str(headers['Authorization'])
        if len(token) < 1:
            raise errors.ApiError(errors.invalid_auth_token)
        return token
    except KeyError:
        raise errors.ApiError(errors.invalid_auth_token)
Exemple #9
0
def check_password(password, password_hash):
    try:
        if not pbkdf2_sha256.verify(password, password_hash):
            raise errors.ApiError(errors.invalid_user_name_or_password)

    except Exception as e:
        print('unable to check password')
        raise errors.ApiError(errors.invalid_user_name_or_password, e)
Exemple #10
0
def delete(_id):
    print('flashcard id:', _id)
    try:
        table.delete_item(Key={'id': _id})

        print('deleted:', _id)

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #11
0
def delete(question_id):
    print('question_id:', question_id)
    try:
        table.delete_item(Key={'id': question_id})

        print('deleted:', question_id)

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error)

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #12
0
def decode_auth_token(auth_token):
    """
    Decodes the auth token
    :param auth_token:
    :return: integer|string
    """
    try:
        payload = jwt.decode(auth_token, __key)
        return payload['sub']
    except jwt.ExpiredSignatureError:
        raise errors.ApiError(errors.unauthorized)
    except jwt.InvalidTokenError:
        raise errors.ApiError(errors.unauthorized)
Exemple #13
0
def get_by_id(_id):
    try:
        response = table.get_item(Key={'id': _id})
        item = validation.validate_item_exists(response)
        return FlashcardModel.from_dynamo(item)

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception:
        raise errors.ApiError(errors.internal_server_error)
Exemple #14
0
def create(flashcard):
    try:
        _id = generate_id()
        flashcard.id = _id
        table.put_item(Item=flashcard.to_dynamo_dict())
        return get_by_id(_id)

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception:
        raise errors.ApiError(errors.internal_server_error)
Exemple #15
0
def register(event, context):
    try:
        logging.debug('register')
        user_request = UserModel.from_request(json.loads(event['body']))
        logging.debug('user model:')
        logging.debug(user_request)
        validation.validate_email(user_request.email)
        validate_user_does_not_exist(user_request.email)
        logging.debug('user does not exist, proceeding with creation')
        user = ud.create(user_request)
        res = build_response_with_body(
            200, {
                'id': user.id,
                'first_name': user.first_name,
                'last_name': user.last_name,
                'email': user.email,
                'token': auth_utils.generate_auth_token(user.id).decode()
            })
        return res

    except errors.ApiError as ae:
        return errors.build_response_from_api_error(ae)

    except Exception as e:
        logging.debug(e)
        logging.debug(str(e))
        return errors.build_response_from_api_error(
            errors.ApiError(errors.internal_server_error, e))
Exemple #16
0
def create(question):
    logging.debug('creating question')
    try:
        _id = generate_id()
        question.id = _id
        table.put_item(Item=question.to_dict())
        logging.debug('question created')
        return get_by_id(_id)

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception:
        raise errors.ApiError(errors.internal_server_error)
Exemple #17
0
def validate_user_exists_by_id(id):
    try:
        user = user_dao.get_by_id(id)
        print('user.id {}'.format(user.id))
    except errors.ApiError as e:
        if e.api_error.issue == 'NOT_FOUND':
            raise errors.ApiError(errors.invalid_user_id)
        return
Exemple #18
0
def validate_items_exist(response, quietly=False):
    try:
        return response['Items']
    except KeyError as e:
        print(str(e))
        if quietly:
            return []
        raise errors.ApiError(errors.not_found)
def delete(_id):
    try:
        flashcard_deck = get_by_id(_id)
        table.delete_item(Key={'id': _id})

        print('deleted:', _id)
        for flashcard in flashcard_deck.flashcards:
            flashcard_dao.delete(flashcard.id)

    except errors.ApiError as e:
        if e.api_error.issue == 'NOT_FOUND':
            return

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error)

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #20
0
def get_by_email(email):

    try:
        response = table.query(IndexName="email-index",
                               KeyConditionExpression=Key('email').eq(email))
        logging.debug('dynamo response: ')
        logging.debug(response)
        item = validation.validate_items_exist(response)[0]
        return UserModel.from_dynamo(item)
    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except Exception as e:
        if str(e) == 'list index out of range':
            print(e)
            raise errors.ApiError(errors.not_found)
        else:
            raise errors.ApiError(errors.internal_server_error, e)
Exemple #21
0
def validate_flashcard_deck_exists(flashcard_deck_id):
    try:
        flashcard_deck = flashcard_deck_dao.get_by_id(flashcard_deck_id)
        logging.debug('deck id {}'.format(flashcard_deck.id))
        return
    except errors.ApiError as e:
        if e.api_error.issue == 'NOT_FOUND':
            raise errors.ApiError(errors.invalid_flashcard_deck_id)
        return
Exemple #22
0
def validate_quiz_exists(quiz_id):
    try:
        quiz = quiz_dao.get_by_id(quiz_id)
        logging.debug('quiz id {}'.format(quiz.id))
        return
    except errors.ApiError as e:
        if e.api_error.issue == 'NOT_FOUND':
            raise errors.ApiError(errors.invalid_quiz_id)
        return
Exemple #23
0
def get_by_id(_id):
    try:
        logging.debug('retrieve user by id')
        response = table.get_item(Key={'id': _id})
        logging.debug('dynamo response: ', response)
        item = validation.validate_item_exists(response)
        return UserModel.from_dynamo(item)
    except ClientError as e:
        logging.debug(e)
        raise errors.ApiError(errors.internal_server_error)
Exemple #24
0
def create(user):
    logging.debug('creating user...')
    try:
        _id = generate_id()
        logging.debug('id generated')
        user.id = _id
        user.password = hash_password(user.password)
        logging.debug('hash generated')
        table.put_item(Item=user.to_dynamo_dict())
        logging.debug('success creating user')
        return get_by_id(_id)
    except ClientError as e:
        logging.debug(e)
        logging.debug(str(e))
        raise errors.ApiError(errors.internal_server_error)
    except Exception as e:
        logging.debug(e)
        logging.debug(str(e))
        raise errors.ApiError(errors.internal_server_error)
Exemple #25
0
def create(quiz):
    try:
        logging.debug('creating quiz')
        _id = generate_id()
        quiz.id = _id
        table.put_item(
            Item=quiz.to_dynamo_dict()
        )
        logging.debug('created quiz')
        return get_by_id(_id)

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #26
0
def get_by_flashcard_deck_id(flashcard_deck_id, quietly=False):
    try:
        response = table.query(IndexName="flashcard_deck_id-index",
                               KeyConditionExpression=Key(
                                   'flashcard_deck_id').eq(flashcard_deck_id))
        items = validation.validate_items_exist(response, quietly)
        flashcards = [
            FlashcardModel.from_dynamo(item) for item in items if items
        ]

        return flashcards

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error)
Exemple #27
0
def delete(message_id):
    try:
        message = get_by_id(message_id)
        table.delete_item(
            Key={
                'id': message_id
            }
        )

        print('deleted:', message.id)

    except errors.ApiError as e:
        if e.api_error.issue == 'NOT_FOUND':
            return

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error)

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #28
0
def get_all():
    try:
        response = table.scan()
        items = validation.validate_items_exist_quietly(response)
        quizzes = []
        for item in items:
            quiz = QuizModel.from_dynamo(item)
            questions = question_dao.get_by_quiz_id_quietly(quiz.id)
            quiz.questions = questions
            quizzes.append(quiz)
        return quizzes

    except ClientError:
        raise errors.ApiError(errors.internal_server_error)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        errors.ApiError(errors.internal_server_error, e)
Exemple #29
0
def create(message):
    try:
        logging.debug('creating message')
        _id = generate_id()
        message.id = _id
        message.timestamp = generate_timestamp()
        table.put_item(
            Item=message.to_dynamo_dict()
        )
        logging.debug('created message')
        return get_by_id(_id)

    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)
Exemple #30
0
def update(flashcard):
    try:
        table.update_item(Key={'id': flashcard.id},
                          UpdateExpression='set #q=:q, #a=:a',
                          ExpressionAttributeValues={
                              ':q': flashcard.question,
                              ':a': flashcard.answer
                          },
                          ExpressionAttributeNames={
                              '#q': 'question',
                              '#a': 'answer'
                          })
        return get_by_id(flashcard.id)
    except ClientError as e:
        raise errors.ApiError(errors.internal_server_error, e)

    except errors.ApiError as e:
        raise e

    except Exception as e:
        raise errors.ApiError(errors.internal_server_error, e)