B-4 逐条勾选:入库前把一块里的题摊开,只收勾上的(prepare 接口 + 弹窗,239 测试)

This commit is contained in:
2026-11-22 15:30:47 +08:00
parent 11a7f18b31
commit ef201ffacc
13 changed files with 305 additions and 15 deletions
+81 -9
View File
@@ -1716,12 +1716,65 @@ def dev_scan_confirm(conn, code: str) -> dict:
return out
def prepare_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
picks: list) -> dict:
"""入库前的「逐条勾选」预览:把勾中的块认一遍,列出里面的每一道小题。
为什么要单独一步:口算卡一块十几道,切题偶尔会把相邻的题读进来、甚至读歪
(`27+69` 读成 `77+69`)。整块入库会把孩子没做错的题也塞进错题本,家长得
事后一条条删。所以入库前把这几道摊开给家长看,取消掉没做错的。
这一步只读不写。它调的 `recognize_block` 与真正入库时同一套(裁剪图按
sha1 命中 OCR 缓存),所以预览过一遍之后,正式入库**不会再花一次调用**。
"""
from . import scanflow
require_active(conn, gid)
get_child(conn, gid, cid)
src, meta = scanflow.load_draft(draft_id)
blocks = meta.get("blocks") or []
items = []
for p in picks:
try:
i = int(p if not isinstance(p, dict) else p.get("i"))
except (TypeError, ValueError):
continue
if not (0 <= i < len(blocks)):
continue
r = scanflow.recognize_block(src, blocks[i])
subs = r.get("subs") or []
if subs:
for k, x in enumerate(subs):
items.append({"block": i, "sub": k, "stem": x.get("stem") or "",
"judge_type": "calc", "answer": x.get("answer"),
"options": None, "source": r.get("source")})
else:
items.append({"block": i, "sub": None, "stem": r.get("stem") or "",
"judge_type": r.get("judge_type"), "answer": r.get("answer"),
"options": r.get("options"), "source": r.get("source")})
if not items:
raise ApiError(400, "NO_PICKS", "一道都没选,先点上哪几道错了")
if len(items) > 60:
items = items[:60]
return {"draft_id": draft_id, "items": items,
"hint": "这些是勾中的块里认出来的题。取消掉没做错的那几道,再入库。"}
def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
picks: list[int]) -> dict:
picks: list) -> dict:
"""把「家长勾中的那几块」入库。
流程是:整页拍 → 切块给家长点 → 选中的块从原图裁出来**再单独识别一次**。
单块图字更大、没有相邻题干扰,题干比整页切时干净得多。
picks 每一项可以是:
- `3`(块序号):整块都要,块里若是口算卡就展开成全部小问;
- `{"i": 3, "subs": [0, 2]}`:只要这块里的第 0、2 个小问——家长在
「这块里有 8 道,取消你没做错的」那一步勾出来的结果。
为什么要第二种:口算卡一块十几道,切题偶尔会把相邻的题读进来(甚至读歪),
整块入库会把没做错的题也塞进错题本。逐条勾选把最后这个决定权还给家长。
"""
from . import scanflow
@@ -1734,13 +1787,27 @@ def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
src, meta = scanflow.load_draft(draft_id)
blocks = meta.get("blocks") or []
chosen = []
for i in picks:
for p in picks:
subs_only = None
if isinstance(p, dict):
raw_i, raw_subs = p.get("i"), p.get("subs")
if isinstance(raw_subs, list):
sub_idx = set()
for s in raw_subs:
try:
sub_idx.add(int(s))
except (TypeError, ValueError):
continue
subs_only = sub_idx
p = raw_i
try:
i = int(i)
i = int(p)
except (TypeError, ValueError):
continue
if 0 <= i < len(blocks):
chosen.append(blocks[i])
if subs_only is not None and not subs_only:
continue # 这块里的小题全被取消 = 这块不入库
chosen.append((blocks[i], subs_only))
if not chosen:
raise ApiError(400, "NO_PICKS", "一道都没选,先点上哪几道错了")
if len(chosen) > 20:
@@ -1748,15 +1815,20 @@ def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
# 口算卡(一块里十几道)先展开成多道,再往库里写
questions = []
for b in chosen:
for b, subs_only in chosen:
r = scanflow.recognize_block(src, b)
subs = r.get("subs") or []
if subs:
questions.extend({"stem": x.get("stem") or "", "judge_type": "calc",
"options": None, "answer": x.get("answer"),
"page_answer": None, "source": r.get("source")} for x in subs)
else:
keep = subs_only if subs_only is not None else range(len(subs))
for k in keep:
if 0 <= k < len(subs):
x = subs[k]
questions.append({"stem": x.get("stem") or "", "judge_type": "calc",
"options": None, "answer": x.get("answer"),
"page_answer": None, "source": r.get("source")})
elif subs_only is None:
questions.append(r)
# subs_only 给了但这块其实只有一道题:说明前端的勾选过期了,跳过不猜
# 切题不给知识点;这里按题干补一次,补不上就留空(学情分析宁可少,不可错)
for q in questions: