student.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from fastapi import Query, Depends
  4. from sqlalchemy import text, or_, asc
  5. from sqlalchemy.ext.asyncio import AsyncSession
  6. from crud.user import crud_student
  7. from models.user import Teacher, Student
  8. from utils.depends import get_async_db, get_current_user
  9. # 学生列表
  10. async def get_students(page: int = 1,
  11. size: int = 10,
  12. cid: str = Query(None, description="班级ID"),
  13. name: str = Query("", description="学生姓名"),
  14. _all: int = Query(0, description="是否获取全部数据,1=是,0=否,默认为0"),
  15. db: AsyncSession = Depends(get_async_db),
  16. current_user: Teacher = Depends(get_current_user)):
  17. # 过滤条件
  18. _q = []
  19. if (not cid) or (cid == "0"):
  20. class_ids = [int(x) for x in current_user.class_id.split(',')]
  21. _q.append(Student.class_id.in_(class_ids))
  22. else:
  23. _q.append(Student.class_id == int(cid))
  24. if name:
  25. _q.append(or_(Student.sno == name, Student.sno == name))
  26. # 为错题推送特别设置_all参数
  27. if _all:
  28. limit = 1000
  29. offset = 0
  30. else:
  31. limit = size
  32. offset = (page - 1) * size
  33. return_fields = ["id", "name", "sno", "sex", "age", "phone", "class_id", "class_name"]
  34. total, items = await crud_student.find_all(db,
  35. filters=_q,
  36. return_fields=return_fields,
  37. offset=offset,
  38. limit=limit,
  39. order_by=[asc("id")])
  40. return {"total": total, "data": items}