def user_timeline(username):
    profile_user = db.query_db('select * from user where username = ?', [username], one=True)
    if profile_user is None:
        abort(404)
    followed = False
    if g.user:
        followed = db.query_db('''select 1 from follower
          where follower.who_id = ? and follower.whom_id = ?
        ''', [session['user_id'], profile_user['user_id']], one=True) is not None
    return render_template('timeline.html', messages=db.query_db('''
        select message.*, user.* from message, user
        where user.user_id = message.author_id and user.user_id = ?
        order by message.pub_date desc limit ?
        ''', [profile_user['user_id'], Config.PER_PAGE]), followed=followed, profile_user=profile_user)
def timeline():
    if not g.user:
        return redirect(url_for('bp_minitwit.public_timeline'))
    return render_template('timeline.html', messages=db.query_db('''
        select message.*, user.* from message, user where message.author_id = user.user_id and (
          user.user_id = ? or
          user.user_id in (select whom_id from follower where who_id = ?))
        order by message.pub_date desc limit ?
    ''', [session['user_id'], session['user_id'], Config.PER_PAGE]))
 def before_request():
     """
     Flask 에서 제공하는 요청에 앞서서 실행되는 함수
     """
     g.db = db.connect_db()
     # 전역객체 g: 한 번의 요청에 대해서만 같은 값을 유지하고 /
     #             스레드에 대해 안전하는 전제 조건
     g.user = None
     if 'user_id' in session:
         g.user = db.query_db('select * from user where user_id = ?', [session['user_id']], one=True)
 def before_request():
     """
     Flask 에서 제공하는 요청에 앞서서 실행되는 함수
     """
     g.db = db.connect_db()
     # 전역객체 g: 한 번의 요청에 대해서만 같은 값을 유지하고 /
     #             스레드에 대해 안전하는 전제 조건
     g.user = None
     if 'user_id' in session:
         g.user = db.query_db('select * from user where user_id = ?',
                              [session['user_id']],
                              one=True)
def login():
    if g.user:
        return redirect(url_for('bp_minitwit.timeline'))
    error = None
    if request.method == 'POST':
        user = db.query_db(''' select * from user where
            username = ?''', [request.form['username']], one=True)
        if user is None:
            error = 'Invalid username'
        elif not check_password_hash(user['pw_hash'], request.form['password']):
            error = 'Invalid passowrd'
        else:
            flash('You were logged in')
            session['user_id'] = user['user_id']
            return redirect(url_for('bp_minitwit.timeline'))
    return render_template('login.html', error=error)
def public_timeline():
    return render_template('timeline.html', messages=db.query_db('''
        select message.*, user.* from message, user
        where message.author_id = user.user_id
        order by message.pub_date desc limit ?
    ''', [Config.PER_PAGE]))