Template mapping and detect issue fixed
This commit is contained in:
@@ -83,6 +83,63 @@ def create_template(
|
||||
}
|
||||
|
||||
|
||||
@router.put(
|
||||
"/{template_id}",
|
||||
response_model=dict,
|
||||
summary="Update Template",
|
||||
description="Update an existing template name and its fields.",
|
||||
)
|
||||
def update_template(
|
||||
template_id: uuid.UUID,
|
||||
payload: TemplateCreateRequest,
|
||||
current_user: CurrentUser,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
"""Update a template."""
|
||||
template_repo = TemplateRepository(db)
|
||||
template = template_repo.get_by_id(template_id)
|
||||
if not template:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Template '{template_id}' not found",
|
||||
)
|
||||
|
||||
if template.name != payload.template_name:
|
||||
template.name = payload.template_name
|
||||
|
||||
from app.models.template import DocumentCell
|
||||
db.query(DocumentCell).filter(DocumentCell.format_id == template_id).delete()
|
||||
|
||||
saved_fields = []
|
||||
cell_repo = DocumentCellRepository(db)
|
||||
for index, field in enumerate(payload.fields):
|
||||
cell = cell_repo.create_cell(
|
||||
format_id=template.id,
|
||||
page_number=1,
|
||||
row_no=0,
|
||||
column_no=0,
|
||||
field_name=field.field_label,
|
||||
data_type=field.field_type,
|
||||
x=0.0,
|
||||
y=0.0,
|
||||
width=0.0,
|
||||
height=0.0,
|
||||
is_dynamic=True
|
||||
)
|
||||
saved_fields.append({
|
||||
"field_label": cell.field_name,
|
||||
"field_type": cell.data_type
|
||||
})
|
||||
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"pk_template_id": str(template.id),
|
||||
"template_name": template.name,
|
||||
"fields": saved_fields
|
||||
}
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{template_id}/mappings/save",
|
||||
response_model=SuccessResponse,
|
||||
@@ -109,6 +166,15 @@ def save_mappings(
|
||||
table_repo = TableFormatRepository(db)
|
||||
table_col_repo = TableColumnRepository(db)
|
||||
|
||||
# Delete existing field mappings and table formats for this template
|
||||
from app.models.template import DocumentRegion, TableFormat
|
||||
db.query(DocumentRegion).filter(
|
||||
DocumentRegion.format_id == template_id,
|
||||
DocumentRegion.region_type == "field_mapping"
|
||||
).delete()
|
||||
db.query(TableFormat).filter(TableFormat.format_id == template_id).delete()
|
||||
db.flush()
|
||||
|
||||
cells = cell_repo.get_template_cells(template_id)
|
||||
cell_map = {c.field_name: c for c in cells}
|
||||
table_format = None
|
||||
|
||||
Reference in New Issue
Block a user