def construct_follows_list_response(follows, followers_mode=False): res = [] for follow in follows: with Transaction() as tr: try: user_id = follow.user_id if not followers_mode else follow.follower_id res.append(User.read_by_pk(tr.cursor, user_id).username) except ItemNotFoundError: continue return res
def construct_posts_list_response(posts): res = [] for post in posts: post_dict = post.to_dict() with Transaction() as tr: try: post_dict['username'] = User.read_by_pk( tr.cursor, post_dict.pop('user_id')).username except ItemNotFoundError: continue res.append(post_dict) return res
def handle_admin_info(self, request): with Transaction() as tr: try: user = User.read_by_pk(tr.cursor, self.context['user_id']) except ItemNotFoundError: return construct_result(404, 'User not found') if not user.is_admin: return construct_result(403, 'Access denied') users = User.read_all(tr.cursor) posts = Post.read_all(tr.cursor) follows = Follow.read_all(tr.cursor) return construct_result( 200, { 'users': [user.to_dict() for user in users], 'posts': [post.to_dict() for post in posts], 'folows': [follow.to_dict() for follow in follows], } )