Esempio n. 1
0
def delete_article(article_id: str,
                   current_user: User = Depends(get_current_user)):
    article = Article.filter(Article.id == article_id, Article.user_id == current_user.uuid).first()
    if not article:
        return fail_response('此文章不存在')
    title = article.title
    try:
        # 之前这个是 物理删除
        # result = article.delete_instance()
        # 删除改为逻辑删除
        query = Article.update(is_delete=True).where(
            Article.id == article_id, Article.user_id == current_user.uuid)
        result = query.execute()
        # print(result, "运行后的结果")
        if result:
            res = redis_client.exists("article_rank_list")
            if res:
                # 存在了但是这个文章
                res_score = redis_client.zscore("article_rank_list", "article_%s_%s" % (article_id, title))
                if res_score:
                    # 删除之前的分数记录
                    redis_client.zrem("article_rank_list", "article_%s_%s" % (article_id, title))
            return success_response('删除博客文章成功')
        return fail_response('删除博客文章失败')
    except Exception as e:
        db.rollback()
        logger.error(f'删除博客文章失败,失败原因:{e}')
        return fail_response('删除博客文章失败')
Esempio n. 2
0
def update_article(article_id: str,
                   new_article: UpdateArticle,
                   current_user: User = Depends(get_current_user)):
    title = new_article.title
    desc = new_article.desc
    content = new_article.content
    category_id = new_article.category_id
    article = Article.filter(Article.id == article_id, Article.user_id == current_user.uuid,
                             Article.is_delete == 0).first()
    if not article:
        return fail_response('此文章不存在')
    try:
        query = Article.update(title=title, desc=desc, content=content, category_id=category_id,
                               update_time=str(datetime.now)).where(
            Article.id == article_id, Article.user_id == current_user.uuid)
        query.execute()
        # 修改排行榜的标题
        res = redis_client.exists("article_rank_list")
        if not res:
            # 连有序集合都没有的情况 初始化分数 0
            redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): 0})
            # print(redis_client.zscore("article_rank_list", "article_id_1"))
        else:
            # 存在了但是这个文章
            res_score = redis_client.zscore("article_rank_list", "article_%s_%s" % (article_id, title))
            if res_score is None:
                # 初始化分数
                redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): 0})
            else:
                # 删除之前的分数记录
                redis_client.zrem("article_rank_list", "article_%s_%s" % (article_id, title))

                # 重新建立一个并将之前的分数给这个
                redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): res_score})
            # 存在这个key的时候就分值增加 1
            redis_client.zincrby("article_rank_list", 1, "article_%s_%s" % (article_id, title))
        return success_response('更新博客文章成功')
    except Exception as e:
        db.rollback()
        logger.error(f'更新博客文章失败,失败原因:{e}')
        return fail_response('更新博客文章失败')