123456789101112131415161718192021222324252627282930313233343536373839 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from typing import Any, List, Union
- from fastapi import Depends, Query
- from sqlalchemy.ext.asyncio import AsyncSession
- from common.const import RESOURCE_TYPES
- from crud.base import CrudManager
- from crud.resource import CATEGORY_CRUDS
- from models.resource import WorkResource, ExamResource, CATEGORY_MODES
- from models.user import Student, Teacher
- from utils.depends import get_async_db, get_current_user
- # 分类
- async def get_category_tree(crud: CrudManager,
- model: Union[WorkResource, ExamResource],
- db: AsyncSession = Depends(get_async_db),
- root: List[Any] = None):
- for item in root:
- children = await crud.fetch_all(db, filters=[model.pid == item.id])
- item.children = children
- await get_category_tree(crud, model, 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": "资源类型错误!"}
- else:
- crud = CATEGORY_CRUDS[ctype]
- model = CATEGORY_MODES[ctype]
- root = await crud.fetch_all(db, filters=[model.pid == 0])
- await get_category_tree(crud, model, db, root)
- return {"data": root}
|