Skip to content

Commit

Permalink
Improve post timeline endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 23, 2021
1 parent 8464107 commit 224bb16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
15 changes: 13 additions & 2 deletions api/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@ def delete(id):
return '', 204


@posts.route('/posts/timeline', methods=['GET'])
@posts.route('/users/<int:id>/timeline', methods=['GET'])
@authenticate(token_auth)
@paginated_response(posts_schema, order_by=Post.timestamp,
order_direction='desc', model_from_statement=Post,
pagination_schema=DateTimePaginationSchema)
def timeline():
def timeline(id):
"""Retrieve the user's post timeline"""
user = db.session.get(User, id) or abort(404)
return user.followed_posts_select().order_by(Post.timestamp.desc())


@posts.route('/users/me/timeline', methods=['GET'])
@authenticate(token_auth)
@paginated_response(posts_schema, order_by=Post.timestamp,
order_direction='desc', model_from_statement=Post,
pagination_schema=DateTimePaginationSchema)
def my_timeline():
"""Retrieve the logged in user's post timeline"""
user = token_auth.current_user()['user']
return user.followed_posts_select().order_by(Post.timestamp.desc())
20 changes: 11 additions & 9 deletions tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ def test_post_timeline(self):
db.session.add_all([post1, post2, post3])
db.session.commit()

rv = self.client.get('/api/posts/timeline')
assert rv.status_code == 200
assert rv.json['pagination']['total'] == 3
assert rv.json['data'][0]['body'] == 'Post 3'
assert rv.json['data'][0]['author']['username'] == 'susan'
assert rv.json['data'][1]['body'] == 'Post 2'
assert rv.json['data'][1]['author']['username'] == 'test'
assert rv.json['data'][2]['body'] == 'Post 1'
assert rv.json['data'][2]['author']['username'] == 'susan'
rv1 = self.client.get('/api/users/me/timeline')
rv2 = self.client.get('/api/users/1/timeline')
for rv in [rv1, rv2]:
assert rv.status_code == 200
assert rv.json['pagination']['total'] == 3
assert rv.json['data'][0]['body'] == 'Post 3'
assert rv.json['data'][0]['author']['username'] == 'susan'
assert rv.json['data'][1]['body'] == 'Post 2'
assert rv.json['data'][1]['author']['username'] == 'test'
assert rv.json['data'][2]['body'] == 'Post 1'
assert rv.json['data'][2]['author']['username'] == 'susan'

def test_permissions(self):
user = User(username='susan', email='[email protected]',
Expand Down

0 comments on commit 224bb16

Please sign in to comment.