Esempio n. 1
0
def post():
    body = request.json
    required_parameters = [
        'first_name', 'last_name', 'email', 'username', 'password'
    ]
    if not all(x in body for x in required_parameters):
        return response.build(
            error=True,
            error_message='All request body parameters are required.')

    user = db_session().query(User).filter_by(
        username=body['username']).first()
    if user:
        return response.build(error=True,
                              error_message='The user already exists.')

    user = User(username=body['username'],
                first_name=body['first_name'],
                last_name=body['last_name'],
                email=body['email'],
                password=body['password'])
    db_session().add(user)
    db_session().commit()

    return response.build(error=False, response=user.username)
Esempio n. 2
0
def get(image_url):
    tags_list = imagga.extract_tags_url(image_url)
    if tags_list is None:
        return response.build(error=True, error_message='Invalid input image.')

    response_dict = {'total_calories': 0.0, 'ingredients': []}
    for ingredient in tags_list:
        calories_count = nutritionix.get_ingredient_calories(ingredient)
        response_dict['total_calories'] += calories_count
        response_dict['ingredients'].append({'calories': calories_count, 'ingredient': ingredient})
    return response.build(error=False, response=response_dict)
Esempio n. 3
0
def count_post():
    request_body = request.json
    if not check.exist('ingredients_list', request_body):
        return response.build(error=True, error_message='No ingredients specified.')

    response_dict = {'total_calories': 0.0, 'ingredients': []}
    try:
        for ingredient in request_body['ingredients_list']:
            calories_count = nutritionix.get_ingredient_calories(ingredient)
            response_dict['total_calories'] += calories_count
            response_dict['ingredients'].append({'calories': calories_count, 'ingredient': ingredient})
        return response.build(error=False, response=response_dict)
    except Exception as e:
        log.error('Error while counting calories.')
        log.exception(e)
        return response.build(error=True, error_message='Unexpected error.')
Esempio n. 4
0
def ingredients_post():
    request_body = request.json
    if not check.exist('image_url', request_body) and not check.exist('image_base64', request_body):
        return response.build(error=True, error_message='No specified image in the request body.')
    if check.exist('image_url', request_body) and check.exist('image_base64', request_body):
        return response.build(error=True, error_message='Both image_url and image_base64 specified.')

    if check.exist('image_url', request_body):
        tags_list = imagga.extract_tags_url(request_body['image_url'])
    else:
        tags_list = imagga.extract_tags_base64(request_body['image_base64'])

    if tags_list is not None:
        return response.build(error=False, response=tags_list)
    else:
        return response.build(error=True, error_message='Invalid input image.')
Esempio n. 5
0
def login_post():
    body = request.json
    required_parameters = ['username', 'password']
    if not all(x in body for x in required_parameters):
        return response.build(
            error=True,
            error_message='All request body parameters are required.')

    user = db_session().query(User).filter_by(
        username=body['username']).first()
    if not user:
        return response.build(
            error=True, error_message='There is no user with that username.')

    if user.password != body['password']:
        return response.build(error=True, error_message='Incorrect password.')
    else:
        return response.build(error=False, response=user.username)
Esempio n. 6
0
def get(user_id):
    try:
        week_date = datetime.datetime.today() - datetime.timedelta(days=7)
        reports = db_session().query(Report).filter(and_(Report.time >= week_date, Report.username == user_id)).all()

        response_dict = {}
        while week_date.date() < datetime.datetime.now().date():
            week_date = week_date + datetime.timedelta(days=1)
            response_dict[week_date.strftime("%Y-%m-%d")] = 0.0
        for report in reports:
            response_dict[report.time.strftime("%Y-%m-%d")] += float(report.calories)

        response_list = []
        for key, value in response_dict.items():
            response_list.append({'date': key, 'calories': int(value)})
        return response.build(error=False, response=response_list)
    except Exception as e:
        log.error('Exception while getting data for the dashboard.')
        log.exception(e)
        return response.build(error=True, error_message='Unexpected error.')
Esempio n. 7
0
def confirm_post():
    request_body = request.json
    if not check.exist('ingredients_list', request_body):
        return response.build(error=True, error_message='No ingredients specified.')
    if not check.exist('user_id', request_body):
        return response.build(error=True, error_message='No user specified.')
    for ingredient in request_body['ingredients_list']:
        if not check.exist('calories', ingredient) or not check.exist('ingredient', ingredient):
            return response.build(error=True, error_message='No calories or ingredient specified.')

    user = db_session().query(User).filter_by(username=request_body['user_id']).first()
    if not user:
        return response.build(error=True, error_message='No user found with the given username.')
    else:
        calories = sum([ingredient['calories'] for ingredient in request_body['ingredients_list']])
        report = Report(
            username=request_body['user_id'],
            calories=calories,
            time=datetime.datetime.now()
        )
        db_session().add(report)
        db_session().flush()
        if not report.id:
            return response.build(error=True, error_message='Error adding an ingredient.')

        for ing in request_body['ingredients_list']:
            ingredient = Ingredient(
                report_id=report.id,
                calories=ing['calories'],
                name=ing['ingredient']
            )
            db_session().add(ingredient)
            db_session().flush()
            if not ingredient.id:
                return response.build(error=True, error_message='Error adding an ingredient.')

        db_session().commit()
        return response.build(error=False, response=dict(report_id=report.id))
Esempio n. 8
0
def get(user_id):
    user = db_session().query(User).filter_by(username=user_id).first()
    if not user:
        return response.build(error=True,
                              response='There is no user with that username.')
    return response.build(error=False, response=user.serialize())