示例#1
0
def get_user_infos(db, user_id):
    resp = Response()
    user = User.query.filter_by(id=user_id).first()
    if user is None:
        resp.add_error('user_id', 'not found')
        return resp
    resp.message = {
        'gender': user.gender,
        'age': user.age,
        'weight': user.weight,
        'height': user.height,
        'lifestyle': user.lifestyle,
        'allergies': str_to_list(user.allergies)
    }
    return resp
示例#2
0
文件: auth.py 项目: kameholic/thesis
def login_user(db, args):
    resp = Response()
    email = args.get('email', None)
    password = args.get('password', None)
    user = User.query.filter_by(email=email).first()
    if user is None or not user.confirmed:
        resp.add_error('password', 'email or password does not match')
        return resp
    if check_password(user.password, password):
        access_token = create_access_token(identity=user.id)
        resp.message = {'access_token': access_token}
    else:
        resp.add_error('password', 'email or password does not match')
    return resp
示例#3
0
文件: auth.py 项目: kameholic/thesis
def register_user(db, args, confirm_required):
    print('confirm_required %s' % confirm_required)
    resp = Response()
    email = args.get('email', None)
    password = args.get('password', None)
    user = User.query.filter_by(email=email).first()
    if user is not None:
        resp.add_error('email', 'email is already taken')
        return resp
    user = User(email, hash_password(password))
    if confirm_required:
        try:
            send_email(email)
        except smtplib.SMTPRecipientsRefused:
            resp.add_error('email', '%s is not a valid address' % email)
            return resp
    else:
        user.confirmed = True
    db.session.add(user)
    db.session.commit()
    resp.message = 'Successfully registered, please confirm in your e-mail'
    return resp
示例#4
0
 def get(self):
     resp = Response()
     allergies = generator.get_allergies()
     resp.message = {'allergies': allergies}
     return create_response(resp)
示例#5
0
def save_user_infos(db, user_id, infos):
    resp = Response()
    user = User.query.filter_by(id=user_id).first()
    if user is None:
        resp.add_error('user_id', 'not found')
    if infos['age'] < 0:
        resp.add_error('age', 'cannot be negative')
    if infos['weight'] < 0:
        resp.add_error('weight', 'cannot be negative')
    if infos['height'] < 0:
        resp.add_error('height', 'cannot be negative')
    if not isinstance(infos['allergies'], list):
        resp.add_error('allergies', 'has to be a list of int')
    else:
        for val in infos['allergies']:
            if not isinstance(val, int):
                resp.add_error('allergies', 'has to be a list of int')
                break
    if not resp:
        return resp
    user.gender = infos['gender']
    user.age = infos['age']
    user.weight = infos['weight']
    user.height = infos['height']
    user.lifestyle = infos['lifestyle']
    user.allergies = list_to_str(infos['allergies'])
    db.session.add(user)
    db.session.commit()
    resp.message = 'User infos saved'
    return resp
示例#6
0
def load_diet(db, user_id):
    resp = Response()
    diet = Diet.query.filter_by(id=user_id).first()
    if diet is None:
        resp.message = {'diet': None}
        return resp

    is_complex = diet.days[0].is_complex
    days = []

    if is_complex:
        recipes = get_recipes()

        for day in diet.days:
            day_ = {'breakfast': [], 'lunch': [], 'dinner': []}
            for dine_type in ['breakfast', 'lunch', 'dinner']:
                recipe_list = str_to_list(getattr(day, dine_type))
                portion = getattr(day, '{}_portion'.format(dine_type))
                assert (len(recipe_list) == 1)

                for recipe_id in recipe_list:
                    recipe = next((x for x in recipes if x.id == recipe_id),
                                  None)
                    day_[dine_type] = [{
                        'id': recipe_id,
                        'name': recipe.name,
                        'portion': float(portion),
                        'description': recipe.description
                    }]
            days.append(day_)
    else:
        ingredients = get_ingredients()

        for day in diet.days:
            day_ = {'breakfast': [], 'lunch': [], 'dinner': []}
            for dine_type in ['breakfast', 'lunch', 'dinner']:
                ing_list = str_to_list(getattr(day, dine_type))
                portion_list = str_to_list(
                    getattr(day, '{}_portion'.format(dine_type)))
                assert (len(ing_list) == len(portion_list))

                for i in range(0, len(ing_list)):
                    ing_id = ing_list[i]
                    portion = portion_list[i]
                    ing = next(
                        (x for x in ingredients.values() if x.id == ing_id),
                        None)
                    day_[dine_type].append({
                        'id':
                        ing_id,
                        'name':
                        '{} grams or ml {}'.format(portion, ing.name),
                        '_portion':
                        portion,
                        'portion':
                        1,
                        'description':
                        ''
                    })
            days.append(day_)

    resp.message = {'diet': days}
    return resp
示例#7
0
def generate_diet(db, user_id, diet_type, goal, is_complex):
    resp = Response()

    user = User.query.filter_by(id=user_id).first()
    if user is None:
        resp.add_error('user_id', 'not found')
        return resp

    if user.gender is None:
        resp.add_error('gender', 'is not set')
    if user.age is None:
        resp.add_error('age', 'is not set')
    if user.weight is None:
        resp.add_error('weight', 'is not set')
    if user.height is None:
        resp.add_error('height', 'is not set')
    if user.lifestyle is None:
        resp.add_error('lifestyle', 'is not set')
    if len(resp.errors) > 0:
        return resp

    allergies = str_to_list(user.allergies)
    calories = 0
    W = user.weight
    H = user.height
    A = user.age

    mult = 0
    if user.lifestyle == 'sitting':
        mult = 1.2
    elif user.lifestyle == 'normal':
        mult = 1.3
    else:
        mult = 1.7

    if user.gender == 'male':
        calories = 66 + (13.7 * W) + (5.0 * H) - (6.8 * A)
    else:
        calories = 655 + (9.6 * W) + (1.8 * H) - (4.7 * A)
    calories = mult * calories
    if goal == 'lose':
        calories -= 300
    elif goal == 'gain':
        calories += 300

    r_calories = {
        'breakfast': 0.35 * calories,
        'lunch': 0.4 * calories,
        'dinner': 0.25 * calories
    }

    result = None
    if is_complex:
        result = generate_complex_diet(resp, r_calories, allergies)
    else:
        result = generate_simple_diet(resp, r_calories, allergies)

    if len(resp.errors) == 0:
        save_diet(db, user_id, result, is_complex)
        resp.message = {'diet': result}
    return resp