Browse Source

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

master
T. Meissner 6 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):
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.')


+ 5
- 0
app/models.py View File

@ -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 '<User %r>' % self.username


Loading…
Cancel
Save