1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from fastapi import Query, Depends
- from sqlalchemy import text, or_, asc
- from sqlalchemy.ext.asyncio import AsyncSession
- from crud.user import crud_student
- from models.user import Teacher, Student
- from utils.depends import get_async_db, get_current_user
- # 学生列表
- async def get_students(page: int = 1,
- size: int = 10,
- cid: str = Query(None, description="班级ID"),
- name: str = Query("", description="学生姓名"),
- _all: int = Query(0, description="是否获取全部数据,1=是,0=否,默认为0"),
- db: AsyncSession = Depends(get_async_db),
- current_user: Teacher = Depends(get_current_user)):
- # 过滤条件
- _q = []
- if (not cid) or (cid == "0"):
- class_ids = [int(x) for x in current_user.class_id.split(',')]
- _q.append(Student.class_id.in_(class_ids))
- else:
- _q.append(Student.class_id == int(cid))
- if name:
- _q.append(or_(Student.sno == name, Student.sno == name))
- # 为错题推送特别设置_all参数
- if _all:
- limit = 1000
- offset = 0
- else:
- limit = size
- offset = (page - 1) * size
- return_fields = ["id", "name", "sno", "sex", "age", "phone", "class_id", "class_name"]
- total, items = await crud_student.find_all(db,
- filters=_q,
- return_fields=return_fields,
- offset=offset,
- limit=limit,
- order_by=[asc("id")])
- return {"total": total, "data": items}
|