Browse Source

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

master
T. Meissner 5 years 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):
def follow(username): def follow(username):
user = User.query.filter_by(username=username).first() user = User.query.filter_by(username=username).first()
if user is None: if user is None:
flash('Invalud user')
flash('Invalid user')
return redirect(url_for('.index')) return redirect(url_for('.index'))
if current_user.is_following(user): if current_user.is_following(user):
flash('You are already follwoing this user.') flash('You are already follwoing this user.')


+ 5
- 0
app/models.py View File

@ -226,6 +226,11 @@ class User(UserMixin, db.Model):
return self.followers.filter_by( return self.followers.filter_by(
follower_id=user.id).first() is not None 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): def __repr__(self):
return '<User %r>' % self.username return '<User %r>' % self.username


Loading…
Cancel
Save