def toDictionary(self, options=[], user=None): '''To dictionary''' # Remeber not to use 'user' because it is a parameter idToUser = dict([(u.id(), u) for u in self.users()]) # If we need to, fetch Follows to check if there are return follows # for if people in userIds follow the user passed in as a parameter if user and UserList.Options.USER_FOLLOW in options: userFollows = DataApi.getUserFollowsFromList(user.model(), idToUser.keys()) userFollowIds = [f.userId for f in userFollows] from Follow import Follow idToFollow = dict([(f.userId, Follow(f)) for f in userFollows]) objList = [] for u in self.users(): obj = u.toDictionary() if user and UserList.Options.USER_FOLLOW in options: if u.id() in idToFollow: follow = idToFollow[u.id()] obj[User.Key.BLESSED] = follow.blessed(user) obj[User.Key.FOLLOW] = True else: obj[User.Key.BLESSED] = False obj[User.Key.FOLLOW] = False objList.append(obj) return objList
def toDictionary(self, options=[], user=None): '''To dictionary''' from User import User # get list of user ids we want to fetch if FollowList.Options.FOLLOW_LIST in options: userIds = [follow.userId() for follow in self.followList()] elif FollowList.Options.FOLLOWER_LIST in options: userIds = [follow.followerId() for follow in self.followList()] else: assert False, "Should have FOLLOW_LIST or FOLLOWER_LIST options" users = User.getByUserIds(userIds) # don't user 'user' because it is a parameter idToUser = dict([(u.id(), u) for u in users]) # If we need to, fetch Follows to check if there are return follows # for if people in userIds follow the user passed in as a parameter if FollowList.Options.USER_FOLLOW in options: userFollows = DataApi.getUserFollowsFromList(user.model(), userIds) userFollowIds = [f.userId for f in userFollows] objList = [] for follow in self.followList(): if FollowList.Options.FOLLOW_LIST in options: u = idToUser[follow.userId()] elif FollowList.Options.FOLLOWER_LIST in options: u = idToUser[follow.followerId()] else: assert False, "Should have FOLLOW_LIST or FOLLOWER_LIST options" obj = u.toDictionary() obj[User.Key.BLESSED] = follow.blessed(user) if FollowList.Options.USER_FOLLOW in options: obj[User.Key.FOLLOW] = u.id() in userFollowIds objList.append(obj) return objList