def create_category(app: Flask) -> Category: category = Category("test") with app.app_context(): db.session.add(category) db.session.commit() category = Category.query.filter_by(name="test").first() return category
def create_categories(app: Flask) -> None: category_names = ["test" + str(i) for i in range(10)] with app.app_context(): for name in category_names: category = Category(name) db.session.add(category) db.session.commit()
def get_category_info(category_id: str, up_category: UpdateCategory, 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: query = Category.update(title=up_category.title, description=up_category.description, update_time=str(datetime.now)).where( Category.id == category_id, Category.blog_id == blog_site.id) query.execute() return success_response('更新博客分类成功') except Exception as e: db.rollback() logger.error(f'更新博客分类失败,失败原因:{e}') return fail_response('更新博客分类失败')
def get_category_info(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 category is None: return fail_response("博客分类不存在") return success_response(category.to_dict())
def create_categories(app: Flask) -> None: print("create some fake categories...") with app.app_context(): categorie_names = ["python", "php", "javascript", "c++", "world"] for name in categorie_names: category = Category(name) db.session.add(category) db.session.commit()
def create_category(*, new_category: CreateCategory, current_user: User = Depends(get_current_user)): title = new_category.title description = new_category.description blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first() if blog_site is None: return fail_response('此用户未创建博客站点,请先创建') category = Category.filter(Category.title == title, Category.blog_id == blog_site.id).first() if category: return fail_response("相同分类已存在!") try: Category.create(title=title, description=description, blog_id=blog_site.id) except Exception as e: db.rollback() logger.error(f'创建博客文章失败,失败原因:{e}') return fail_response('创建博客文章失败') return success_response('创建博客文章成功')
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('删除博客文章成功')
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)
def post(self): data = request.get_json() or {} name = data.get("name") if not name: return invalid_api_usage("No name provided", 400) if Category.query.filter_by(name=name).first(): return invalid_api_usage("Name is already exist", 400) category = Category(name) db.session.add(category) db.session.commit() return { "name": category.name, "slug": category.slug, }, 201