def user(username): """Display's a users tweets.""" profile_user = functions.query_db('select * from user where username = ?', [username], one=True) count_tweets = functions.count_db(''' select message.* from message where message.author_id = ?''', [profile_user['user_id']]) count_following = functions.count_db(''' select whom_id from follower where who_id = ?''', [profile_user['user_id']]) count_followers = functions.count_db(''' select who_id from follower where whom_id = ?''', [profile_user['user_id']]) if profile_user is None: abort(404) followed = False if g.user: followed = functions.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=functions.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'], PER_PAGE]), followed=followed, profile_user=profile_user, tweets=count_tweets, following=count_following, followers=count_followers)
def index(): """Shows a users timeline or if no user is logged in it will redirect to the public timeline. This timeline shows the user's messages as well as all the messages of followed users. """ if not g.user: return redirect(functions.url_for('/public')) query_messages = functions.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'], PER_PAGE]) count_tweets = functions.count_db(''' select message.* from message where message.author_id = ?''', [session['user_id']]) count_following = functions.count_db(''' select whom_id from follower where who_id = ?''', [session['user_id']]) count_followers = functions.count_db(''' select who_id from follower where whom_id = ?''', [session['user_id']]) return render_template('timeline.html', messages=query_messages, tweets=count_tweets, following=count_following, followers=count_followers)
def login(): """Logs the user in.""" if g.user: return redirect(functions.url_for('/')) error = None if request.method == 'POST': user = functions.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 password' else: flash('You were logged in') session['user_id'] = user['user_id'] return redirect(functions.url_for('/')) return render_template('login.html', error=error)
def public(): """Displays the latest messages of all users.""" return render_template('timeline.html', messages=functions.query_db(''' select message.*, user.* from message, user where message.author_id = user.user_id order by message.pub_date desc limit ?''', [PER_PAGE]))