question.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from fastapi import Depends, Query
  5. from sqlalchemy import text
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from crud.paper import crud_question, crud_paper
  8. from models.user import SysUser
  9. from schemas.base import ReturnField
  10. from schemas.paper.questions import QuestionInfo, SaveQuePieceInfo
  11. from utils.depends import get_async_db, get_current_user
  12. from utils.imgtool import download_remote_img, crop_img
  13. async def create_question(info: QuestionInfo,
  14. db: AsyncSession = Depends(get_async_db),
  15. current_user: SysUser = Depends(get_current_user)):
  16. info = info.dict(exclude_none=True)
  17. if not info:
  18. return {"errcode": 400, "mess": "请求参数为空!"}
  19. db_exist = await crud_question.find_one(
  20. db, {
  21. "pid": info["pid"],
  22. "pno": info["pno"],
  23. "qno": info["qno"],
  24. "sqno": info["sqno"]
  25. })
  26. if db_exist:
  27. return {"errcode": 400, "mess": "试题重复!"}
  28. info["creator_id"] = current_user.id
  29. info["creator_name"] = current_user.username
  30. db_obj = await crud_question.insert_one(db, info)
  31. return {"data": db_obj}
  32. async def update_question(qid: int,
  33. info: QuestionInfo,
  34. db: AsyncSession = Depends(get_async_db),
  35. current_user: SysUser = Depends(get_current_user)):
  36. info = info.dict(exclude_none=True)
  37. if not info:
  38. return {"errcode": 400, "mess": "请求参数为空!"}
  39. db_exist = await crud_question.find_one(db, {"id": qid})
  40. if not db_exist:
  41. return {"errcode": 400, "mess": "试题不存在!"}
  42. info["editor_id"] = current_user.id
  43. info["editor_name"] = current_user.username
  44. db_obj = await crud_question.update(db, db_exist, info)
  45. return {"data": db_obj}
  46. async def delete_question(qid: int,
  47. db: AsyncSession = Depends(get_async_db),
  48. current_user: SysUser = Depends(get_current_user)):
  49. existed = await crud_question.count(db, {"id": qid})
  50. if not existed:
  51. return {"errcode": 400, "mess": "试题不存在!"}
  52. await crud_question.delete(db, qid)
  53. return {"data": None}
  54. async def get_question(qid: int,
  55. db: AsyncSession = Depends(get_async_db),
  56. current_user: SysUser = Depends(get_current_user)):
  57. db_exist = await crud_question.find_one(db, {"id": qid})
  58. if not db_exist:
  59. return {"errcode": 400, "mess": "角色不存在!"}
  60. return {"data": db_exist}
  61. async def get_question_list(page: int = 1,
  62. size: int = 10,
  63. name: str = "",
  64. res: ReturnField = Query(
  65. "", description="控制返回字段,字段逗号分隔"),
  66. db: AsyncSession = Depends(get_async_db),
  67. current_user: SysUser = Depends(get_current_user)):
  68. filters = []
  69. if name:
  70. filters.append(text(f"name LIKE '%{name}%'"))
  71. offset = (page - 1) * size
  72. total, items = await crud_question.find_all(db,
  73. filters=filters,
  74. offset=offset,
  75. limit=size,
  76. return_fields=res)
  77. return {"total": total, "data": items}
  78. async def cut_imgs(std_img, points):
  79. """根据points完成图片切割
  80. """
  81. cut_urls = []
  82. for idx, point in enumerate(points):
  83. x = point["x"]
  84. y = point["y"]
  85. w = point["w"]
  86. h = point["h"]
  87. point = (x, y, x + w, y + h)
  88. orgimg = await download_remote_img(std_img)
  89. url = await crop_img(orgimg, point, idx)
  90. cut_urls.append(url)
  91. return cut_urls
  92. async def get_cut_imgs(db, current_user, info: SaveQuePieceInfo):
  93. """
  94. """
  95. db_exist = await crud_paper.find_one(db, {"id": info.pid})
  96. imgs = db_exist.imgs.split(";")
  97. cut_data = []
  98. add_ques = list(filter(lambda x: not x["id"], info.ques))
  99. for aq in add_ques:
  100. await crud_question.delete(db,
  101. where_clauses={
  102. "pid": info.pid,
  103. "qno": aq.qno,
  104. "sqno": aq.sqno
  105. })
  106. # 试题切割
  107. shiti_ques = filter(lambda x: x["imgtype"] == 0, add_ques)
  108. for que in shiti_ques:
  109. cut_urls = await cut_imgs(imgs[que.page], que.points)
  110. queinfo = QuestionInfo(
  111. **{
  112. "pid": info.pid,
  113. "pno": info.pno,
  114. "qno": que.qno,
  115. "sqno": que.sqno,
  116. "points": json.dumps(que.points),
  117. "imgs": json.dumps(cut_urls),
  118. "creator_id": current_user.id,
  119. "creator_name": current_user.username,
  120. })
  121. cut_data.append(queinfo)
  122. await crud_question.insert_many(db, cut_data)
  123. # 材料切割
  124. stuff_ques = filter(lambda x: x["imgtype"] == 1, add_ques)
  125. for que in stuff_ques:
  126. cut_urls = await cut_imgs(imgs[que.page], que.points)
  127. db_exist = await crud_question.find_one(db, {
  128. "pid": info.pid,
  129. "qno": que.qno,
  130. "sqno": que.sqno
  131. })
  132. if db_exist:
  133. upinfo = {
  134. "stuff_points": json.dumps(que.points),
  135. "stuff_imgs": json.dumps(cut_urls)
  136. }
  137. await crud_question.update(db, db_exist, upinfo)
  138. return len(cut_data)
  139. async def save_pieces(info: SaveQuePieceInfo,
  140. db: AsyncSession = Depends(get_async_db),
  141. current_user: SysUser = Depends(get_current_user)):
  142. info_dict = info.dict(exclude_none=True)
  143. if not info_dict:
  144. return {"errcode": 400, "mess": "请求参数为空!"}
  145. cut_data = await get_cut_imgs(db, current_user, info)
  146. return {"data": cut_data}