Пример #1
0
def index():
    date = None
    time = None
    location = None
    errors = {}

    db = get_db()

    if request.method == 'POST':
        date = request.form.get('date')
        time = request.form.get('time')
        location = request.form.get('location')

        if not (date and time):
            if not date:
                errors['date'] = 'Date is required.'

            if not time:
                errors['time'] = 'Time is required.'
        else:
            date_time = datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M')

        if not location:
            errors['location'] = 'Location is required.'

        if not errors:
            db.execute('INSERT INTO appointment (user_id, date_time, location) VALUES (?, ?, ?)', [g.user['id'], date_time, location])
            db.commit()

            return redirect(url_for('.index'))

    appointments = db.execute('SELECT * FROM appointment WHERE user_id = ? ORDER BY date_time DESC', [g.user['id']]).fetchall()

    return render_template('appointment/index.html', date=date, time=time, location=location, errors=errors, appointments=appointments)
Пример #2
0
def login():
    email = None
    errors = {}

    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        db = get_db()

        if not email:
            errors['email'] = 'Email address is required.'
        else:
            user = db.execute('SELECT * FROM user WHERE email = ?',
                              [email.lower()]).fetchone()

            if user is None:
                errors['email'] = email + ' does not exist.'
            elif not check_password_hash(user['password'], password):
                errors['password'] = '******'

        if not errors:
            session.clear()
            session['user_id'] = user['id']

            return redirect(url_for('general.index'))

    return render_template('auth/login.html',
                           title='Log in',
                           email=email,
                           errors=errors)
Пример #3
0
def index():
    date = None
    description = None
    errors = {}

    db = get_db()

    if request.method == 'POST':
        date = request.form.get('date')
        description = request.form.get('description')

        if not date:
            errors['date'] = 'Date is required.'

        if not description:
            errors['description'] = 'Description is required.'

        if not errors:
            db.execute(
                'INSERT INTO history (user_id, date, description) VALUES (?, ?, ?)',
                [g.user['id'], date, description])
            db.commit()

    histories = db.execute(
        'SELECT * FROM history WHERE user_id = ? ORDER BY date DESC',
        [g.user['id']]).fetchall()

    return render_template('history/index.html',
                           date=date,
                           description=description,
                           errors=errors,
                           histories=histories)
Пример #4
0
def index():
    time = None
    medication = None
    quantity = None
    errors = {}

    db = get_db()

    if request.method == 'POST':
        time = request.form.get('time')
        medication = request.form.get('medication')
        quantity = request.form.get('quantity')

        if not time:
            errors['time'] = 'Time is required.'
        else:
            time = datetime.strptime(time, '%H:%M').time().isoformat()

        if not medication:
            errors['medication'] = 'Medication is required.'

        if not quantity:
            errors['quantity'] = 'Quantity is required.'

        if not errors:
            db.execute('INSERT INTO reminder (user_id, time, medication, quantity) VALUES (?, ?, ?, ?)', [g.user['id'], time, medication, quantity])
            db.commit()

            return redirect(url_for('.index'))

    reminders = db.execute('SELECT * FROM reminder WHERE user_id = ? ORDER BY time', [g.user['id']]).fetchall()

    return render_template('reminder/index.html', time=time, medication=medication, quantity=quantity, errors=errors, reminders=reminders)
Пример #5
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  [user_id]).fetchone()
Пример #6
0
def signup():
    name = None
    email = None
    errors = {}

    if request.method == 'POST':
        name = request.form.get('name')
        email = request.form.get('email')
        password = request.form.get('password')
        confirm = request.form.get('confirm')

        db = get_db()

        if not name:
            errors['name'] = 'Name is required.'
        elif not re.match(r'[a-zA-Z]+(?:\s[a-zA-Z]+)*$', name):
            errors['name'] = 'Name is invalid.'

        if not email:
            errors['email'] = 'Email address is required.'
        elif not re.match(
                r"[a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
                email):
            errors['email'] = 'Email address is invalid.'
        elif db.execute('SELECT id FROM user WHERE email = ?',
                        [email]).fetchone() is not None:
            errors['email'] = email + ' already exists.'

        if not password:
            errors['password'] = '******'
        elif not re.match(r'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}$',
                          password):
            errors['password'] = '******'
        elif not confirm:
            errors['confirm'] = 'Please re-enter password for confirmation.'
        elif not password == confirm:
            errors['confirm'] = 'Passwords do not match.'

        if not errors:
            db.execute(
                'INSERT INTO user (email, password, name, role) VALUES (?, ?, ?, ?)',
                [
                    email.lower(),
                    generate_password_hash(password), name, 'user'
                ])
            db.commit()

            return redirect(url_for('.login'))

    return render_template('auth/signup.html',
                           title='Sign up',
                           name=name,
                           email=email,
                           errors=errors)
Пример #7
0
def index():
    reminders = None
    appointments = None

    if g.user:
        db = get_db()

        reminders = db.execute(
            'SELECT * FROM reminder WHERE user_id = ? ORDER BY time LIMIT 5',
            [g.user['id']]).fetchall()
        appointments = db.execute(
            'SELECT * FROM appointment WHERE user_id = ? AND date_time > datetime("now") ORDER BY date_time DESC LIMIT 5',
            [g.user['id']]).fetchall()

    return render_template('index.html',
                           title='Home Page',
                           reminders=reminders,
                           appointments=appointments)
Пример #8
0
def index(user_id):
    if g.user['id'] != user_id and g.user['role'] != 'admin':
        return redirect(url_for('general.index'))

    bmi = [
        0 if g.user['weight'] == 0 or g.user['height'] == 0 else round(
            g.user['weight'] /
            (g.user['height'] / 100 * g.user['height'] / 100), 1)
    ]

    if bmi[0] >= 27.5:
        bmi.insert(1, 'HIGH RISK')
    elif bmi[0] >= 23:
        bmi.insert(1, 'MODERATE RISK')
    elif bmi[0] >= 18.5:
        bmi.insert(1, 'LOW RISK')
    else:
        bmi.insert(1, 'Risk Of Nutritional Deficiency')

    db = get_db()

    reminders = db.execute(
        'SELECT * FROM reminder WHERE user_id = ? ORDER BY time LIMIT 10',
        [user_id]).fetchall()
    appointments = db.execute(
        'SELECT * FROM appointment WHERE user_id = ? ORDER BY date_time DESC LIMIT 10',
        [user_id]).fetchall()
    histories = db.execute(
        'SELECT * FROM history WHERE user_id = ? ORDER BY date DESC LIMIT 10',
        [user_id]).fetchall()

    return render_template('user/index.html',
                           reminders=reminders,
                           appointments=appointments,
                           histories=histories,
                           bmi=bmi)
Пример #9
0
def edit():
    name = None
    gender = None
    dob = None
    height = None
    weight = None
    email = None
    errors = {}

    if request.method == 'POST':
        name = request.form.get('name')
        gender = request.form.get('gender')
        dob = request.form.get('dob')
        height = request.form.get('height')
        weight = request.form.get('weight')
        email = request.form.get('email')
        password = request.form.get('password')
        confirm = request.form.get('confirm')
        data = {}

        db = get_db()

        if name:
            if not re.match(r'[a-zA-Z]+(?:\s[a-zA-Z]+)*$', name):
                errors['name'] = 'Name is invalid.'
            else:
                data['name'] = name

        if gender:
            data['gender'] = gender
        else:
            errors['gender'] = 'Gender is required'

        if dob:
            data['dob'] = dob

        if height:
            data['height'] = height

        if weight:
            data['weight'] = weight

        if email:
            if not re.match(
                    r"[a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
                    email):
                errors['email'] = 'Email address is invalid.'
            elif g.user['email'] != email and db.execute(
                    'SELECT id FROM user WHERE email = ?',
                [email]).fetchone() is not None:
                errors['email'] = email + ' already exists.'
            else:
                data['email'] = email.lower()

        if password:
            if not re.match(r'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}$',
                            password):
                errors['password'] = '******'
            elif not confirm:
                errors[
                    'confirm'] = 'Please re-enter password for confirmation.'
            elif not password == confirm:
                errors['confirm'] = 'Passwords do not match.'
            else:
                data['password'] = generate_password_hash(password)

        if not errors:
            if not data:
                errors['all'] = 'Nothing to update.'
            else:
                db.execute(
                    'UPDATE user SET ' + ', '.join(key + ' = ?'
                                                   for key in data.keys()) +
                    ' WHERE id = ?',
                    [value for value in data.values()] + [g.user['id']])
                db.commit()

                g.user = db.execute('SELECT * FROM user WHERE id = ?',
                                    [g.user['id']]).fetchone()

                return redirect(url_for('.index', user_id=g.user['id']))

    return render_template('user/edit.html',
                           name=name,
                           gender=gender,
                           dob=dob,
                           height=height,
                           weight=weight,
                           email=email,
                           errors=errors)