Book Image

Flask Blueprints

By : Joel Perras
Book Image

Flask Blueprints

By: Joel Perras

Overview of this book

Table of Contents (14 chapters)

Your newsfeed


While we have built up most of the supporting architecture to provide the functionality for our Socializer application, we are still missing one of the more fundamental pieces of the puzzle: being able to view the posts of the people you follow in a chronological order.

To make the display of information about the owner of a post a bit simpler, let's add a relationship definition to our Post model:

class Post(db.Model):
    # …
    user = db.relationship('User',
        backref=db.backref('posts', lazy='dynamic'))

This will allow us to use post.user to access any of the user information that is associated with a given post, which is going to be quite useful in any view that displays a single post or a list of posts.

Let's add a route for this in application/users/views.py:

@users.route('/feed', methods=['GET'])
@login_required
def feed():
    """
    List all posts for the authenticated user; most recent first.
    """
    posts = current_user.newsfeed()
    return render_template...