예제 #1
0
def check_float(key, value):
    if isinstance(value, int):
        try:
            value = float(value)
        except:
            raise BadArgumentError(f"Parameter {key} must be a float")
    check(value is None or isinstance(value, float), f"Parameter {key} must be a float")
예제 #2
0
def check_barcode(key, value):
    if isinstance(value, str):
        try:
            value = int(value)
        except:
            raise BadArgumentError(f"Parameter {key} must be an integer or integer compatible")
    check(value is None or isinstance(value, int), f"Parameter {key} must be an integer")
예제 #3
0
def checked(obj, requirements, convert_missing_nut_to_zero=False):
    if not isinstance(obj, dict):
        raise BadArgumentError("Invalid arguments provided, JSON required")
    checkedArgs = {}

    for key, constraint in requirements.items():
        value = obj.get(key)

        for check in constraint:
            check(key, value)
        if value is not None:
            checkedArgs[key] = value

        elif convert_missing_nut_to_zero:
            checkedArgs[key] = 0.0

    return checkedArgs
예제 #4
0
def server_format(payload, upc):
    '''Converts Nutritionix API results to db mapping format.'''

    new_food = {}
    if 'foods' not in payload:
        raise BadArgumentError('invalid barcode provided')

    food = payload['foods'][0]
    div = food['serving_weight_grams'] if food[
        'serving_weight_grams'] else 100.0

    scale = 100.0 / div

    unit_res = {
        'serving_qty':
        food['serving_qty'],
        'serving_unit':
        food['serving_unit'],
        'grams_per_unit':
        food['serving_weight_grams']
        if food['serving_weight_grams'] else 100.0,
    }

    brand = food['brand_name'] if 'brand_name' in food else ''
    new_food['food_detail'] = {
        'food_desc':
        food['food_name'] + ', ' +
        brand if len(brand) > 0 else food['food_name'],
        'barcode':
        upc,
        'brand':
        brand,
    }
    new_food['image'] = food['photo']['thumb']
    new_food['nutrition'] = convert_to_nut_map(food['full_nutrients'], scale)
    new_food['food_units'] = get_food_units(unit_res)

    return new_food
예제 #5
0
def parsed_request():
    content = request.get_json()
    if content is None:
        raise BadArgumentError("Error on parsing arguments")
    return content
예제 #6
0
def check(condition, message):
    from daily_bites_app.errors import BadArgumentError

    if not condition:
        raise BadArgumentError(message)
예제 #7
0
 def get_meal_by_id(cls, meal_id):
     meal = cls.query.filter_by(id=meal_id).first()
     # do a check here for meal
     if meal is None:
         raise BadArgumentError("No such meal")
     return meal