Exemple #1
0
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
Exemple #2
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
Exemple #3
0
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
Exemple #4
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
Exemple #5
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