Accident: add incident_datetime computed field (date HH:MM:SS)

Backend:
- to_dict() now includes incident_datetime: "YYYY-MM-DD HH:MM:SS"
- Composed from r.date (YYYY-MM-DD) + r.time (HH:MM:SS, padded if HH:MM)
- _get_excel_column_order_accident() adds incident_datetime column after time
- Export xlsx rec dict now includes incident_datetime

Verified:
- /api/accident/records: incident_datetime = "2026-07-30 09:40:00"
- column_order includes incident_datetime column
- /api/accident/export/excel: xlsx 明細 sheet now has incident_datetime column
- Time edge case: HH:MM auto-padded to HH:MM:SS

Frontend:
- Accident list page auto-displays new column via column_order
- No frontend changes needed (renders record[h] as string)
This commit is contained in:
IT Dog
2026-07-20 01:38:24 +08:00
parent 0d34b51918
commit 975791b9bd
+22
View File
@@ -2113,6 +2113,7 @@ def _get_excel_column_order_accident() -> list:
"description",
"severity",
"time",
"incident_datetime", # combined "YYYY-MM-DD HH:MM:SS" (computed)
"location",
"patient_or_staff",
"long_description",
@@ -2340,11 +2341,22 @@ async def accident_records(
rows = q.offset((page - 1) * per_page).limit(per_page).all()
def to_dict(r):
# Compose incident_datetime from date + time (display as "YYYY-MM-DD HH:MM:SS")
incident_dt = None
if r.date:
date_part = r.date.isoformat()
time_part = r.time or "00:00:00"
# Ensure time has HH:MM:SS format (may be HH:MM from Excel import)
if len(time_part) == 5: # "HH:MM"
time_part += ":00"
incident_dt = f"{date_part} {time_part}"
return {
"id": r.id,
"submitted_at": r.submitted_at.isoformat() if r.submitted_at else None,
"date": r.date.isoformat() if r.date else None,
"time": r.time,
"incident_datetime": incident_dt, # YYYY-MM-DD HH:MM:SS combined
"location": r.location,
"employee_name": r.submitter_email,
"employee_id": r.injured_person_id,
@@ -2946,11 +2958,21 @@ async def accident_export_excel(
ws.freeze_panes(1, 0)
for r_idx, r in enumerate(rows, start=1):
# Compose incident_datetime from date + time
incident_dt = ""
if r.date:
date_part = r.date.isoformat()
time_part = r.time or "00:00:00"
if len(time_part) == 5:
time_part += ":00"
incident_dt = f"{date_part} {time_part}"
rec = {
"id": r.id,
"submitted_at": r.submitted_at.isoformat() if r.submitted_at else "",
"date": r.date.isoformat() if r.date else "",
"time": r.time or "",
"incident_datetime": incident_dt,
"location": r.location or "",
"submitter_email": r.submitter_email or "",
"injured_person_id": r.injured_person_id or "",