def repostUsers(self): reposts = self.reposts() userIds = set() users = [] if reposts.length() > 0: from User import User for vision in reposts.visions(): userIds.add(vision.userId()) users = User.getByUserIds(userIds) return users
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
def toDictionary(self, options=[], user=None): '''For packaging to JSON output. options input is list of Vision.Options when we want more information fetched from DB and placed into objects This batches up queries for pictures and comments across all the visions. Whenever we want to get all this data across a list of visions, it is good to use VisionList.toDictionary() instead of the individual toDictionary() calls on objects. 'user' parameter is only used when LIKES in provided as an option. If it is passed in, we also check whether this user likes the visions or not. ''' from User import User retObj = [] # Start collecting picture ids pictureIds = set() if Vision.Options.PICTURE in options: pictureIds = set([vision.pictureId() for vision in self.visions()]) # Get parent users if we need them parentUserIds = set() if Vision.Options.PARENT_USER in options: parentVisionIds = set([vision.parentId() for vision in self.visions()]) parentVisionIds.discard(0) parentVisions = VisionList.getByIds(parentVisionIds, allowRemovedVisions=True) idToParentVisions = dict([(v.id(), v) for v in parentVisions.visions()]) parentUserIds = set([parent.userId() for parent in parentVisions.visions()]) # Get all users (including parent users if needed) userIds = set([vision.userId() for vision in self.visions()]) users = User.getByUserIds(userIds.union(parentUserIds)) idToUser = dict([(u.id(), u) for u in users]) # If COMMENTS, get comments if Vision.Options.COMMENTS in options: commentList = VisionCommentList.getEmptyList() for vision in self.visions(): # TODO: Speed up performance of this some time! commentList.extend(vision.comments(5)) idToComments = {} for comment in commentList.comments(): if not comment.visionId() in idToComments: idToComments[comment.visionId()] = [comment] else: idToComments[comment.visionId()].append(comment) if Vision.Options.COMMENT_PICTURES: if comment.hasPicture(): pictureIds.add(comment.pictureId()) authorIds = set([comment.authorId() for comment in commentList.comments()]) authors = User.getByUserIds(authorIds) idToAuthor = dict([(author.id(), author) for author in authors]) # If COMMENT_LIKES if Vision.Options.COMMENT_LIKES in options: commentIds = [c.id() for c in commentList.comments()] tuples = DataApi.getVisionCommentListLikeCount(commentIds) idToCommentLikes = dict([(commentId, count) for commentId, count in tuples]) if user: commentUserLikes = DataApi.getVisionCommentIdsLikedByUser( commentIds, user.id()) # If LIKES, get vision likes in batch if Vision.Options.LIKES in options: visionIds = [v.id() for v in self.visions()] tuples = DataApi.getVisionListLikeCount(visionIds) idToLikes = dict([(visionId, count) for visionId, count in tuples]) if user: userLikes = DataApi.getVisionIdsLikedByUser(visionIds, user.id()) # Get pictures and hash them if (Vision.Options.PICTURE in options) or \ (Vision.Options.COMMENT_PICTURES in options): pictureIds.discard(0) pictures = Picture.getByIds(pictureIds) idToPicture = dict([(picture.id(), picture) for picture in pictures]) idToPicture[0] = "" # Now start building object list to return for vision in self.visions(): obj = vision.toDictionary() if Vision.Options.USER in options: obj[Vision.Key.USER] = idToUser[vision.userId()].toDictionary() # If PICTURE if Vision.Options.PICTURE in options: if vision.pictureId != 0: picture = idToPicture[vision.pictureId()].toDictionary() obj[Vision.Key.PICTURE] = picture # If COMMENTS if Vision.Options.COMMENTS in options: obj[Vision.Key.COMMENTS] = [] if vision.id() in idToComments: for comment in idToComments[vision.id()]: commentObj = comment.toDictionary() author = idToAuthor[comment.authorId()] commentObj[VisionComment.Key.AUTHOR] = author.toDictionary() if Vision.Options.COMMENT_LIKES in options: commentObj[VisionComment.Key.LIKE] = { VisionComment.Key.LIKE_COUNT: idToCommentLikes[comment.id()] if comment.id() in idToCommentLikes else 0 } if user: commentObj[VisionComment.Key.LIKE]\ [VisionComment.Key.USER_LIKE] = \ comment.id() in commentUserLikes if Vision.Options.COMMENT_PICTURES in options: if comment.hasPicture() and\ comment.pictureId() in idToPicture: cp = idToPicture[comment.pictureId()] commentObj[VisionComment.Key.PICTURE] =\ cp.toDictionary() obj[Vision.Key.COMMENTS].append(commentObj) # If PARENT_USER if Vision.Options.PARENT_USER in options: if not vision.isOriginalVision(): if vision.parentId() in idToParentVisions: parentVision = idToParentVisions[vision.parentId()] if parentVision.userId() in idToUser: parentUser = idToUser[parentVision.userId()] obj[Vision.Key.PARENT_USER] = \ parentUser.toDictionary() # If LIKES if Vision.Options.LIKES in options: obj[Vision.Key.LIKE] = { Vision.Key.LIKE_COUNT: idToLikes[vision.id()] if vision.id() in idToLikes else 0 } if user: obj[Vision.Key.LIKE][Vision.Key.USER_LIKE] = \ vision.id() in userLikes # finally append object to list retObj.append(obj) return retObj