feat(shipping-notice): 添加订单文档 Profile 的样本和审计管理功能,支持样本绑定、审计记录和模板预览,增强文档模板的管理和验证能力
This commit is contained in:
1 parent
d62f5107b2
commit
93777fd6df
63 files changed
+9707
-1491
No files matched your search
@@ -8,7 +8,9 @@ backend/app/api/admin/shipping_order_document_profiles.py
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import g, request
|
||||
import io
|
||||
|
||||
from flask import g, request, send_file
|
||||
|
||||
from app.api.admin.blueprint import admin_bp
|
||||
from app.core.decorators import require_admin
|
||||
@@ -26,6 +28,14 @@ def _ok(data, *, status: int = 200):
|
||||
return ok(data, request_id=getattr(g, 'request_id', None), status=status)
|
||||
|
||||
|
||||
def _private_ok(data, *, status: int = 200):
|
||||
"""返回不允许浏览器或代理缓存的受控预览 JSON。"""
|
||||
response, response_status = _ok(data, status=status)
|
||||
response.headers['Cache-Control'] = 'private, no-store, max-age=0'
|
||||
response.headers['Pragma'] = 'no-cache'
|
||||
return response, response_status
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles', methods=['GET'])
|
||||
@require_admin
|
||||
def list_order_document_profiles():
|
||||
@@ -145,6 +155,30 @@ def clone_order_document_profile(profile_id: int):
|
||||
return _ok(service.clone_draft(profile_id, actor_id=_actor_id()), status=201)
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/schema-migration', methods=['POST'])
|
||||
@require_admin
|
||||
def migrate_order_document_profile_schema(profile_id: int):
|
||||
"""从已发布模板创建目标 Schema 版本的字段级迁移草稿。"""
|
||||
data = request.get_json(silent=True) or {}
|
||||
try:
|
||||
target_version_id = int(data.get('target_schema_version_id'))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise service.ValidationError(
|
||||
'target_schema_version_id is invalid', code='SHIPPING.ORDER_DOCUMENT_PROFILE_MIGRATION_INVALID',
|
||||
) from exc
|
||||
resolutions = data.get('resolutions') or {}
|
||||
if not isinstance(resolutions, dict):
|
||||
raise service.ValidationError(
|
||||
'resolutions must be an object', code='SHIPPING.ORDER_DOCUMENT_PROFILE_MIGRATION_INVALID',
|
||||
)
|
||||
return _ok(service.create_schema_migration_draft(
|
||||
profile_id,
|
||||
target_schema_version_id=target_version_id,
|
||||
resolutions=resolutions,
|
||||
actor_id=_actor_id(),
|
||||
), status=201)
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/publish', methods=['POST'])
|
||||
@require_admin
|
||||
def publish_order_document_profile(profile_id: int):
|
||||
@@ -159,7 +193,7 @@ def publish_order_document_profile(profile_id: int):
|
||||
@require_admin
|
||||
def retire_order_document_profile(profile_id: int):
|
||||
"""手工退役当前发布模板。"""
|
||||
return _ok(service.retire(profile_id))
|
||||
return _ok(service.retire(profile_id, actor_id=_actor_id()))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>', methods=['DELETE'])
|
||||
@@ -170,10 +204,96 @@ def delete_order_document_profile(profile_id: int):
|
||||
return '', 204
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/samples', methods=['GET'])
|
||||
@require_admin
|
||||
def list_order_document_profile_bound_samples(profile_id: int):
|
||||
"""列出当前模板绑定的标注和训练样本摘要。"""
|
||||
return _ok(service.list_profile_samples(profile_id))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/samples', methods=['POST'])
|
||||
@require_admin
|
||||
def add_order_document_profile_bound_sample(profile_id: int):
|
||||
"""向草稿绑定受控样本,不接受正文或对象存储地址。"""
|
||||
data = request.get_json(silent=True) or {}
|
||||
return _ok(service.add_profile_sample(
|
||||
profile_id, document_id=data.get('document_id'), table_id=data.get('table_id'),
|
||||
sample_role=data.get('sample_role'), actor_id=_actor_id(),
|
||||
), status=201)
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/samples/<int:sample_id>', methods=['DELETE'])
|
||||
@require_admin
|
||||
def remove_order_document_profile_bound_sample(profile_id: int, sample_id: int):
|
||||
"""解除草稿样本绑定,原始文件按既有样本生命周期单独回收。"""
|
||||
return _ok(service.remove_profile_sample(profile_id, sample_id, actor_id=_actor_id()))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/preview-pages', methods=['POST'])
|
||||
@require_admin
|
||||
def authorize_order_document_profile_preview_pages(profile_id: int):
|
||||
"""签发管理员单用途 PDF 页面预览令牌,不返回对象存储下载地址。"""
|
||||
data = request.get_json(silent=True) or {}
|
||||
return _private_ok(service.list_preview_pages(
|
||||
profile_id, document_id=data.get('document_id'), actor_id=_actor_id(),
|
||||
))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/preview-pages/<int:page_no>', methods=['GET'])
|
||||
@require_admin
|
||||
def get_order_document_profile_preview_page(profile_id: int, page_no: int):
|
||||
"""消费短时授权并流式返回一页低分辨率 PNG,禁止缓存和重放。"""
|
||||
image = service.render_preview_page(
|
||||
token=str(request.args.get('preview_token') or ''), page_no=page_no, actor_id=_actor_id(),
|
||||
)
|
||||
response = send_file(io.BytesIO(image), mimetype='image/png', as_attachment=False, max_age=0)
|
||||
response.headers['Cache-Control'] = 'private, no-store, max-age=0'
|
||||
response.headers['Pragma'] = 'no-cache'
|
||||
response.headers['X-Content-Type-Options'] = 'nosniff'
|
||||
return response
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/preview-pages/<int:page_no>/text-candidates', methods=['GET'])
|
||||
@require_admin
|
||||
def get_order_document_profile_preview_page_text_candidates(profile_id: int, page_no: int):
|
||||
"""读取当前页的紧凑文本候选,供标注工作台局部确认。"""
|
||||
return _private_ok(service.page_text_candidates(
|
||||
profile_id, document_id=request.args.get('document_id'), page_no=page_no, actor_id=_actor_id(),
|
||||
))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/preview-region', methods=['POST'])
|
||||
@require_admin
|
||||
def preview_order_document_profile_region(profile_id: int):
|
||||
"""基于当前模板绑定样本重建局部候选,响应不含 HTML、坐标或存储地址。"""
|
||||
return _private_ok(service.preview_profile_region(
|
||||
profile_id, data=request.get_json(silent=True) or {}, actor_id=_actor_id(),
|
||||
))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/header-suggestions', methods=['POST'])
|
||||
@require_admin
|
||||
def suggest_order_document_profile_header(profile_id: int):
|
||||
"""识别当前或全部已配置表头行,仅返回待确认的 Schema 别名建议。"""
|
||||
return _private_ok(service.suggest_profile_header(
|
||||
profile_id, data=request.get_json(silent=True) or {}, actor_id=_actor_id(),
|
||||
))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/ocr-coordinate-evidence', methods=['POST'])
|
||||
@require_admin
|
||||
def generate_bound_order_document_profile_ocr_coordinate_evidence(profile_id: int):
|
||||
"""显式生成当前模板绑定 PDF 样本的 OCR 坐标证据,不接受跨样本访问。"""
|
||||
data = request.get_json(silent=True) or {}
|
||||
return _private_ok(service.generate_profile_ocr_evidence(
|
||||
profile_id, document_id=data.get('document_id'), table_id=data.get('table_id'), actor_id=_actor_id(),
|
||||
))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profiles/<int:profile_id>/test-suites', methods=['POST'])
|
||||
@require_admin
|
||||
def create_order_document_profile_test_suite(profile_id: int):
|
||||
"""为发布模板创建独立盲测集。"""
|
||||
"""为草稿或发布模板创建独立盲测集。"""
|
||||
from app.services.shipping_notice import document_profile_quality_service as quality
|
||||
return _ok(quality.create_suite(profile_id=profile_id, actor_id=_actor_id()), status=201)
|
||||
|
||||
@@ -190,6 +310,14 @@ def add_order_document_profile_test_sample(suite_id: int):
|
||||
), status=201)
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profile-test-suites/<int:suite_id>/samples/<int:sample_id>', methods=['DELETE'])
|
||||
@require_admin
|
||||
def remove_order_document_profile_test_sample(suite_id: int, sample_id: int):
|
||||
"""删除尚未批准盲测集的单个样本真值,并留下操作审计。"""
|
||||
from app.services.shipping_notice import document_profile_quality_service as quality
|
||||
return _ok(quality.remove_sample(suite_id=suite_id, sample_id=sample_id, actor_id=_actor_id()))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/order-document-profile-test-suites/<int:suite_id>/execute', methods=['POST'])
|
||||
@require_admin
|
||||
def execute_order_document_profile_test_suite(suite_id: int):
|
||||
|
||||
@@ -186,6 +186,13 @@ def get_shipping_order_schema_version_usage(version_id: int):
|
||||
return _ok(service.get_version_usage(version_id, page=page, page_size=page_size))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-order-schema/versions/<int:version_id>/profile-impact', methods=['GET'])
|
||||
@require_admin
|
||||
def get_shipping_order_schema_version_profile_impact(version_id: int):
|
||||
"""只读评估目标 Schema 版本对已发布订单文档模板的兼容性影响。"""
|
||||
return _ok(service.get_profile_impact(version_id))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-order-schema/order-extractions/<int:extraction_id>', methods=['GET'])
|
||||
@require_admin
|
||||
def get_shipping_order_schema_extraction_audit(extraction_id: int):
|
||||
@@ -221,6 +228,7 @@ def publish_shipping_order_schema_version(version_id: int):
|
||||
return _ok(service.publish(
|
||||
version_id, actor_id=_actor_id(), expected_lock_version=data.get('expected_lock_version'),
|
||||
request_id=getattr(g, 'request_id', None),
|
||||
confirm_profile_impact=bool(data.get('confirm_profile_impact', False)),
|
||||
))
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user