本文整理汇总了Python中data.DataApi.DataApi.getVisionCommentListLikeCount方法的典型用法代码示例。如果您正苦于以下问题:Python DataApi.getVisionCommentListLikeCount方法的具体用法?Python DataApi.getVisionCommentListLikeCount怎么用?Python DataApi.getVisionCommentListLikeCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类data.DataApi.DataApi
的用法示例。
在下文中一共展示了DataApi.getVisionCommentListLikeCount方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: toDictionary
# 需要导入模块: from data.DataApi import DataApi [as 别名]
# 或者: from data.DataApi.DataApi import getVisionCommentListLikeCount [as 别名]
def toDictionary(self, options=[], user=None):
'''For packaging JSON objects
Pass in list of VisionComment.Options.* for extra information
'''
objs = []
commentList = self._commentModels
if self.length() > 0:
from Picture import Picture
idToPicture = dict()
if VisionComment.Options.PICTURE in options:
pictureIds = set([c.pictureId() for c in self.comments()])
pictureIds.discard(0)
pictures = Picture.getByIds(pictureIds)
idToPicture = dict([(picture.id(), picture)
for picture in pictures])
if VisionComment.Options.AUTHOR in options:
authorIds = set([comment.authorId()
for comment in self.comments()])
authors = DataApi.getUsersFromIds(authorIds)
idToAuthor = dict([(u.id, u) for u in authors])
# If LIKES
if VisionComment.Options.LIKES in options:
commentIds = [c.id() for c in self.comments()]
tuples = DataApi.getVisionCommentListLikeCount(commentIds)
idToLikeCount = dict([(commentId, count)
for commentId, count in tuples])
if user:
commentUserLikes = DataApi.getVisionCommentIdsLikedByUser(
commentIds, user.id())
for comment in commentList:
obj = VisionComment(comment).toDictionary()
if VisionComment.Options.AUTHOR in options:
from User import User
author = idToAuthor[comment.authorId]
obj[VisionComment.Key.AUTHOR] = User(author).toDictionary()
# If LIKES
if VisionComment.Options.LIKES in options:
obj[VisionComment.Key.LIKE] = \
{ VisionComment.Key.LIKE_COUNT :
idToLikeCount[comment.id] if
comment.id in idToLikeCount else 0 }
if user:
obj[VisionComment.Key.LIKE]\
[VisionComment.Key.USER_LIKE] =\
comment.id in commentUserLikes
if VisionComment.Options.PICTURE in options:
if comment.pictureId > 0 and \
comment.pictureId in idToPicture:
cp = idToPicture[comment.pictureId]
obj[VisionComment.Key.PICTURE] = cp.toDictionary()
objs.append(obj)
return objs
示例2: toDictionary
# 需要导入模块: from data.DataApi import DataApi [as 别名]
# 或者: from data.DataApi.DataApi import getVisionCommentListLikeCount [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:
#.........这里部分代码省略.........