Browse Source

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

T. Meissner 1 year 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 @@
1 1
 from flask import render_template, redirect, url_for, flash, request, \
2
-    current_app, abort
2
+    current_app, abort, make_response
3 3
 from flask_login import login_required, current_user
4 4
 from . import main
5 5
 from .forms import EditProfileForm, EditProfileAdminForm, PostForm
@@ -18,12 +18,19 @@ def index():
18 18
         db.session.commit()
19 19
         return redirect(url_for('.index'))
20 20
     page = request.args.get('page', 1, type=int)
21
-    pagination = Post.query.order_by(Post.timestamp.desc()).paginate(
21
+    show_followed = False
22
+    if current_user.is_authenticated:
23
+        show_followed = bool(request.cookies.get('show_followed', ''))
24
+    if show_followed:
25
+        query = current_user.followed_posts
26
+    else:
27
+        query = Post.query
28
+    pagination = query.order_by(Post.timestamp.desc()).paginate(
22 29
         page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
23 30
         error_out=False)
24 31
     posts = pagination.items
25 32
     return render_template('index.html', form=form, posts=posts,
26
-                           pagination=pagination)
33
+                           show_followed=show_followed, pagination=pagination)
27 34
 
28 35
 
29 36
 @main.route('/user/<username>')
@@ -174,3 +181,19 @@ def followed_by(username):
174 181
     return render_template('followers.html', user=user, title="Followed by",
175 182
                            endpoint='.followed_by', pagination=pagination,
176 183
                            follows=follows)
184
+
185
+
186
+@main.route('/all')
187
+@login_required
188
+def show_all():
189
+    resp = make_response(redirect(url_for('.index')))
190
+    resp.set_cookie('show_followed', '', max_age=30*24*60*60)  # lasts 30 days
191
+    return resp
192
+
193
+
194
+@main.route('/followed')
195
+@login_required
196
+def show_followed():
197
+    resp = make_response(redirect(url_for('.index')))
198
+    resp.set_cookie('show_followed', '1', max_age=30*24*60*60)  # lasts 30 days
199
+    return resp

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

@@ -5,12 +5,19 @@
5 5
     min-height: 260px;
6 6
     margin-left: 280px;
7 7
 }
8
+div.post-tabs {
9
+    margin-top: 16px;
10
+}
8 11
 ul.posts {
9 12
     list-style-type: none;
10 13
     padding: 0px;
11 14
     margin: 16px 0px 0px 0px;
12 15
     border-top: 1px solid #e0e0e0;
13 16
 }
17
+div.post-tabs ul.posts {
18
+    margin: 0px;
19
+    border-top: none;
20
+}
14 21
 ul.posts li.post {
15 22
     padding: 8px;
16 23
     border-bottom: 1px solid #e0e0e0;

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

@@ -16,7 +16,20 @@
16 16
     {% endif %}
17 17
 </div>
18 18
 
19
-{% include '_posts.html' %}
19
+<div class="post-tabs">
20
+    <ul class="nav nav-tabs">
21
+        <li {% if not show_followed %}class="active"{% endif %}>
22
+            <a href="{{ url_for('.show_all') }}">All</a>
23
+        </li>
24
+        {% if current_user.is_authenticated %}
25
+        <li {% if show_followed %}class="active"{% endif %}>
26
+            <a href="{{ url_for('.show_followed') }}">Followed</a>
27
+        </li>
28
+        {% endif %}
29
+    </ul>
30
+    {% include '_posts.html' %}
31
+</div>
32
+
20 33
 {% if pagination %}
21 34
  <div class="pagination">
22 35
      {{ macros.pagination_widget(pagination, '.index') }}