예제 #1
0
파일: habits.py 프로젝트: Baltoli/inhabited
def complete(id):
    get_habit(id)
    db = get_db()
    db.execute('INSERT INTO completion (habit_id, timestamp)'
               ' VALUES (?, ?)', (id, to_timestamp(datetime.now())))
    db.commit()
    return redirect(url_for('habits.index'))
예제 #2
0
파일: habits.py 프로젝트: Baltoli/inhabited
def completed_today(habit_id):
    h = get_habit(id)
    db = get_db()
    ts = db.execute('SELECT timestamp FROM completions '
                    ' WHERE habit_id = ?'
                    ' ORDER BY timestamp DESC').fetchone()['timestamp']
    return is_today(ts)
예제 #3
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()
예제 #4
0
파일: habits.py 프로젝트: Baltoli/inhabited
def get_habit(id):
    habit = get_db().execute(
        'SELECT h.id, name, user_id, u.username '
        ' FROM habit h JOIN user u ON h.user_id = u.id'
        ' WHERE h.id = ?', (id, )).fetchone()

    if habit is None:
        abort(404, "Habit id {0} doesn't exist".format(id))

    if habit['user_id'] != g.user['id']:
        abort(403)

    return habit
예제 #5
0
파일: habits.py 프로젝트: Baltoli/inhabited
def create():
    if request.method == 'POST':
        name = request.form['name']
        error = None

        if not name:
            error = 'Name is required'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute('INSERT INTO habit (name, user_id) '
                       'VALUES (?, ?)', (name, g.user['id']))
            db.commit()
            return redirect(url_for('habits.index'))

    return render_template('habits/create.html')
예제 #6
0
파일: habits.py 프로젝트: Baltoli/inhabited
def update(id):
    habit = get_habit(id)

    if request.method == 'POST':
        name = request.form['name']
        error = None

        if not name:
            error = 'Name is required'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute('UPDATE habit SET name = ?' ' WHERE id = ?', (name, id))
            db.commit()
            return redirect(url_for('habits.index'))

    return render_template('habits/update.html', habit=habit)
예제 #7
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = 'Incorrect username'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
예제 #8
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username required'
        elif not password:
            error = 'Password required'
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered'.format(username)

        if error is None:
            db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                       (username, generate_password_hash(password)))
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
예제 #9
0
파일: habits.py 프로젝트: Baltoli/inhabited
def index():
    db = get_db()

    habits = db.execute('SELECT id, name '
                        'FROM habit WHERE user_id = ?',
                        (g.user['id'], )).fetchall()

    completions = [
        db.execute(
            'SELECT * FROM completion WHERE '
            ' habit_id = ?'
            ' ORDER BY timestamp DESC', (h['id'], )).fetchall() for h in habits
    ]

    periods = [completed_periods(10, c) for c in completions]

    to_complete = [not p[0] for p in periods]

    # Do the conversion logic here to turn timestamps into completion data -
    # probably will need a separate module in which I do these conversions based
    # on the current date, then pass to the templating engine etc

    return render_template('habits/index.html',
                           data=zip(habits, periods, to_complete))
예제 #10
0
파일: habits.py 프로젝트: Baltoli/inhabited
def delete(id):
    get_habit(id)
    db = get_db()
    db.execute('DELETE FROM habit WHERE id = ?', (id, ))
    db.commit()
    return redirect(url_for('habits.index'))