question.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. from fastapi import Path, Depends
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from common.const import OBJECTIVE_QUESTION_TYPES
  5. from crud.paper import crud_question
  6. from models.paper import PaperQuestion
  7. from models.user import Teacher
  8. from utils.depends import get_async_db, get_current_user
  9. async def get_paper_questions(page: int = 1,
  10. size: int = 10,
  11. pid: int = Path(..., description="试卷ID"),
  12. db: AsyncSession = Depends(get_async_db),
  13. current_user: Teacher = Depends(get_current_user)):
  14. # 分页
  15. offset = (page - 1) * size
  16. total, db_questions = await crud_question.find_all(
  17. db,
  18. filters=[PaperQuestion.pid == pid,
  19. PaperQuestion.qtype.notin_(OBJECTIVE_QUESTION_TYPES)],
  20. offset=offset,
  21. limit=size)
  22. return {"data": db_questions, "total": total}
  23. # 查看答案
  24. async def get_question_answer(pid: int = Path(..., description="试卷ID"),
  25. qno: int = Path(..., description="题号"),
  26. sqno: int = Path(..., description="子题号"),
  27. db: AsyncSession = Depends(get_async_db),
  28. current_user: Teacher = Depends(get_current_user)):
  29. db_obj = await crud_question.find_one(db, filters={"pid": pid, "qno": qno, "sqno": sqno})
  30. if not db_obj:
  31. return {"errcode": 404, "mess": "试题不存在!"}
  32. return {"data": db_obj}