def get_posts(): posts = [p.as_dict() for p in Post.query.order_by(desc(Post.id)).all()] for p in posts: p["user"] = User.by_id(p["user_id"]).as_dict() like_users = PostLike.user_ids(p["id"]) p["num_of_likes"] = len(like_users) p["liked"] = (request.user.id in like_users) p["num_of_comments"] = len(PostComment.comments(p["id"])) return jsonify(posts)
def get_comment(post_id): comments = [c.as_dict() for c in PostComment.comments(post_id)] for c in comments: c["user"] = User.by_id(c["user_id"]).as_dict() return jsonify(comments)