Browse Source

Chapter 12: Show followed blog posts in home page (12d)

master
T. Meissner 6 years ago
parent
commit
814cbfc996
3 changed files with 47 additions and 4 deletions
  1. +26
    -3
      app/main/views.py
  2. +7
    -0
      app/static/styles.css
  3. +14
    -1
      app/templates/index.html

+ 26
- 3
app/main/views.py View File

@ -1,5 +1,5 @@
from flask import render_template, redirect, url_for, flash, request, \
current_app, abort
current_app, abort, make_response
from flask_login import login_required, current_user
from . import main
from .forms import EditProfileForm, EditProfileAdminForm, PostForm
@ -18,12 +18,19 @@ def index():
db.session.commit()
return redirect(url_for('.index'))
page = request.args.get('page', 1, type=int)
pagination = Post.query.order_by(Post.timestamp.desc()).paginate(
show_followed = False
if current_user.is_authenticated:
show_followed = bool(request.cookies.get('show_followed', ''))
if show_followed:
query = current_user.followed_posts
else:
query = Post.query
pagination = query.order_by(Post.timestamp.desc()).paginate(
page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
error_out=False)
posts = pagination.items
return render_template('index.html', form=form, posts=posts,
pagination=pagination)
show_followed=show_followed, pagination=pagination)
@main.route('/user/<username>')
@ -174,3 +181,19 @@ def followed_by(username):
return render_template('followers.html', user=user, title="Followed by",
endpoint='.followed_by', pagination=pagination,
follows=follows)
@main.route('/all')
@login_required
def show_all():
resp = make_response(redirect(url_for('.index')))
resp.set_cookie('show_followed', '', max_age=30*24*60*60) # lasts 30 days
return resp
@main.route('/followed')
@login_required
def show_followed():
resp = make_response(redirect(url_for('.index')))
resp.set_cookie('show_followed', '1', max_age=30*24*60*60) # lasts 30 days
return resp

+ 7
- 0
app/static/styles.css View File

@ -5,12 +5,19 @@
min-height: 260px;
margin-left: 280px;
}
div.post-tabs {
margin-top: 16px;
}
ul.posts {
list-style-type: none;
padding: 0px;
margin: 16px 0px 0px 0px;
border-top: 1px solid #e0e0e0;
}
div.post-tabs ul.posts {
margin: 0px;
border-top: none;
}
ul.posts li.post {
padding: 8px;
border-bottom: 1px solid #e0e0e0;


+ 14
- 1
app/templates/index.html View File

@ -16,7 +16,20 @@
{% endif %}
</div>
{% include '_posts.html' %}
<div class="post-tabs">
<ul class="nav nav-tabs">
<li {% if not show_followed %}class="active"{% endif %}>
<a href="{{ url_for('.show_all') }}">All</a>
</li>
{% if current_user.is_authenticated %}
<li {% if show_followed %}class="active"{% endif %}>
<a href="{{ url_for('.show_followed') }}">Followed</a>
</li>
{% endif %}
</ul>
{% include '_posts.html' %}
</div>
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.index') }}


Loading…
Cancel
Save