diff --git a/app.db b/app.db index dfff975..3162060 100644 Binary files a/app.db and b/app.db differ diff --git a/app/forms.py b/app/forms.py index 6668c52..113ec57 100644 --- a/app/forms.py +++ b/app/forms.py @@ -61,3 +61,8 @@ class EditProfileForm(FlaskForm): class EmptyForm(FlaskForm): submit = SubmitField("Submit") + +class PostForm(FlaskForm): + post = TextAreaField('Say something', validators=[ + DataRequired(), Length(min=1, max=140)]) + submit = SubmitField('Submit') \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 5526ada..60090ee 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,8 +3,8 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import current_user, login_user, logout_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,18 +14,20 @@ 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')) + posts = current_user.followed_posts().all() + return render_template("index.html", title='Home Page', form=form, + posts=posts) @app.route("/login", methods=["GET", "POST"]) @@ -75,7 +77,8 @@ def user(username): {"author": user, "body": "Test post #1"}, {"author": user, "body": "Test post #2"}, ] - return render_template("user.html", user=user, posts=posts) + form = EmptyForm() + return render_template("user.html", user=user, posts=posts, form=form) @app.route("/edit_profile", methods=["GET", "POST"]) @@ -134,3 +137,9 @@ def unfollow(username): return redirect(url_for("user", username=username)) else: return redirect(url_for("index")) + +@app.route('/explore') +@login_required +def explore(): + posts = Post.query.order_by(Post.timestamp.desc()).all() + return render_template('index.html', title='Explore', posts=posts) \ No newline at end of file diff --git a/app/templates/_post.html b/app/templates/_post.html index c764a55..29bc463 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -4,7 +4,8 @@ - {{ post.author.username }} says:
{{ post.body }} + {{ post.author.username }} + says:
{{ post.body }} diff --git a/app/templates/base.html b/app/templates/base.html index 4b5bb70..e43a9cc 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -14,6 +14,7 @@ Login {% else %} Profile + Explore Logout {% endif %} diff --git a/app/templates/index.html b/app/templates/index.html index 76dbc10..b01d201 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,11 +2,20 @@ {% block content %}

Hi, {{ current_user.username }}!

- {% for post in posts %} -
+ {% if form %} +
+ {{ form.hidden_tag() }}

- {{ post.author.username }} says: {{ post.body }} + {{ form.post.label }}
+ {{ form.post(cols = 32, rows = 4) }}
+ {% for error in form.post.errors %} + [{{ error }}] + {% endfor %}

-
+

{{ form.submit() }}

+ + {% endif %} + {% for post in posts %} + {% include "_post.html" %} {% endfor %} -{% endblock %} +{% endblock %}s