Example #1
0
    def get(self, post_id):
        post = yield gen.maybe_future(Post.get(post_id))
        if not post:
            raise HTTPError(404)

        author = yield gen.maybe_future(User.get(post.author_id))
        favorites = yield gen.maybe_future(Favorite.count_by_post(post_id))
        up_votes = yield gen.maybe_future(PostUpVote.count_by_post(post_id))
        down_votes = yield gen.maybe_future(PostDownVote.count_by_post(post_id))

        username = self.current_user
        favorited, up_voted, down_voted = False, False, False
        if username:
            up_voted = bool((yield gen.maybe_future(
                PostUpVote.get_by_user_post(username, post_id))))
            down_voted = bool((yield gen.maybe_future(
                PostDownVote.get_by_user_post(username, post_id))))
            favorited = bool((yield gen.maybe_future(
                Favorite.get_by_user_post(username, post_id))))
        self.render(
            'post.html',
            title=post.title,
            keywords=post.keywords,
            description=post.title,
            topic_id=post.topic_id,
            post_id=post_id,
            author=author.username,
            avatar=author.profile.avatar,
            date=post.created_date,
            content=post.content,
            up_votes=up_votes,
            down_votes=down_votes,
            favorites=favorites,
            favorited=int(favorited),
            up_voted=int(up_voted),
            down_voted=int(down_voted),
            keep_silent=post.keep_silent,
        )