@app.route('/<username>') defuser_timeline(username): profile_user = query_db('SELECT * FROM user WHERE user_name = ?', [username], one=True) if profile_user isNone: abort(404) followed = False if g.user: followed = query_db('''SELECT 1 FROM follows WHERE follows.follower_id = ? AND follows.followed_id = ?''', [session['user_id'], profile_user['user_id']], one=True) isnotNone query = ''' SELECT messages.*, users.* FROM messages, users WHERE users.user_id = message.user_id AND users.user_id = ? ORDER BY messages.pub_time DESC LIMIT ?''' messages = query_db(query, [profile_user[user_id], PER_PAGE]) return render_template('timeline.html', messages=messages, followed=followed, profile_user=profile_user)
关注
关注用户,就是在关注表中添加一条记录,关注人为当前登录用户,被关注者为浏览的用户。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from flask import flash
@app.route('/<username>/follow') deffollow(username): ifnot g.user: abort(401) followed_id = get_user_id(username) if followed_id isNone: abort(404) db = get_db() db.execute('''INSERT INTO follows (follower_id, followed_id) VALUES (?, ?)''', [session['user_id'], followed_id]) db.commit() flash('Your are now following "%s".' % username) return redirect(url_for('user_timeline', username=username))
取消关注
1 2 3 4 5 6 7 8 9 10 11 12 13
@app.route('/<username>/unfollow') defunfollow(username): ifnot g.user: abort(401) followed_id = get_user_id(username) if followed_id isNone: abort(404) db = get_db() db.execute('DELETE FROM follows WHERE follower_id=? AND followed_id=?', [session['user_id'], followed_id]) db.commit() flash('You are no longer following "%s".' % username) return redirect(url_for('user_timeline', username=username))
发布消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import time from flask import request
@app.route('/add_message', methods=['POST']) defadd_message(): ifnot g.user: abort(401) if request.form['text']: db = get_db() db.execute('''INSERT INTO messages (user_id, text, pub_time) VALUES (?, ?, ?)''', [session['user_id'], request.form['text'], int(time.time())]) db.commit() flash('Your message was recorded.') return redirect(url_for('timeline'))
@app.route('/register', methods=['GET', 'POST']) defregister(): if g.user: return redirect(url_for('timeline')) error = None if request.method == 'POST': ifnot request.form['username']: error = 'You have to enter a username' elifnot request.form['email'] or \ '@'notin request.form['email']: error = 'You have to enter a valid emial address' elifnot request.form['password']: error = 'You have to enter a password' elif request.form['password'] != request.form['password2']: error = 'The two password do not match' elif get_user_id(request.form['username']) isnotNone: error = 'The username is already taken' else: db = get_db() db.execute('''INSERT INTO users (user_name, email, pw_hash) VALUES (?, ?, ?)''', [request.form['username'], request.form['email'], generate_password_hash(request.form['password'])]) db.commit() flash('You were successfully registered and can login now') return redirect(url_for('login')) if error: flash(error) return render_template('register.html')