Esempio n. 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,
        )
Esempio n. 2
0
File: posts.py Progetto: damnever/2L
def _post_info(post):
    info = post.to_dict()
    favorites = Favorite.count_by_post(post.id)
    up_votes = PostUpVote.count_by_post(post.id)
    down_votes = PostDownVote.count_by_post(post.id)
    latest_comment = Comment.latest_by_post(post.id)
    comment_count = Comment.count_by_post(post.id)
    if latest_comment:
        latest_comment_user = User.get(latest_comment.author_id).username
        latest_comment_date = latest_comment.date
    else:
        latest_comment_user = None
        latest_comment_date = None
    info.update({
        'up_votes': up_votes,
        'down_votes': down_votes,
        'favorites': favorites,
        'latest_comment_user': latest_comment_user,
        'latest_comment_date': latest_comment_date,
        'comment_count': comment_count,
    })
    return info