mirror of
https://github.com/rmontanana/microblog.git
synced 2025-08-16 07:45:51 +00:00
Chapter 9
This commit is contained in:
@@ -3,7 +3,13 @@ from flask import render_template, flash, redirect, url_for, request
|
|||||||
from flask_login import current_user, login_user, logout_user, login_required
|
from flask_login import current_user, login_user, logout_user, login_required
|
||||||
from werkzeug.urls import url_parse
|
from werkzeug.urls import url_parse
|
||||||
from app import app, db
|
from app import app, db
|
||||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm, PostForm
|
from app.forms import (
|
||||||
|
LoginForm,
|
||||||
|
RegistrationForm,
|
||||||
|
EditProfileForm,
|
||||||
|
EmptyForm,
|
||||||
|
PostForm,
|
||||||
|
)
|
||||||
from app.models import User, Post
|
from app.models import User, Post
|
||||||
|
|
||||||
|
|
||||||
@@ -14,8 +20,8 @@ def before_request():
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route("/", methods=["GET", "POST"])
|
||||||
@app.route('/index', methods=['GET', 'POST'])
|
@app.route("/index", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
form = PostForm()
|
form = PostForm()
|
||||||
@@ -23,11 +29,26 @@ def index():
|
|||||||
post = Post(body=form.post.data, author=current_user)
|
post = Post(body=form.post.data, author=current_user)
|
||||||
db.session.add(post)
|
db.session.add(post)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Your post is now live!')
|
flash("Your post is now live!")
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for("index"))
|
||||||
posts = current_user.followed_posts().all()
|
page = request.args.get("page", 1, type=int)
|
||||||
return render_template("index.html", title='Home Page', form=form,
|
posts = current_user.followed_posts().paginate(
|
||||||
posts=posts)
|
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("/login", methods=["GET", "POST"])
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
@@ -73,12 +94,29 @@ def register():
|
|||||||
@login_required
|
@login_required
|
||||||
def user(username):
|
def user(username):
|
||||||
user = User.query.filter_by(username=username).first_or_404()
|
user = User.query.filter_by(username=username).first_or_404()
|
||||||
posts = [
|
page = request.args.get("page", 1, type=int)
|
||||||
{"author": user, "body": "Test post #1"},
|
posts = user.posts.order_by(Post.timestamp.desc()).paginate(
|
||||||
{"author": user, "body": "Test post #2"},
|
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()
|
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"])
|
@app.route("/edit_profile", methods=["GET", "POST"])
|
||||||
@@ -138,8 +176,24 @@ def unfollow(username):
|
|||||||
else:
|
else:
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
@app.route('/explore')
|
|
||||||
|
@app.route("/explore")
|
||||||
@login_required
|
@login_required
|
||||||
def explore():
|
def explore():
|
||||||
posts = Post.query.order_by(Post.timestamp.desc()).all()
|
page = request.args.get("page", 1, type=int)
|
||||||
return render_template('index.html', title='Explore', posts=posts)
|
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,
|
||||||
|
)
|
||||||
|
@@ -16,6 +16,12 @@
|
|||||||
</form>
|
</form>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for post in posts %}
|
{% for post in posts %}
|
||||||
{% include "_post.html" %}
|
{% include '_post.html' %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% if prev_url %}
|
||||||
|
<a href="{{ prev_url }}">Newer posts</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if next_url %}
|
||||||
|
<a href="{{ next_url }}">Older posts</a>
|
||||||
|
{% endif %}
|
||||||
{% endblock %}s
|
{% endblock %}s
|
||||||
|
@@ -41,4 +41,10 @@
|
|||||||
{% for post in posts %}
|
{% for post in posts %}
|
||||||
{% include '_post.html' %}
|
{% include '_post.html' %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% if prev_url %}
|
||||||
|
<a href="{{ prev_url }}">Newer posts</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if next_url %}
|
||||||
|
<a href="{{ next_url }}">Older posts</a>
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@@ -15,3 +15,4 @@ class Config(object):
|
|||||||
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
|
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
|
||||||
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
|
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
|
||||||
ADMINS = ["your-email@example.com"]
|
ADMINS = ["your-email@example.com"]
|
||||||
|
POSTS_PER_PAGE = 25
|
||||||
|
Reference in New Issue
Block a user