feat(shipping-notice): 落地 XFA 统一视觉拆分确认门禁与处理投影收敛
闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d2b3d74def
commit
84a881b554
195 files changed
+20269
-3463
No files matched your search
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
backend/scripts/migrate_order_profiles_to_unified_bindings.py
|
||||
|
||||
用途:将订单文档模板 Dry Run / Execute 迁移到统一 bindings[]。
|
||||
依赖:Flask create_app、order_profile_unified_migration。
|
||||
逻辑:默认 Dry Run;--execute 才写 Draft Revision;可用 --profile-id 限定单个模板。
|
||||
|
||||
用法:
|
||||
cd backend
|
||||
python scripts/migrate_order_profiles_to_unified_bindings.py --dry-run
|
||||
python scripts/migrate_order_profiles_to_unified_bindings.py --profile-id 12 --dry-run
|
||||
python scripts/migrate_order_profiles_to_unified_bindings.py --profile-id 12 --execute
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
_BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
_REPO_ROOT = _BACKEND_ROOT.parent
|
||||
if str(_BACKEND_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(_BACKEND_ROOT))
|
||||
|
||||
|
||||
def _prepare_env() -> None:
|
||||
"""导入 app 前固定 ENV_NAME=dev,并允许 .env.local 覆盖 DATABASE_URL。"""
|
||||
os.environ.setdefault('ENV_NAME', 'dev')
|
||||
try:
|
||||
from dotenv import dotenv_values
|
||||
except ImportError:
|
||||
return
|
||||
local_file = _REPO_ROOT / '.env.local'
|
||||
if not local_file.exists():
|
||||
return
|
||||
values = {k: v for k, v in dotenv_values(local_file).items() if v is not None}
|
||||
if values.get('DATABASE_URL'):
|
||||
os.environ['DATABASE_URL'] = str(values['DATABASE_URL'])
|
||||
|
||||
|
||||
def _parse_args() -> argparse.Namespace:
|
||||
"""解析 --dry-run / --execute / --profile-id。"""
|
||||
parser = argparse.ArgumentParser(description='Migrate order profiles to unified bindings')
|
||||
mode = parser.add_mutually_exclusive_group()
|
||||
mode.add_argument('--dry-run', action='store_true', help='Only convert and print report (default)')
|
||||
mode.add_argument('--execute', action='store_true', help='Write Draft revision with bindings')
|
||||
parser.add_argument('--profile-id', type=int, action='append', default=[], help='Limit to profile id')
|
||||
parser.add_argument('--actor-id', type=int, default=None, help='Optional actor id for audits')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""批量 Dry Run 或 Execute;任一失败返回 1。"""
|
||||
_prepare_env()
|
||||
args = _parse_args()
|
||||
execute = bool(args.execute)
|
||||
dry_run = not execute
|
||||
|
||||
from app import create_app
|
||||
from app.models import ShippingOrderDocumentProfile
|
||||
from app.services.shipping_notice import order_profile_unified_migration as migration
|
||||
|
||||
app = create_app()
|
||||
failures = 0
|
||||
with app.app_context():
|
||||
query = ShippingOrderDocumentProfile.query
|
||||
if args.profile_id:
|
||||
query = query.filter(ShippingOrderDocumentProfile.id.in_(args.profile_id))
|
||||
rows = query.order_by(
|
||||
ShippingOrderDocumentProfile.profile_code.asc(),
|
||||
ShippingOrderDocumentProfile.version.desc(),
|
||||
).all()
|
||||
# 同族只处理最新 published 或 draft,避免重复
|
||||
seen_codes: set[str] = set()
|
||||
targets: list[ShippingOrderDocumentProfile] = []
|
||||
for row in rows:
|
||||
key = str(row.profile_code)
|
||||
if key in seen_codes:
|
||||
continue
|
||||
if row.status not in {'draft', 'published'}:
|
||||
continue
|
||||
seen_codes.add(key)
|
||||
targets.append(row)
|
||||
|
||||
print(json.dumps({
|
||||
'mode': 'execute' if execute else 'dry_run',
|
||||
'target_count': len(targets),
|
||||
}, ensure_ascii=False))
|
||||
|
||||
for profile in targets:
|
||||
try:
|
||||
if dry_run:
|
||||
result = migration.dry_run_unified_migration(
|
||||
int(profile.id),
|
||||
actor_id=args.actor_id,
|
||||
write_audit=True,
|
||||
)
|
||||
else:
|
||||
result = migration.execute_unified_migration(
|
||||
int(profile.id),
|
||||
actor_id=args.actor_id,
|
||||
)
|
||||
print(json.dumps(result, ensure_ascii=False, default=str))
|
||||
except Exception as exc:
|
||||
failures += 1
|
||||
print(json.dumps({
|
||||
'profile_id': int(profile.id),
|
||||
'error': str(exc),
|
||||
}, ensure_ascii=False), file=sys.stderr)
|
||||
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user