From 02e19801227df21eaa460e83ee4e18e0b36c7c57 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sat, 24 Nov 2018 18:28:00 +0100 Subject: [PATCH] Chapter 12: Followed posts with a join (12c) --- app/main/views.py | 2 +- app/models.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/main/views.py b/app/main/views.py index cbbadb0..bcd8367 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -114,7 +114,7 @@ def edit(id): def follow(username): user = User.query.filter_by(username=username).first() if user is None: - flash('Invalud user') + flash('Invalid user') return redirect(url_for('.index')) if current_user.is_following(user): flash('You are already follwoing this user.') diff --git a/app/models.py b/app/models.py index 26508d7..1424abc 100644 --- a/app/models.py +++ b/app/models.py @@ -226,6 +226,11 @@ class User(UserMixin, db.Model): return self.followers.filter_by( follower_id=user.id).first() is not None + @property + def followed_posts(self): + return Post.query.join(Follow, Follow.followed_id == Post.author_id) \ + .filter(Follow.follower_id == self.id) + def __repr__(self): return '' % self.username