def get(self, article_id, order, page): article_id = int(article_id) if not article_id: raise HTTPError(404) article = Article.get_by_id(article_id) if not article: raise HTTPError(404) if not article.public: self.set_cache(is_public=False) if not self.is_admin: raise HTTPError(404) page = int(page) comments_list = [] comments, has_next_page = Comment.get_comments_of_article( article_id, order == 'asc', page) if comments: user_ids = {comment.user_id for comment in comments} users = User.get_by_ids(user_ids, filter_empty=True) if users: user_dict = {user.id: user for user in users} else: user_dict = {} for comment in comments: user = user_dict.get(comment.user_id) if user: user_name = user.name user_site = escape(user.site) if user.site else '' else: user_name = u'匿名用户' user_site = '' comments_list.append({ 'user_name': user_name, 'url': user_site, 'img': user.get_avatar(), 'ua': comment.ua, 'time': formatted_time(timestamp_to_datetime(comment.time)), 'id': comment.id, 'content': comment.html_content() }) output = {'comments': comments_list} if has_next_page: output['next_page'] = page + 1 self.write_json(output)
def get(self, article_id, order, page): article_id = int(article_id) if not article_id: raise HTTPError(404) article = Article.get_by_id(article_id) if not article: raise HTTPError(404) if not article.public: self.set_cache(is_public=False) if not self.is_admin: raise HTTPError(404) page = int(page) comments_list = [] comments, has_next_page = Comment.get_comments_of_article(article_id, order == 'asc', page) if comments: user_ids = {comment.user_id for comment in comments} users = User.get_by_ids(user_ids, filter_empty=True) if users: user_dict = {user.id: user for user in users} else: user_dict = {} for comment in comments: user = user_dict.get(comment.user_id) if user: user_name = user.name user_site = escape(user.site) if user.site else '' else: user_name = u'匿名用户' user_site = '' comments_list.append({ 'user_name': user_name, 'url': user_site, 'img': user.get_avatar(), 'ua': comment.ua, 'time': formatted_time(timestamp_to_datetime(comment.time)), 'id': comment.id, 'content': comment.html_content() }) output = {'comments': comments_list} if has_next_page: output['next_page'] = page + 1 self.write_json(output)