Beispiel #1
0
    def get(self):
        """
        获取用户的收藏历史
        """
        qs_parser = RequestParser()
        qs_parser.add_argument('page',
                               type=inputs.positive,
                               required=False,
                               location='args')
        qs_parser.add_argument('per_page',
                               type=inputs.int_range(
                                   constants.DEFAULT_ARTICLE_PER_PAGE_MIN,
                                   constants.DEFAULT_ARTICLE_PER_PAGE_MAX,
                                   'per_page'),
                               required=False,
                               location='args')
        args = qs_parser.parse_args()
        page = 1 if args.page is None else args.page
        per_page = args.per_page if args.per_page else constants.DEFAULT_ARTICLE_PER_PAGE_MIN

        total_count, collections = cache_user.UserArticleCollectionsCache(
            g.user_id).get_page(page, per_page)

        results = []
        for article_id in collections:
            article = cache_article.ArticleInfoCache(article_id).get()
            results.append(article)

        return {
            'total_count': total_count,
            'page': page,
            'per_page': per_page,
            'results': results
        }
Beispiel #2
0
    def post(self):
        """
        用户收藏文章
        """
        req_parser = RequestParser()
        req_parser.add_argument('target', type=parser.article_id, required=True, location='json')
        req_parser.add_argument('Trace', type=inputs.regex(r'^.+$'), required=False, location='headers')
        args = req_parser.parse_args()

        target = args.target

        # 记录埋点日志
        if args.Trace:
            article = cache_article.ArticleInfoCache(target).get()
            write_trace_log(args.Trace, channel_id=article['ch_id'])

        ret = 1
        try:
            collection = Collection(user_id=g.user_id, article_id=target)
            db.session.add(collection)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            ret = Collection.query.filter_by(user_id=g.user_id, article_id=target, is_deleted=True) \
                .update({'is_deleted': False})
            db.session.commit()

        if ret > 0:
            cache_user.UserArticleCollectionsCache(g.user_id).clear()
            cache_statistic.ArticleCollectingCountStorage.incr(target)
            cache_statistic.UserArticleCollectingCountStorage.incr(g.user_id)

        return {'target': target}, 201
Beispiel #3
0
    def delete(self, target):
        """
        用户取消收藏
        """
        ret = Collection.query.filter_by(user_id=g.user_id, article_id=target, is_deleted=False) \
            .update({'is_deleted': True})
        db.session.commit()

        if ret > 0:
            cache_user.UserArticleCollectionsCache(g.user_id).clear()
            cache_statistic.ArticleCollectingCountStorage.incr(target, -1)
            cache_statistic.UserArticleCollectingCountStorage.incr(g.user_id, -1)
        return {'message': 'OK'}, 204
Beispiel #4
0
    def get(self, article_id):
        """
        获取文章详情
        :param article_id: int 文章id
        """
        user_id = g.user_id
        # 查询文章数据
        exist = cache_article.ArticleInfoCache(article_id).exists()
        if not exist:
            abort(404, message='The article does not exist.')

        article = cache_article.ArticleDetailCache(article_id).get()

        article['is_followed'] = False
        article['attitude'] = None
        # 增加用户是否收藏了文章
        article['is_collected'] = False

        if user_id:
            # 非匿名用户添加用户的阅读历史
            try:
                cache_user.UserReadingHistoryStorage(user_id).save(article_id)
            except ConnectionError as e:
                current_app.logger.error(e)

            # 查询关注
            # article['is_followed'] = cache_user.UserFollowingCache(user_id).determine_follows_target(article['aut_id'])
            article['is_followed'] = cache_user.UserRelationshipCache(
                user_id).determine_follows_target(article['aut_id'])

            # 增加用户是否收藏了文章
            article['is_collected'] = cache_user.UserArticleCollectionsCache(
                g.user_id).determine_collect_target(article_id)

        # 更新阅读数
        cache_statistic.ArticleReadingCountStorage.incr(article_id)
        cache_statistic.UserArticlesReadingCountStorage.incr(article['aut_id'])

        return article
Beispiel #5
0
    def get(self, article_id):
        """
        获取文章详情
        :param article_id: int 文章id
        """
        # 写入埋点日志
        qs_parser = RequestParser()
        qs_parser.add_argument('Trace', type=inputs.regex(r'^.+$'), required=False, location='headers')
        args = qs_parser.parse_args()

        user_id = g.user_id

        # 查询文章数据
        exist = cache_article.ArticleInfoCache(article_id).exists()
        if not exist:
            abort(404, message='The article does not exist.')

        article = cache_article.ArticleDetailCache(article_id).get()

        # 推荐系统所需埋点
        if args.Trace:
            write_trace_log(args.Trace, channel_id=article['ch_id'])

        article['is_followed'] = False
        article['attitude'] = None
        # 增加用户是否收藏了文章
        article['is_collected'] = False

        if user_id:
            # 非匿名用户添加用户的阅读历史
            try:
                cache_user.UserReadingHistoryStorage(user_id).save(article_id)
            except ConnectionError as e:
                current_app.logger.error(e)

            # 查询关注
            # article['is_followed'] = cache_user.UserFollowingCache(user_id).determine_follows_target(article['aut_id'])
            article['is_followed'] = cache_user.UserRelationshipCache(user_id).determine_follows_target(article['aut_id'])

            # 查询登录用户对文章的态度(点赞or不喜欢)
            try:
                # article['attitude'] = cache_article.ArticleUserAttitudeCache(user_id, article_id).get()
                article['attitude'] = cache_user.UserArticleAttitudeCache(user_id).get_article_attitude(article_id)
            except SQLAlchemyError as e:
                current_app.logger.error(e)
                article['attitude'] = -1

            # 增加用户是否收藏了文章
            article['is_collected'] = cache_user.UserArticleCollectionsCache(g.user_id).determine_collect_target(article_id)

        # 获取相关文章推荐
        article['recomments'] = []
        try:
            similar_articles = self._feed_similar_articles(article_id)
            for _article_id in similar_articles:
                _article = cache_article.ArticleInfoCache(_article_id).get()
                article['recomments'].append({
                    'art_id': _article['art_id'],
                    'title': _article['title']
                })
        except Exception as e:
            current_app.logger.error(e)

        # 更新阅读数
        cache_statistic.ArticleReadingCountStorage.incr(article_id)
        cache_statistic.UserArticlesReadingCountStorage.incr(article['aut_id'])

        return article