From 975791b9bd197846a4fb83b2dc17fe824cf4519c Mon Sep 17 00:00:00 2001 From: IT Dog Date: Mon, 20 Jul 2026 01:38:24 +0800 Subject: [PATCH] Accident: add incident_datetime computed field (date HH:MM:SS) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- backend/main.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/main.py b/backend/main.py index 63db3b9..d74bc06 100644 --- a/backend/main.py +++ b/backend/main.py @@ -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 "",