def get(self, match_id): match = Match.get_or_404(id=match_id) query = MatchStatus.select( MatchStatus, ).where(MatchStatus.match_id == match.id).order_by( MatchStatus.created.desc()) statuses = self.paginate_query(query) self.render("match/statuses.html", statuses=statuses, match=match)
def get(self, match_id): """ 获取赛事动态列表 """ # 显示最近 n 条评论 comments_count = self.get_query_argument("comments_count", 2) # 显示最近 n 个点赞 likes_count = self.get_query_argument("likes_count", 5) query = MatchStatus.select()\ .where(MatchStatus.match_id == match_id)\ .order_by(MatchStatus.created.desc(), MatchStatus.id.desc()) CommentAlias = MatchComment.alias() # type: MatchComment subquery = CommentAlias.select(fn.COUNT(CommentAlias.id))\ .where(CommentAlias.status == MatchComment.status, CommentAlias.id >= MatchComment.id) comments = MatchComment.select().where(subquery <= comments_count)\ .order_by(MatchComment.created.desc(), MatchComment.id.desc()) likes = MatchStatusLike\ .select(MatchStatusLike.status, MatchStatusLike.user_id)\ .order_by(-MatchStatusLike.create_at, -MatchStatusLike.id)\ .limit(likes_count)\ .distinct() status_with_comments = prefetch(query, comments, likes) page = self.paginate_query(status_with_comments) _uids = set() for row in page: for like in row.likes_prefetch: _uids.add(like.user_id) for comments in row.comments_prefetch: _uids.add(comments.user_id) parteam_users = dict() if _uids: pt = Parteam(self.settings["parteam_api_url"]) parteam_users = pt.parteam_user(list(_uids)) serializer_kwargs = {"parteam_users": parteam_users} data = self.get_paginated_data(page=page, alias="match_status", serializer=MatchStatusSerializer, serializer_kwargs=serializer_kwargs) self.write(data)