feat: parse docx blocks during upload

This commit is contained in:
zwt13703
2026-07-01 20:29:31 +08:00
parent 95c507536a
commit 389b9c0c92
5 changed files with 147 additions and 54 deletions
+24
View File
@@ -6,6 +6,7 @@ from uuid import uuid4
from fastapi import UploadFile
from sqlalchemy.orm import Session
from ..models.template import Template
from ..models.template_block import TemplateBlock
from .storage import upload_file
@@ -52,3 +53,26 @@ def create_template_record(db: Session, name: str, file_path: str, type: str = "
db.commit()
db.refresh(tmpl)
return tmpl
def save_template_blocks(db: Session, template_id: int, blocks: list[dict]) -> list[TemplateBlock]:
"""批量保存解析出的模板区域。"""
records = [
TemplateBlock(
template_id=template_id,
block_id=block["block_id"],
parent_block_id=block.get("parent_block_id"),
block_type=block["type"],
block_name=block.get("text", "")[:200] or None,
text_preview=block.get("text", "")[:500] or None,
level=block.get("level", 0),
sort_order=block.get("sort_order", index),
table_rows=block.get("rows"),
table_cols=block.get("cols"),
)
for index, block in enumerate(blocks, start=1)
]
db.add_all(records)
db.commit()
return records