def post(self, match_id, status_id):
        match = Match.get_or_404(id=match_id)
        status = MatchStatus.get_or_404(id=status_id)
        delete_photo_keys = self.get_argument("delete-photo-keys", "")

        form = MatchStatusForm(self.arguments)

        if not form.validate():
            self.render("match/statuses_edit.html",
                        match=match,
                        status=status,
                        form=form)
        else:
            if delete_photo_keys:
                delete_photo_keys_list = delete_photo_keys.split(",")
            else:
                delete_photo_keys_list = []

            if "photos" in self.request.files:
                photo_keys_list = self.upload_photos(match_id)
            else:
                photo_keys_list = []

            new_photos_set = set(status.photos) ^ set(delete_photo_keys_list)
            new_photos_set = new_photos_set.union(set(photo_keys_list))

            form.populate_obj(status)
            status.photos = list(new_photos_set)
            status.save()

            self.redirect(self.reverse_url("admin_match_statuses", match_id))
    def get(self, match_id, status_id):
        match = Match.get_or_404(id=match_id)
        status = MatchStatus.get_or_404(id=status_id)

        form = MatchStatusForm(obj=status)
        self.render("match/statuses_edit.html",
                    match=match,
                    status=status,
                    form=form)
 def delete(self, match_status_id):
     """
     用户取消点赞,
     该方法会取消当前登录用户对本动态的所有点赞
     :param match_status_id:
     :return:
     """
     match_status = MatchStatus.get_or_404(id=match_status_id)
     status = MatchStatusService.undo_like(user_id=self.current_user.id,
                                           match_status=match_status)
     self.write(MatchStatusSimpleSerializer(status).data)
    def get(self, match_status_id):
        match_status = MatchStatus.get_or_404(id=match_status_id)
        query = MatchStatusService.get_likes(match_status)
        page = self.paginate_query(query)

        data = self.render_page_info(page)

        users = set([like.user_id for like in page])
        likes = []
        if users:
            parteam = Parteam(self.settings["parteam_api_url"])
            users = parteam.parteam_user(list(users))
            likes = [
                users[uid].secure_info for uid in users if uid in users.keys()
            ]
        data.update({"likes": likes})
        self.write(data)
    def post(self, match_status_id):
        form = self.validated_arguments
        match_status = MatchStatus.get_or_404(id=match_status_id)
        inst = MatchComment.create(user_id=self.current_user.id,
                                   status=match_status,
                                   match=match_status.match_id,
                                   **form)

        self.set_status(201, reason="提交评论成功")

        if isinstance(self.current_user, Storage):
            parteam_users = {
                self.current_user.id: ParteamUser(self.current_user)
            }
        else:
            parteam_users = {}

        self.write(
            MatchCommentSerializer(inst, parteam_users=parteam_users).data)
    def get(self, match_status_id):
        match_status = MatchStatus.get_or_404(id=match_status_id)

        # 显示最近 n 条评论
        comments_count = self.get_query_argument("comments_count", 2)
        # 显示最近 n 个点赞
        likes_count = self.get_query_argument("likes_count", 5)

        comments = MatchComment.select()\
            .where(MatchComment.status == match_status_id)\
            .order_by(-MatchComment.created, -MatchComment.id)\
            .limit(comments_count)

        likes = MatchStatusLike\
            .select(MatchStatusLike.status, MatchStatusLike.user_id)\
            .where(MatchStatusLike.status == match_status_id)\
            .order_by(-MatchStatusLike.create_at, -MatchStatusLike.id)\
            .limit(likes_count)\
            .distinct()

        setattr(match_status, "comments_prefetch", comments)
        setattr(match_status, "likes_prefetch", likes)

        _uids = set()
        for like in likes:
            _uids.add(like.user_id)
        for comments in comments:
            _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 = MatchStatusSerializer(match_status,
                                           parteam_users=parteam_users)
        self.write(serializer.data)
 def post(self, match_status_id):
     match_status = MatchStatus.get_or_404(id=match_status_id)
     liked = MatchStatusService.do_like(user_id=self.current_user.id,
                                        match_status=match_status)
     self.set_status(204, reason="点赞成功")