def delete(self, post_id): username = self.current_user f = yield gen.maybe_future(Favorite.get_by_user_post(username, post_id)) if f: yield gen.maybe_future(f.delete()) count = yield gen.maybe_future(Favorite.count_by_post(post_id)) # Update gold. update_gold.apply_async(("cancel_post_be_favorite", post_id)) raise gen.Return({"count": count}) else: raise exceptions.PostHasNotBeenFavorited()
def post(self, post_id): username = self.current_user f = yield gen.maybe_future(Favorite.get_by_user_post(username, post_id)) if f: raise exceptions.PostAlreadyFavorited() else: yield gen.maybe_future(Favorite.create(username, post_id)) count = yield gen.maybe_future(Favorite.count_by_post(post_id)) # Update gold. update_gold.apply_async(("post_be_favorite", post_id)) raise gen.Return({"count": count})
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
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, )