当前位置: 首页>>代码示例>>Python>>正文


Python DataApi.getVisionListLikeCount方法代码示例

本文整理汇总了Python中data.DataApi.DataApi.getVisionListLikeCount方法的典型用法代码示例。如果您正苦于以下问题:Python DataApi.getVisionListLikeCount方法的具体用法?Python DataApi.getVisionListLikeCount怎么用?Python DataApi.getVisionListLikeCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在data.DataApi.DataApi的用法示例。


在下文中一共展示了DataApi.getVisionListLikeCount方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: toDictionary

# 需要导入模块: from data.DataApi import DataApi [as 别名]
# 或者: from data.DataApi.DataApi import getVisionListLikeCount [as 别名]
    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:
#.........这里部分代码省略.........
开发者ID:nikilster,项目名称:projectAwesome,代码行数:103,代码来源:VisionList.py


注:本文中的data.DataApi.DataApi.getVisionListLikeCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。