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 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 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, )