Skip to content

Commit

Permalink
Add Chapter 9 in full
Browse files Browse the repository at this point in the history
  • Loading branch information
lubianat committed May 11, 2023
1 parent 2cca614 commit 9ff47d6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
Binary file modified app/app.db
Binary file not shown.
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class Config(object):
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
ADMINS = ["tiago.lubiana.alves@usp.br"]
POSTS_PER_PAGE = 3
POSTS_PER_PAGE = 20
49 changes: 41 additions & 8 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ def index():
posts = current_user.followed_posts().paginate(
page=page, per_page=app.config["POSTS_PER_PAGE"], error_out=False
)
return render_template("index.html", title="Home", form=form, posts=posts.items)
next_url = url_for("index", page=posts.next_num) if posts.has_prev 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")
Expand All @@ -102,7 +111,15 @@ def explore():
posts = Post.query.order_by(Post.timestamp.desc()).paginate(
page=page, per_page=app.config["POSTS_PER_PAGE"], error_out=False
)
return render_template("index.html", title="Explore", posts=posts.items)
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("/register", methods=["GET", "POST"])
Expand Down Expand Up @@ -142,13 +159,29 @@ def login():
@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("/logout")
Expand Down
6 changes: 6 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ <h1>Hi, {{ current_user.username }}!</h1>
{% for post in posts %}
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older posts</a>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ <h1>User: {{ user.username }}</h1>
{% for post in posts %}
{% include "_post.html" %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older posts</a>
{% endif %}
{% endblock %}

0 comments on commit 9ff47d6

Please sign in to comment.