Ali2206 commited on
Commit
81dee31
·
verified ·
1 Parent(s): 9eb9a6a

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +25 -14
api/routes.py CHANGED
@@ -139,7 +139,6 @@ def hyphenate_long_strings(text: str, max_length: int = 10) -> str:
139
  """Insert LaTeX hyphenation points into long strings to prevent overfull hboxes."""
140
  if not isinstance(text, str) or len(text) <= max_length:
141
  return text
142
- # Insert a discretionary hyphen (\-) every max_length characters
143
  result = ""
144
  for i, char in enumerate(text):
145
  result += char
@@ -147,6 +146,18 @@ def hyphenate_long_strings(text: str, max_length: int = 10) -> str:
147
  result += "\\-"
148
  return result
149
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  async def process_synthea_patient(bundle: dict, file_path: str) -> Optional[dict]:
151
  logger.debug(f"Processing patient from file: {file_path}")
152
  patient_data = {}
@@ -602,45 +613,45 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
602
  # Format rows safely, ensuring proper column counts and hyphenation
603
  note_rows = " \\\\\n".join(
604
  "{} & {} & {}".format(
605
- escape_latex_special_chars(hyphenate_long_strings(n.get("date", "") or "")),
606
  escape_latex_special_chars(hyphenate_long_strings(n.get("type", "") or "")),
607
  escape_latex_special_chars(hyphenate_long_strings(n.get("text", "") or ""))
608
  )
609
  for n in patient.get("notes", [])
610
- ) or "No notes available \\\\"
611
 
612
  condition_rows = " \\\\\n".join(
613
  "{} & {} & {} & {} & {}".format(
614
  escape_latex_special_chars(hyphenate_long_strings(c.get("id", "") or "")),
615
  escape_latex_special_chars(hyphenate_long_strings(c.get("code", "") or "")),
616
  escape_latex_special_chars(hyphenate_long_strings(c.get("status", "") or "")),
617
- escape_latex_special_chars(hyphenate_long_strings(c.get("onset_date", "") or "")),
618
  escape_latex_special_chars(hyphenate_long_strings(c.get("verification_status", "") or ""))
619
  )
620
  for c in patient.get("conditions", [])
621
- ) or "No conditions available \\\\"
622
 
623
  medication_rows = " \\\\\n".join(
624
  "{} & {} & {} & {} & {}".format(
625
  escape_latex_special_chars(hyphenate_long_strings(m.get("id", "") or "")),
626
  escape_latex_special_chars(hyphenate_long_strings(m.get("name", "") or "")),
627
  escape_latex_special_chars(hyphenate_long_strings(m.get("status", "") or "")),
628
- escape_latex_special_chars(hyphenate_long_strings(m.get("prescribed_date", "") or "")),
629
  escape_latex_special_chars(hyphenate_long_strings(m.get("dosage", "") or ""))
630
  )
631
  for m in patient.get("medications", [])
632
- ) or "No medications available \\\\"
633
 
634
  encounter_rows = " \\\\\n".join(
635
  "{} & {} & {} & {} & {}".format(
636
  escape_latex_special_chars(hyphenate_long_strings(e.get("id", "") or "")),
637
  escape_latex_special_chars(hyphenate_long_strings(e.get("type", "") or "")),
638
  escape_latex_special_chars(hyphenate_long_strings(e.get("status", "") or "")),
639
- escape_latex_special_chars(hyphenate_long_strings(e.get("period", {}).get("start", "") or "")),
640
  escape_latex_special_chars(hyphenate_long_strings(e.get("service_provider", "") or ""))
641
  )
642
  for e in patient.get("encounters", [])
643
- ) or "No encounters available \\\\"
644
 
645
  # Use Template for safe insertion
646
  latex_template = Template(r"""
@@ -689,7 +700,7 @@ $notes
689
  \end{longtable}
690
 
691
  \section*{Conditions}
692
- \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}}
693
  \toprule
694
  \textbf{ID} & \textbf{Code} & \textbf{Status} & \textbf{Onset} & \textbf{Verification} \\
695
  \midrule
@@ -699,7 +710,7 @@ $conditions
699
  \end{longtable}
700
 
701
  \section*{Medications}
702
- \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{2cm}}
703
  \toprule
704
  \textbf{ID} & \textbf{Name} & \textbf{Status} & \textbf{Date} & \textbf{Dosage} \\
705
  \midrule
@@ -709,7 +720,7 @@ $medications
709
  \end{longtable}
710
 
711
  \section*{Encounters}
712
- \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{3cm}}
713
  \toprule
714
  \textbf{ID} & \textbf{Type} & \textbf{Status} & \textbf{Start} & \textbf{Provider} \\
715
  \midrule
@@ -721,8 +732,8 @@ $encounters
721
  \end{document}
722
  """)
723
 
724
- # Set the generated_on date to 03:52 PM CET, May 16, 2025
725
- generated_on = datetime.strptime("2025-05-16 15:52:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p")
726
 
727
  latex_filled = latex_template.substitute(
728
  generated_on=generated_on,
 
139
  """Insert LaTeX hyphenation points into long strings to prevent overfull hboxes."""
140
  if not isinstance(text, str) or len(text) <= max_length:
141
  return text
 
142
  result = ""
143
  for i, char in enumerate(text):
144
  result += char
 
146
  result += "\\-"
147
  return result
148
 
149
+ def format_timestamp(timestamp: str) -> str:
150
+ """Shorten a timestamp to YYYY-MM-DD format."""
151
+ if not isinstance(timestamp, str):
152
+ return ""
153
+ try:
154
+ # Parse the timestamp and extract just the date part
155
+ dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")
156
+ return dt.strftime("%Y-%m-%d")
157
+ except ValueError:
158
+ # If parsing fails, return the original string (safely escaped later)
159
+ return timestamp
160
+
161
  async def process_synthea_patient(bundle: dict, file_path: str) -> Optional[dict]:
162
  logger.debug(f"Processing patient from file: {file_path}")
163
  patient_data = {}
 
613
  # Format rows safely, ensuring proper column counts and hyphenation
614
  note_rows = " \\\\\n".join(
615
  "{} & {} & {}".format(
616
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(n.get("date", "") or ""))),
617
  escape_latex_special_chars(hyphenate_long_strings(n.get("type", "") or "")),
618
  escape_latex_special_chars(hyphenate_long_strings(n.get("text", "") or ""))
619
  )
620
  for n in patient.get("notes", [])
621
+ ) or "\\multicolumn{3}{l}{No notes available} \\\\"
622
 
623
  condition_rows = " \\\\\n".join(
624
  "{} & {} & {} & {} & {}".format(
625
  escape_latex_special_chars(hyphenate_long_strings(c.get("id", "") or "")),
626
  escape_latex_special_chars(hyphenate_long_strings(c.get("code", "") or "")),
627
  escape_latex_special_chars(hyphenate_long_strings(c.get("status", "") or "")),
628
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(c.get("onset_date", "") or ""))),
629
  escape_latex_special_chars(hyphenate_long_strings(c.get("verification_status", "") or ""))
630
  )
631
  for c in patient.get("conditions", [])
632
+ ) or "\\multicolumn{5}{l}{No conditions available} \\\\"
633
 
634
  medication_rows = " \\\\\n".join(
635
  "{} & {} & {} & {} & {}".format(
636
  escape_latex_special_chars(hyphenate_long_strings(m.get("id", "") or "")),
637
  escape_latex_special_chars(hyphenate_long_strings(m.get("name", "") or "")),
638
  escape_latex_special_chars(hyphenate_long_strings(m.get("status", "") or "")),
639
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(m.get("prescribed_date", "") or ""))),
640
  escape_latex_special_chars(hyphenate_long_strings(m.get("dosage", "") or ""))
641
  )
642
  for m in patient.get("medications", [])
643
+ ) or "\\multicolumn{5}{l}{No medications available} \\\\"
644
 
645
  encounter_rows = " \\\\\n".join(
646
  "{} & {} & {} & {} & {}".format(
647
  escape_latex_special_chars(hyphenate_long_strings(e.get("id", "") or "")),
648
  escape_latex_special_chars(hyphenate_long_strings(e.get("type", "") or "")),
649
  escape_latex_special_chars(hyphenate_long_strings(e.get("status", "") or "")),
650
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(e.get("period", {}).get("start", "") or ""))),
651
  escape_latex_special_chars(hyphenate_long_strings(e.get("service_provider", "") or ""))
652
  )
653
  for e in patient.get("encounters", [])
654
+ ) or "\\multicolumn{5}{l}{No encounters available} \\\\"
655
 
656
  # Use Template for safe insertion
657
  latex_template = Template(r"""
 
700
  \end{longtable}
701
 
702
  \section*{Conditions}
703
+ \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{3cm}}
704
  \toprule
705
  \textbf{ID} & \textbf{Code} & \textbf{Status} & \textbf{Onset} & \textbf{Verification} \\
706
  \midrule
 
710
  \end{longtable}
711
 
712
  \section*{Medications}
713
+ \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{2cm}}
714
  \toprule
715
  \textbf{ID} & \textbf{Name} & \textbf{Status} & \textbf{Date} & \textbf{Dosage} \\
716
  \midrule
 
720
  \end{longtable}
721
 
722
  \section*{Encounters}
723
+ \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{3cm}}
724
  \toprule
725
  \textbf{ID} & \textbf{Type} & \textbf{Status} & \textbf{Start} & \textbf{Provider} \\
726
  \midrule
 
732
  \end{document}
733
  """)
734
 
735
+ # Set the generated_on date to 03:59 PM CET, May 16, 2025
736
+ generated_on = datetime.strptime("2025-05-16 15:59:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p")
737
 
738
  latex_filled = latex_template.substitute(
739
  generated_on=generated_on,