12345678910111213141516171819202122232425262728293031323334353637 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from typing import Any, List, Union
- from fastapi import Depends, Query
- from sqlalchemy import text
- from sqlalchemy.ext.asyncio import AsyncSession
- from common.const import RESOURCE_TYPES
- from crud.base import CrudManager
- from crud.resource import crud_work_category, crud_exam_category
- from models.user import SysUser, Student, Teacher
- from utils.depends import get_async_db, get_current_user
- # 分类
- async def get_category_tree(crud: CrudManager,
- db: AsyncSession = Depends(get_async_db),
- root: List[Any] = None):
- for item in root:
- children = await crud.fetch_all(db, filters=[text(f"pid={item.id}")])
- item.children = children
- await get_category_tree(crud, db, item.children)
- async def get_categories(
- ctype: str = Query(..., description="资源类型,exam / work"),
- db: AsyncSession = Depends(get_async_db),
- current_user: Union[Teacher, Student] = Depends(get_current_user)):
- if ctype not in RESOURCE_TYPES:
- return {"errcode": 400, "mess": "资源类型错误!"}
- crud = crud_work_category if ctype == "work" else crud_exam_category
- root = await crud.fetch_all(db, filters=[text("pid = 0")])
- await get_category_tree(crud, db, root)
- return {"data": root}
|