feat: 决策59-63--类似清单对比区右栏/价格目录树改版/导入整份拒绝/数据目录拍平 + det类型传参修复
This commit is contained in:
@@ -21,9 +21,9 @@ ROOT = os.path.join(APP_DIR, "ui") # 前端页面目录
|
||||
CORE = os.path.join(APP_DIR, "core") # 解析器
|
||||
DATA_DIR = os.path.join(PRJ_DIR, "data")
|
||||
SRC_DIR = os.path.join(os.path.dirname(PRJ_DIR), "后台需要导入的项目") # 批量导入参考目录
|
||||
UPLOAD_DIR = os.path.join(DATA_DIR, "uploads") # 导入的文件副本(存在软件内)
|
||||
ATTACH_DIR = os.path.join(DATA_DIR, "attachments") # 价格批次原件 PDF(存在软件内)
|
||||
INQ_DIR = os.path.join(DATA_DIR, "inquiry") # 项目询价附件 pdf/zip/rar(2026-10-06)
|
||||
UPLOAD_DIR = os.path.join(DATA_DIR, "source_files") # 工作台原文件(原数据页数据源同这份;2026-10-14 拍平不分桶)
|
||||
ATTACH_DIR = os.path.join(DATA_DIR, "price_attachments") # 市场价信息价附件(价格批次原件;2026-10-14 拍平不分桶)
|
||||
INQ_DIR = os.path.join(DATA_DIR, "inquiry_attachments") # 询价附件(采购公告/厂商回复等;2026-10-14 拍平不分桶)
|
||||
# 注:会话库的路径由 session_job 自己管(sess.connect()),这里不再重复定义常量
|
||||
for _d in (UPLOAD_DIR, ATTACH_DIR, INQ_DIR):
|
||||
os.makedirs(_d, exist_ok=True)
|
||||
@@ -462,9 +462,9 @@ def _allowed_source_path(p):
|
||||
|
||||
|
||||
def _save_to(dirpath, sha, safe_name, raw):
|
||||
sub = os.path.join(dirpath, sha[:2])
|
||||
os.makedirs(sub, exist_ok=True)
|
||||
fp = os.path.join(sub, sha + "_" + safe_name)
|
||||
# 2026-10-14 主人定:三个附件目录全部拍平,不再分 sha 前两位的桶子目录
|
||||
os.makedirs(dirpath, exist_ok=True)
|
||||
fp = os.path.join(dirpath, sha + "_" + safe_name)
|
||||
if not os.path.exists(fp):
|
||||
with open(fp, "wb") as f:
|
||||
f.write(raw)
|
||||
@@ -1297,10 +1297,10 @@ SESSION_KEEP_DAYS = 0
|
||||
|
||||
|
||||
def _src_path(src_name):
|
||||
"""会话的源文件还在不在软件里?src_name 形如「<sha256>_<原文件名>」,落在 uploads/<sha[:2]>/ 下"""
|
||||
"""会话的源文件还在不在软件里?src_name 形如「<sha256>_<原文件名>」,落在 source_files/ 下(2026-10-14 拍平)"""
|
||||
if not src_name:
|
||||
return None
|
||||
p = os.path.join(UPLOAD_DIR, src_name[:2], src_name)
|
||||
p = os.path.join(UPLOAD_DIR, src_name)
|
||||
if os.path.exists(p):
|
||||
return p
|
||||
# 老会话可能是别的名字:按文件名尾部兜底找一遍
|
||||
@@ -1338,7 +1338,7 @@ def _purge_upload_files(names):
|
||||
nm = os.path.basename((raw or "").strip())
|
||||
if not nm or "_" not in nm:
|
||||
continue # 老格式(无 sha 前缀)没有对应的 uploads 文件
|
||||
fp = os.path.join(UPLOAD_DIR, nm[:2], nm)
|
||||
fp = os.path.join(UPLOAD_DIR, nm) # 2026-10-14 拍平:不再有桶层
|
||||
if not os.path.isfile(fp):
|
||||
continue
|
||||
try:
|
||||
@@ -1368,18 +1368,73 @@ def _purge_orphan_uploads():
|
||||
|
||||
2026-10-11 起因:删除路径只清被删会话当时引用的文件,模拟数据工具等渠道留下的
|
||||
孤儿会一直积压(实测 1799 个)。引用口径与 _purge_upload_files 完全一致
|
||||
(sessions.src_name 分段 + batches.x_src_hash),复用它逐个判+删,返回删除个数。"""
|
||||
(sessions.src_name 分段 + batches.x_src_hash),复用它逐个判+删,返回删除个数。
|
||||
2026-10-14 拍平:不再分桶,直接扫目录,只认 <sha256>_<原名> 规矩文件。"""
|
||||
if not os.path.isdir(UPLOAD_DIR):
|
||||
return 0
|
||||
gone = 0
|
||||
for sub in os.listdir(UPLOAD_DIR):
|
||||
d = os.path.join(UPLOAD_DIR, sub)
|
||||
if not os.path.isdir(d) or len(sub) != 2:
|
||||
for f in os.listdir(UPLOAD_DIR):
|
||||
if not os.path.isfile(os.path.join(UPLOAD_DIR, f)) or "_" not in f:
|
||||
continue
|
||||
for f in os.listdir(d):
|
||||
# 只认 <sha256>_<原名> 且桶号=sha 前两位的规矩文件,别的一律不碰
|
||||
if f.startswith(sub) and "_" in f and os.path.isfile(os.path.join(d, f)):
|
||||
gone += _purge_upload_files([f])
|
||||
head = f.split("_", 1)[0].lower()
|
||||
if len(head) < 8 or any(ch not in "0123456789abcdef" for ch in head):
|
||||
continue # 只认 <sha256>_<原名> 规矩文件,别的一律不碰
|
||||
gone += _purge_upload_files([f])
|
||||
return gone
|
||||
|
||||
|
||||
def _purge_orphan_inq():
|
||||
"""启动自愈:询价附件目录里 inquiry_files 表不再引用的物理文件回收(幂等,2026-10-14 拍平同款)。"""
|
||||
if not os.path.isdir(INQ_DIR):
|
||||
return 0
|
||||
c = conn()
|
||||
try:
|
||||
ref = {r[0] for r in c.execute(
|
||||
"SELECT DISTINCT sha256 FROM inquiry_files WHERE sha256 IS NOT NULL")}
|
||||
finally:
|
||||
c.close()
|
||||
gone = 0
|
||||
for f in os.listdir(INQ_DIR):
|
||||
fp = os.path.join(INQ_DIR, f)
|
||||
if not os.path.isfile(fp) or "_" not in f:
|
||||
continue
|
||||
head = f.split("_", 1)[0].lower()
|
||||
if len(head) < 8 or any(ch not in "0123456789abcdef" for ch in head) or head in ref:
|
||||
continue
|
||||
try:
|
||||
os.remove(fp)
|
||||
gone += 1
|
||||
except OSError:
|
||||
pass
|
||||
return gone
|
||||
|
||||
|
||||
def _purge_orphan_attach():
|
||||
"""启动自愈:价格附件目录里未被 attachments 表引用的物理文件回收(幂等,2026-10-14 拍平同款)。
|
||||
|
||||
之前 _sweep_orphan_attach 只挂在删除流程上;现在启动也对账一遍,stored_path 里旧桶层
|
||||
路径不影响——按文件名 sha 前缀对账,不依赖路径。"""
|
||||
if not os.path.isdir(ATTACH_DIR):
|
||||
return 0
|
||||
c = conn()
|
||||
try:
|
||||
ref = {r[0] for r in c.execute(
|
||||
"SELECT DISTINCT sha256 FROM attachments WHERE sha256 IS NOT NULL")}
|
||||
finally:
|
||||
c.close()
|
||||
gone = 0
|
||||
for f in os.listdir(ATTACH_DIR):
|
||||
fp = os.path.join(ATTACH_DIR, f)
|
||||
if not os.path.isfile(fp) or "_" not in f:
|
||||
continue
|
||||
head = f.split("_", 1)[0].lower()
|
||||
if len(head) < 8 or any(ch not in "0123456789abcdef" for ch in head) or head in ref:
|
||||
continue
|
||||
try:
|
||||
os.remove(fp)
|
||||
gone += 1
|
||||
except OSError:
|
||||
pass
|
||||
return gone
|
||||
|
||||
|
||||
@@ -3023,10 +3078,15 @@ def api_import_price(con, body):
|
||||
return {"ok": False, "msg": "导入失败:%s" % e}
|
||||
fname = os.path.splitext(os.path.basename(path))[0]
|
||||
fp1, fp2 = price.guess_period(fname)
|
||||
# 2026-10-14 主人定:整份校验——空块/空记录/无目录数据一律整份拒绝,不再静默跳过
|
||||
if not blocks:
|
||||
return {"ok": False, "msg": "与模板数据结构不一致,请按模板数据结构录入"}
|
||||
for b in blocks:
|
||||
recs = price_xlsx.to_records([b])
|
||||
if not recs:
|
||||
continue
|
||||
return {"ok": False, "msg": "与模板数据结构不一致,请按模板数据结构录入"}
|
||||
if any(not (r.get("Class") or "").strip() for r in recs):
|
||||
return {"ok": False, "msg": "与模板数据结构不一致,请按模板数据结构录入"}
|
||||
s1 = b.get("period_start_m") if b.get("period_start_m") is not None else (fp1 if fp1 is not None else p1)
|
||||
s2 = b.get("period_end_m") if b.get("period_end_m") is not None else (fp2 if fp2 is not None else p2)
|
||||
if s1 is None:
|
||||
@@ -4529,6 +4589,12 @@ def run():
|
||||
_g = _purge_orphan_uploads() # 2026-10-11:uploads 零引用源文件副本对账回收(幂等)
|
||||
if _g:
|
||||
print("[启动自愈] 回收零引用源文件副本 %d 个" % _g)
|
||||
_gi = _purge_orphan_inq() # 2026-10-14:询价附件零引用对账回收(拍平后同款自愈)
|
||||
if _gi:
|
||||
print("[启动自愈] 回收零引用询价附件 %d 个" % _gi)
|
||||
_ga = _purge_orphan_attach() # 2026-10-14:价格附件零引用对账回收(启动也扫,不只挂在删除流程)
|
||||
if _ga:
|
||||
print("[启动自愈] 回收零引用价格附件 %d 个" % _ga)
|
||||
# 2026-10-11:启动「数据过期」打印已取消(主人定)——打开即自动升级(决策 34)后「需重导」口径不再成立;
|
||||
# stale_batches() 保留:/api/admin/summary 的 stale 字段仍返回,供后续一键批量升级(方案 B)复用。
|
||||
srv = ThreadingHTTPServer(("127.0.0.1", PORT), Handler)
|
||||
|
||||
Reference in New Issue
Block a user