Browse Source

Chapter 12: Followed posts with a join (12c)

T. Meissner 1 year ago
parent
commit
02e1980122
2 changed files with 6 additions and 1 deletions
  1. 1
    1
      app/main/views.py
  2. 5
    0
      app/models.py

+ 1
- 1
app/main/views.py View File

@@ -114,7 +114,7 @@ def edit(id):
114 114
 def follow(username):
115 115
     user = User.query.filter_by(username=username).first()
116 116
     if user is None:
117
-        flash('Invalud user')
117
+        flash('Invalid user')
118 118
         return redirect(url_for('.index'))
119 119
     if current_user.is_following(user):
120 120
         flash('You are already follwoing this user.')

+ 5
- 0
app/models.py View File

@@ -226,6 +226,11 @@ class User(UserMixin, db.Model):
226 226
         return self.followers.filter_by(
227 227
             follower_id=user.id).first() is not None
228 228
 
229
+    @property
230
+    def followed_posts(self):
231
+        return Post.query.join(Follow, Follow.followed_id == Post.author_id) \
232
+               .filter(Follow.follower_id == self.id)
233
+
229 234
     def __repr__(self):
230 235
         return '<User %r>' % self.username
231 236