diff --git a/app/forms.py b/app/forms.py index 1ca012c..f10edee 100644 --- a/app/forms.py +++ b/app/forms.py @@ -50,3 +50,8 @@ class EditProfileForm(FlaskForm): class EmptyForm(FlaskForm): submit = SubmitField('Submit') + + +class PostForm(FlaskForm): + post = TextAreaField('Say something', validators=[DataRequired()]) + submit = SubmitField('Submit') diff --git a/app/routes.py b/app/routes.py index 8c36e98..44934c4 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,8 +3,9 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import login_user, logout_user, current_user, login_required from werkzeug.urls import url_parse from app import app, db -from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm -from app.models import User +from app.forms import LoginForm, RegistrationForm, EditProfileForm, \ + EmptyForm, PostForm +from app.models import User, Post @app.before_request @@ -14,21 +15,41 @@ def before_request(): db.session.commit() -@app.route('/') -@app.route('/index') +@app.route('/', methods=['GET', 'POST']) +@app.route('/index', methods=['GET', 'POST']) @login_required def index(): - posts = [ - { - 'author': {'username': 'John'}, - 'body': 'Beautiful day in Portland!' - }, - { - 'author': {'username': 'Susan'}, - 'body': 'The Avengers movie was so cool!' - } - ] - return render_template('index.html', title='Home', posts=posts) + form = PostForm() + if form.validate_on_submit(): + post = Post(body=form.post.data, author=current_user) + db.session.add(post) + db.session.commit() + flash('Your post is now live!') + return redirect(url_for('index')) + page = request.args.get('page', 1, type=int) + posts = current_user.following_posts().paginate( + page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('index', page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('index', page=posts.prev_num) \ + if posts.has_prev else None + return render_template('index.html', title='Home', form=form, + posts=posts.items, next_url=next_url, + prev_url=prev_url) + + +@app.route('/explore') +@login_required +def explore(): + page = request.args.get('page', 1, type=int) + posts = Post.query.order_by(Post.timestamp.desc()).paginate( + page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('explore', page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('explore', page=posts.prev_num) \ + if posts.has_prev else None + return render_template('index.html', title='Explore', posts=posts.items, + next_url=next_url, prev_url=prev_url) @app.route('/login', methods=['GET', 'POST']) @@ -74,12 +95,16 @@ def register(): @login_required def user(username): user = User.query.filter_by(username=username).first_or_404() - posts = [ - {'author': user, 'body': 'Test post #1'}, - {'author': user, 'body': 'Test post #2'} - ] + page = request.args.get('page', 1, type=int) + posts = user.posts.order_by(Post.timestamp.desc()).paginate( + page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('user', username=user.username, page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('user', username=user.username, page=posts.prev_num) \ + if posts.has_prev else None form = EmptyForm() - return render_template('user.html', user=user, posts=posts, form=form) + return render_template('user.html', user=user, posts=posts.items, + next_url=next_url, prev_url=prev_url, form=form) @app.route('/edit_profile', methods=['GET', 'POST']) diff --git a/app/templates/_post.html b/app/templates/_post.html index d020426..a705b31 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -1,6 +1,6 @@
- | {{ post.author.username }} says: {{ post.body }} |
+ {{ post.author.username }} says: {{ post.body }} |