Beispiel #1
0
def get_category_list(page: int = Query(1, description='页码'),
                      page_size: int = Query(settings.PAGE_SIZE, description='每页条数'),
                      search: str = Query(None, description='查询参数'),
                      current_user: User = Depends(get_current_user)):
    try:
        blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first()
        if blog_site is None:
            return fail_response('此用户未创建博客站点,请先创建')
        if not search:
            categories = Category.select().where(Category.blog_id == blog_site.id).order_by(Category.create_time.desc())
        else:
            categories = Category.select().where(
                Category.blog_id == blog_site.id,
                Category.title % f'%{search}%' |
                Category.description % f'%{search}%'
            ).order_by(Category.create_time.desc())
        paginate_categories = categories.paginate(page, page_size)
        paginate = {
            'page': page,
            'page_size': page_size,
            'total': categories.count()
        }
        category_list = []
        if not paginate_categories:
            return success_response({
                'paginate': paginate,
                'product_list': []
            })
        for category in paginate_categories:
            category_list.append(category.to_dict())
        data = {
            'paginate': paginate,
            'article_list': category_list
        }
    except Exception as e:
        logger.error(f'获取个人博客分类列表失败,失败原因:{e}')
        return fail_response('获取个人博客分类列表失败')
    return success_response(data)
Beispiel #2
0
def delete_category(category_id: str, current_user: User = Depends(get_current_user)):
    # 法一:
    # blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first()
    # if blog_site is None:
    #     return fail_response('此用户未创建博客站点,请先创建')
    # category = Category.filter(Category.id == category_id, Category.blog_id == blog_site.id).first()
    # if not category:
    #     return fail_response("博客分类不存在")
    # try:
    #     result = category.delete_instance()
    #     if result:
    #         return success_response('删除博客文章成功')
    #     return fail_response('更新博客文章失败')
    # except Exception as e:
    #     db.rollback()
    #     logger.error(f'更新博客文章失败,失败原因:{e}')
    #     return fail_response('删除博客文章失败')
    # 法二:使用联表查询的方式
    category = Category.select().join(BlogSite, on=(Category.blog_id == BlogSite.id)).where(
        BlogSite.user_id == current_user.uuid, Category.id == category_id)
    if not category:
        return fail_response("博客分类不存在")
    try:
        # 先将这个用户下这个分类的文章的分类改为空值
        articles = Article.filter(Article.category_id == category_id)
        for article in articles:
            article.category_id = None
            article.save()
        # 再将这个文章分类删掉
        query = Category.delete().where(Category.id == category_id)
        result = query.execute()  # 删除成功返回 1  所以不等于1为失败
        if result != 1:
            return fail_response('更新博客文章失败')
    except Exception as e:
        db.rollback()
        logger.error(f'更新博客文章失败,失败原因:{e}')
        return fail_response('删除博客文章失败')
    return success_response('删除博客文章成功')