feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
backend/scripts/rebuild_order_profile_matching_signatures.py
|
||||
|
||||
用途:为已发布/草稿订单文档模板批量重建版面签名与交叉验证安全报告(不自动发布)。
|
||||
依赖:Flask create_app、order_document_profile_validation_service、Profile ORM。
|
||||
逻辑:按模板独立处理;dry-run 只打印计划;失败不影响其他模板;不输出正文/URL。
|
||||
|
||||
用法:
|
||||
cd backend
|
||||
python scripts/rebuild_order_profile_matching_signatures.py --dry-run
|
||||
python scripts/rebuild_order_profile_matching_signatures.py --profile-code ZT_PO
|
||||
python scripts/rebuild_order_profile_matching_signatures.py --include-published
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import secrets
|
||||
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、模板过滤、是否包含已发布版本。"""
|
||||
parser = argparse.ArgumentParser(description='Rebuild order profile layout signatures')
|
||||
parser.add_argument('--dry-run', action='store_true', help='Only print planned work')
|
||||
parser.add_argument('--profile-code', action='append', default=[], help='Limit to profile_code')
|
||||
parser.add_argument(
|
||||
'--include-published',
|
||||
action='store_true',
|
||||
help='Also attempt clone-to-draft rebuild for published profiles (still no auto-publish)',
|
||||
)
|
||||
parser.add_argument('--actor-id', type=int, default=None, help='Optional actor id for audits')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""按模板重建签名;成功返回 0,任一失败返回 1。"""
|
||||
_prepare_env()
|
||||
args = _parse_args()
|
||||
|
||||
from app import create_app
|
||||
from app.extensions import db
|
||||
from app.models import ShippingOrderDocumentProfile
|
||||
from app.services.shipping_notice import order_document_profile_validation_service as validation_service
|
||||
|
||||
app = create_app()
|
||||
failures = 0
|
||||
with app.app_context():
|
||||
query = ShippingOrderDocumentProfile.query
|
||||
if args.profile_code:
|
||||
query = query.filter(ShippingOrderDocumentProfile.profile_code.in_(args.profile_code))
|
||||
statuses = ['draft']
|
||||
if args.include_published:
|
||||
statuses.append('published')
|
||||
rows = query.filter(ShippingOrderDocumentProfile.status.in_(statuses)).order_by(
|
||||
ShippingOrderDocumentProfile.profile_code.asc(),
|
||||
ShippingOrderDocumentProfile.version.desc(),
|
||||
).all()
|
||||
|
||||
print(f'planned_profiles={len(rows)} dry_run={args.dry_run}')
|
||||
for profile in rows:
|
||||
label = f'{profile.profile_code}@v{profile.version}#{profile.id} status={profile.status}'
|
||||
if profile.status != 'draft':
|
||||
print(f'SKIP {label} reason=not_draft_use_admin_clone')
|
||||
continue
|
||||
if args.dry_run:
|
||||
print(f'PLAN {label}')
|
||||
continue
|
||||
operation_id = secrets.token_hex(16)
|
||||
try:
|
||||
# 重建脚本不持有浏览器租约;仅用于运维批处理,需在维护窗口执行。
|
||||
result = validation_service.run_layout_signature(
|
||||
profile.id,
|
||||
data={},
|
||||
actor_id=args.actor_id,
|
||||
browser_session_id='rebuild-script',
|
||||
lease_token=None,
|
||||
operation_id=operation_id,
|
||||
require_lease=False,
|
||||
)
|
||||
print(
|
||||
f'OK {label} run_id={result.get("id")} '
|
||||
f'checksum={result.get("signature_checksum")} status={result.get("status")}'
|
||||
)
|
||||
except Exception as exc:
|
||||
failures += 1
|
||||
code = getattr(exc, 'code', type(exc).__name__)
|
||||
print(f'FAIL {label} code={code}')
|
||||
db.session.rollback()
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user