Fix accident export xlsx: indent + submitter_email mapping
Issues fixed: 1. Indentation error after rename (rec dict outdented one level) 2. rec dict used employee_name/responsible_person keys but column_order used submitter_email/injured_person keys → rec.get(col, \"\") returned \"\" → xlsx cells showed None Fix: - Re-indent whole for r_idx block (ws.set_row, for c_idx) - Use submitter_email/injured_person/injured_person_id as rec keys to match column_order - Tested: xlsx 電子郵件地址 column now shows real email values
This commit is contained in:
+16
-13
@@ -1974,7 +1974,7 @@ async def _import_accident_to_db(db: Session, headers, rows, user_id: int) -> di
|
|||||||
for row_idx, row in enumerate(rows, start=2): # row_idx is the Excel 1-based row number
|
for row_idx, row in enumerate(rows, start=2): # row_idx is the Excel 1-based row number
|
||||||
try:
|
try:
|
||||||
submitted_at = _parse_dt(_cell(row, 0))
|
submitted_at = _parse_dt(_cell(row, 0))
|
||||||
employee_name = _cell(row, 1) or ""
|
submitter_email = _cell(row, 1) or ""
|
||||||
department = _cell(row, 2)
|
department = _cell(row, 2)
|
||||||
description = _cell(row, 3) or ""
|
description = _cell(row, 3) or ""
|
||||||
severity = str(_cell(row, 4) or "")
|
severity = str(_cell(row, 4) or "")
|
||||||
@@ -1992,7 +1992,7 @@ async def _import_accident_to_db(db: Session, headers, rows, user_id: int) -> di
|
|||||||
date_val = submitted_at.date()
|
date_val = submitted_at.date()
|
||||||
|
|
||||||
# Skip rows missing critical identifiers
|
# Skip rows missing critical identifiers
|
||||||
if not date_val or not employee_name:
|
if not date_val or not submitter_email:
|
||||||
errors += 1
|
errors += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -2010,7 +2010,7 @@ async def _import_accident_to_db(db: Session, headers, rows, user_id: int) -> di
|
|||||||
continue
|
continue
|
||||||
# Update fields from Excel
|
# Update fields from Excel
|
||||||
existing.submitted_at = submitted_at
|
existing.submitted_at = submitted_at
|
||||||
existing.employee_name = employee_name
|
existing.submitter_email = submitter_email
|
||||||
existing.department = department
|
existing.department = department
|
||||||
existing.description = description
|
existing.description = description
|
||||||
existing.severity = severity
|
existing.severity = severity
|
||||||
@@ -2027,7 +2027,7 @@ async def _import_accident_to_db(db: Session, headers, rows, user_id: int) -> di
|
|||||||
else:
|
else:
|
||||||
rec = Accident(
|
rec = Accident(
|
||||||
submitted_at=submitted_at,
|
submitted_at=submitted_at,
|
||||||
employee_name=employee_name,
|
submitter_email=submitter_email,
|
||||||
department=department,
|
department=department,
|
||||||
description=description,
|
description=description,
|
||||||
severity=severity,
|
severity=severity,
|
||||||
@@ -2072,7 +2072,7 @@ def _get_excel_headers_accident() -> dict:
|
|||||||
# Hardcoded column index → SQL field mapping (matches _import_accident_to_db)
|
# Hardcoded column index → SQL field mapping (matches _import_accident_to_db)
|
||||||
idx_to_sql = {
|
idx_to_sql = {
|
||||||
0: "submitted_at",
|
0: "submitted_at",
|
||||||
1: "employee_name",
|
1: "submitter_email", # was employee_name, renamed 2026-07-20
|
||||||
2: "department",
|
2: "department",
|
||||||
3: "description",
|
3: "description",
|
||||||
4: "severity",
|
4: "severity",
|
||||||
@@ -2108,7 +2108,7 @@ def _get_excel_column_order_accident() -> list:
|
|||||||
return [
|
return [
|
||||||
"id", # virtual
|
"id", # virtual
|
||||||
"submitted_at",
|
"submitted_at",
|
||||||
"employee_name",
|
"submitter_email", # was employee_name, renamed 2026-07-20
|
||||||
"department",
|
"department",
|
||||||
"description",
|
"description",
|
||||||
"severity",
|
"severity",
|
||||||
@@ -2390,10 +2390,13 @@ async def update_accident_record(
|
|||||||
raise HTTPException(status_code=404, detail="Record not found")
|
raise HTTPException(status_code=404, detail="Record not found")
|
||||||
# Allowed fields to edit (whitelist for safety)
|
# Allowed fields to edit (whitelist for safety)
|
||||||
editable = {
|
editable = {
|
||||||
"employee_name", "department", "description", "severity",
|
"submitter_email", # was employee_name, renamed 2026-07-20
|
||||||
|
"injured_person_id", # was employee_id, renamed 2026-07-20
|
||||||
|
"injured_person", # was responsible_person, renamed 2026-07-19
|
||||||
|
"department", "description", "severity",
|
||||||
"time", "location", "patient_or_staff", "long_description",
|
"time", "location", "patient_or_staff", "long_description",
|
||||||
"incident_photos", "action_taken", "needs_escalation",
|
"incident_photos", "action_taken", "needs_escalation",
|
||||||
"incident_score", "injured_person", # was responsible_person, renamed 2026-07-19", "medical_report",
|
"incident_score", "medical_report",
|
||||||
}
|
}
|
||||||
for k, v in payload.items():
|
for k, v in payload.items():
|
||||||
if k in editable:
|
if k in editable:
|
||||||
@@ -2949,14 +2952,14 @@ async def accident_export_excel(
|
|||||||
"date": r.date.isoformat() if r.date else "",
|
"date": r.date.isoformat() if r.date else "",
|
||||||
"time": r.time or "",
|
"time": r.time or "",
|
||||||
"location": r.location or "",
|
"location": r.location or "",
|
||||||
"employee_name": r.submitter_email or "",
|
"submitter_email": r.submitter_email or "",
|
||||||
"employee_id": r.injured_person_id or "",
|
"injured_person_id": r.injured_person_id or "",
|
||||||
"department": r.department or "",
|
"department": r.department or "",
|
||||||
"description": r.description or "",
|
"description": r.description or "",
|
||||||
"severity": str(r.severity or ""),
|
"severity": str(r.severity or ""),
|
||||||
"medical_report": r.medical_report or "",
|
"medical_report": r.medical_report or "",
|
||||||
"action_taken": r.action_taken or "",
|
"action_taken": r.action_taken or "",
|
||||||
"responsible_person": r.injured_person or "",
|
"injured_person": r.injured_person or "",
|
||||||
"patient_or_staff": r.patient_or_staff or "",
|
"patient_or_staff": r.patient_or_staff or "",
|
||||||
"long_description": r.long_description or "",
|
"long_description": r.long_description or "",
|
||||||
"incident_photos": r.incident_photos or "",
|
"incident_photos": r.incident_photos or "",
|
||||||
@@ -3319,7 +3322,7 @@ async def export_excel(
|
|||||||
overview.write(f"G{nav_start_row + 1}", "→ 查明細", header_fmt_ov)
|
overview.write(f"G{nav_start_row + 1}", "→ 查明細", header_fmt_ov)
|
||||||
for idx, r in enumerate(rows):
|
for idx, r in enumerate(rows):
|
||||||
row_n = nav_start_row + 2 + idx
|
row_n = nav_start_row + 2 + idx
|
||||||
overview.write(row_n, 1, r.submitter_email or "", cell_fmt_ov)
|
overview.write(row_n, 1, r.employee_name or "", cell_fmt_ov)
|
||||||
overview.write(row_n, 2, r.department or "", cell_fmt_ov)
|
overview.write(row_n, 2, r.department or "", cell_fmt_ov)
|
||||||
overview.write(row_n, 3, r.date.isoformat() if r.date else "", cell_fmt_ov)
|
overview.write(row_n, 3, r.date.isoformat() if r.date else "", cell_fmt_ov)
|
||||||
overview.write(row_n, 4, r.status_text or "", cell_fmt_ov)
|
overview.write(row_n, 4, r.status_text or "", cell_fmt_ov)
|
||||||
@@ -3378,7 +3381,7 @@ async def export_excel(
|
|||||||
for r_idx, r in enumerate(rows, start=1):
|
for r_idx, r in enumerate(rows, start=1):
|
||||||
sc = (r.status_code or "").lower()
|
sc = (r.status_code or "").lower()
|
||||||
fmt = status_fmts.get(sc, status_fmts["default"])
|
fmt = status_fmts.get(sc, status_fmts["default"])
|
||||||
ws.write(r_idx, 0, r.submitter_email or "")
|
ws.write(r_idx, 0, r.employee_name or "")
|
||||||
ws.write(r_idx, 1, r.company or "")
|
ws.write(r_idx, 1, r.company or "")
|
||||||
ws.write(r_idx, 2, r.department or "")
|
ws.write(r_idx, 2, r.department or "")
|
||||||
ws.write(r_idx, 3, r.date.isoformat() if r.date else "")
|
ws.write(r_idx, 3, r.date.isoformat() if r.date else "")
|
||||||
|
|||||||
Reference in New Issue
Block a user