#!/usr/bin/env python # -*- coding: utf-8 -*- import json from fastapi import Depends, Query from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncSession from crud.paper import crud_question, crud_paper from models.user import SysUser from schemas.base import ReturnField from schemas.paper.questions import QuestionInfo, SaveQuePieceInfo from utils.depends import get_async_db, get_current_user from utils.imgtool import download_remote_img, crop_img async def create_question(info: QuestionInfo, db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): info = info.dict(exclude_none=True) if not info: return {"errcode": 400, "mess": "请求参数为空!"} db_exist = await crud_question.find_one( db, { "pid": info["pid"], "pno": info["pno"], "qno": info["qno"], "sqno": info["sqno"] }) if db_exist: return {"errcode": 400, "mess": "试题重复!"} info["creator_id"] = current_user.id info["creator_name"] = current_user.username db_obj = await crud_question.insert_one(db, info) return {"data": db_obj} async def update_question(qid: int, info: QuestionInfo, db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): info = info.dict(exclude_none=True) if not info: return {"errcode": 400, "mess": "请求参数为空!"} db_exist = await crud_question.find_one(db, {"id": qid}) if not db_exist: return {"errcode": 400, "mess": "试题不存在!"} info["editor_id"] = current_user.id info["editor_name"] = current_user.username db_obj = await crud_question.update(db, db_exist, info) return {"data": db_obj} async def delete_question(qid: int, db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): existed = await crud_question.count(db, {"id": qid}) if not existed: return {"errcode": 400, "mess": "试题不存在!"} await crud_question.delete(db, qid) return {"data": None} async def get_question(qid: int, db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): db_exist = await crud_question.find_one(db, {"id": qid}) if not db_exist: return {"errcode": 400, "mess": "角色不存在!"} return {"data": db_exist} async def get_question_list(page: int = 1, size: int = 10, name: str = "", res: ReturnField = Query( "", description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): filters = [] if name: filters.append(text(f"name LIKE '%{name}%'")) offset = (page - 1) * size total, items = await crud_question.find_all(db, filters=filters, offset=offset, limit=size, return_fields=res) return {"total": total, "data": items} async def cut_imgs(std_img, points): """根据points完成图片切割 """ cut_urls = [] for idx, point in enumerate(points): x = point["x"] y = point["y"] w = point["w"] h = point["h"] point = (x, y, x + w, y + h) orgimg = await download_remote_img(std_img) url = await crop_img(orgimg, point, idx) cut_urls.append(url) return cut_urls async def get_cut_imgs(db, current_user, info: SaveQuePieceInfo): """ """ db_exist = await crud_paper.find_one(db, {"id": info.pid}) imgs = db_exist.imgs.split(";") cut_data = [] add_ques = list(filter(lambda x: not x["id"], info.ques)) for aq in add_ques: await crud_question.delete(db, where_clauses={ "pid": info.pid, "qno": aq.qno, "sqno": aq.sqno }) # 试题切割 shiti_ques = filter(lambda x: x["imgtype"] == 0, add_ques) for que in shiti_ques: cut_urls = await cut_imgs(imgs[que.page], que.points) queinfo = QuestionInfo( **{ "pid": info.pid, "pno": info.pno, "qno": que.qno, "sqno": que.sqno, "points": json.dumps(que.points), "imgs": json.dumps(cut_urls), "creator_id": current_user.id, "creator_name": current_user.username, }) cut_data.append(queinfo) await crud_question.insert_many(db, cut_data) # 材料切割 stuff_ques = filter(lambda x: x["imgtype"] == 1, add_ques) for que in stuff_ques: cut_urls = await cut_imgs(imgs[que.page], que.points) db_exist = await crud_question.find_one(db, { "pid": info.pid, "qno": que.qno, "sqno": que.sqno }) if db_exist: upinfo = { "stuff_points": json.dumps(que.points), "stuff_imgs": json.dumps(cut_urls) } await crud_question.update(db, db_exist, upinfo) return len(cut_data) async def save_pieces(info: SaveQuePieceInfo, db: AsyncSession = Depends(get_async_db), current_user: SysUser = Depends(get_current_user)): info_dict = info.dict(exclude_none=True) if not info_dict: return {"errcode": 400, "mess": "请求参数为空!"} cut_data = await get_cut_imgs(db, current_user, info) return {"data": cut_data}