Пример #1
0
    def post(self):
        """
        评论点赞
        """
        json_parser = RequestParser()
        json_parser.add_argument('target',
                                 type=parser.comment_id,
                                 required=True,
                                 location='json')
        args = json_parser.parse_args()
        target = args.target
        ret = 1
        try:
            comment_liking = CommentLiking(user_id=g.user_id,
                                           comment_id=target)
            db.session.add(comment_liking)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            ret = CommentLiking.query.filter_by(user_id=g.user_id, comment_id=target, is_deleted=True) \
                .update({'is_deleted': False})
            db.session.commit()

        if ret > 0:
            cache_statistic.CommentLikingCountStorage.incr(target)
            cache_user.UserCommentLikingCache(g.user_id).clear()
        return {'target': target}, 201
Пример #2
0
 def delete(self, target):
     """
     取消对评论点赞
     """
     ret = CommentLiking.query.filter_by(user_id=g.user_id, comment_id=target, is_deleted=False) \
         .update({'is_deleted': True})
     db.session.commit()
     if ret > 0:
         cache_statistic.CommentLikingCountStorage.incr(target, -1)
         cache_user.UserCommentLikingCache(g.user_id).clear()
     return {'message': 'OK'}, 204
Пример #3
0
    def get(self):
        """
        获取评论列表
        """
        # /comments?type,source,offset,limit
        # return = {
        #     'results': [
        #         {
        #             'com_id': 0,
        #             'aut_id': 0,
        #             'aut_name': '',
        #             'aut_photo': '',
        #             'like_count': 0,
        #             'reply_count': 0,
        #             'pubdate': '',
        #             'content': ''
        #         }
        #     ],
        #     'total_count': 0,
        #     'last_id': 0,
        #     'end_id': 0,
        # }
        qs_parser = RequestParser()
        qs_parser.add_argument('type',
                               type=self._comment_type,
                               required=True,
                               location='args')
        qs_parser.add_argument('source',
                               type=positive,
                               required=True,
                               location='args')
        qs_parser.add_argument('offset',
                               type=positive,
                               required=False,
                               location='args')
        qs_parser.add_argument('limit',
                               type=int_range(
                                   constants.DEFAULT_COMMENT_PER_PAGE_MIN,
                                   constants.DEFAULT_COMMENT_PER_PAGE_MAX,
                                   argument='limit'),
                               required=False,
                               location='args')
        args = qs_parser.parse_args()
        limit = args.limit if args.limit is not None else constants.DEFAULT_COMMENT_PER_PAGE_MIN

        if args.type == 'a':
            # 文章评论
            article_id = args.source
            total_count, end_id, last_id, ret = cache_comment.ArticleCommentsCache(
                article_id).get_page(args.offset, limit)
        else:
            # 评论的评论
            comment_id = args.source
            total_count, end_id, last_id, ret = cache_comment.CommentRepliesCache(
                comment_id).get_page(args.offset, limit)

        results = cache_comment.CommentCache.get_list(ret)

        if g.user_id:
            user_comment_liking_cache = cache_user.UserCommentLikingCache(
                g.user_id)
            for comment in results:
                comment[
                    'is_liking'] = user_comment_liking_cache.determine_liking_comment(
                        comment['com_id'])
        else:
            for comment in results:
                comment['is_liking'] = False

        return {
            'total_count': total_count,
            'end_id': end_id,
            'last_id': last_id,
            'results': results
        }