Ali2206 commited on
Commit
531bc46
·
verified ·
1 Parent(s): 6bf4e8a

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +58 -41
api/routes.py CHANGED
@@ -608,48 +608,64 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
608
  if not patient:
609
  raise HTTPException(status_code=404, detail="Patient not found")
610
 
611
- # Format rows safely, ensuring proper column counts and hyphenation
612
- note_rows = " \\\\\n".join(
613
- "{} & {} & {}".format(
614
- escape_latex_special_chars(hyphenate_long_strings(format_timestamp(n.get("date", "") or ""))),
615
- escape_latex_special_chars(hyphenate_long_strings(n.get("type", "") or "")),
616
- escape_latex_special_chars(hyphenate_long_strings(n.get("text", "") or ""))
 
 
 
 
617
  )
618
- for n in patient.get("notes", [])
619
- ) or "\\multicolumn{3}{l}{No notes available}\n\\hline"
620
-
621
- condition_rows = " \\\\\n".join(
622
- "{} & {} & {} & {} & {}".format(
623
- escape_latex_special_chars(hyphenate_long_strings(c.get("id", "") or "")),
624
- escape_latex_special_chars(hyphenate_long_strings(c.get("code", "") or "")),
625
- escape_latex_special_chars(hyphenate_long_strings(c.get("status", "") or "")),
626
- escape_latex_special_chars(hyphenate_long_strings(format_timestamp(c.get("onset_date", "") or ""))),
627
- escape_latex_special_chars(hyphenate_long_strings(c.get("verification_status", "") or ""))
 
 
 
 
628
  )
629
- for c in patient.get("conditions", [])
630
- ) or "\\multicolumn{5}{l}{No conditions available}\n\\hline"
631
-
632
- medication_rows = " \\\\\n".join(
633
- "{} & {} & {} & {} & {}".format(
634
- escape_latex_special_chars(hyphenate_long_strings(m.get("id", "") or "")),
635
- escape_latex_special_chars(hyphenate_long_strings(m.get("name", "") or "")),
636
- escape_latex_special_chars(hyphenate_long_strings(m.get("status", "") or "")),
637
- escape_latex_special_chars(hyphenate_long_strings(format_timestamp(m.get("prescribed_date", "") or ""))),
638
- escape_latex_special_chars(hyphenate_long_strings(m.get("dosage", "") or ""))
 
 
 
 
639
  )
640
- for m in patient.get("medications", [])
641
- ) or "\\multicolumn{5}{l}{No medications available}\n\\hline"
642
-
643
- encounter_rows = " \\\\\n".join(
644
- "{} & {} & {} & {} & {}".format(
645
- escape_latex_special_chars(hyphenate_long_strings(e.get("id", "") or "")),
646
- escape_latex_special_chars(hyphenate_long_strings(e.get("type", "") or "")),
647
- escape_latex_special_chars(hyphenate_long_strings(e.get("status", "") or "")),
648
- escape_latex_special_chars(hyphenate_long_strings(format_timestamp(e.get("period", {}).get("start", "") or ""))),
649
- escape_latex_special_chars(hyphenate_long_strings(e.get("service_provider", "") or ""))
 
 
 
 
650
  )
651
- for e in patient.get("encounters", [])
652
- ) or "\\multicolumn{5}{l}{No encounters available}\n\\hline"
653
 
654
  # Use Template for safe insertion
655
  latex_template = Template(r"""
@@ -660,7 +676,8 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
660
  \geometry{margin=1in}
661
  \usepackage{booktabs,longtable,fancyhdr}
662
  \usepackage{array}
663
- \usepackage{microtype} % Added for better text wrapping and hyphenation
 
664
  \setlength{\headheight}{14.5pt} % Fix fancyhdr warning
665
  \pagestyle{fancy}
666
  \fancyhf{}
@@ -731,8 +748,8 @@ $encounters
731
  \end{document}
732
  """)
733
 
734
- # Set the generated_on date to 05:08 PM CET, May 16, 2025
735
- generated_on = datetime.strptime("2025-05-16 17:08:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p")
736
 
737
  latex_filled = latex_template.substitute(
738
  generated_on=generated_on,
 
608
  if not patient:
609
  raise HTTPException(status_code=404, detail="Patient not found")
610
 
611
+ # Format rows safely, ensuring proper column counts
612
+ notes = patient.get("notes", [])
613
+ if notes:
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 notes
621
  )
622
+ else:
623
+ note_rows = "\\multicolumn{3}{l}{No notes available}"
624
+
625
+ conditions = patient.get("conditions", [])
626
+ if conditions:
627
+ condition_rows = " \\\\\n".join(
628
+ "{} & {} & {} & {} & {}".format(
629
+ escape_latex_special_chars(hyphenate_long_strings(c.get("id", "") or "")),
630
+ escape_latex_special_chars(hyphenate_long_strings(c.get("code", "") or "")),
631
+ escape_latex_special_chars(hyphenate_long_strings(c.get("status", "") or "")),
632
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(c.get("onset_date", "") or ""))),
633
+ escape_latex_special_chars(hyphenate_long_strings(c.get("verification_status", "") or ""))
634
+ )
635
+ for c in conditions
636
  )
637
+ else:
638
+ condition_rows = "\\multicolumn{5}{l}{No conditions available}"
639
+
640
+ medications = patient.get("medications", [])
641
+ if medications:
642
+ medication_rows = " \\\\\n".join(
643
+ "{} & {} & {} & {} & {}".format(
644
+ escape_latex_special_chars(hyphenate_long_strings(m.get("id", "") or "")),
645
+ escape_latex_special_chars(hyphenate_long_strings(m.get("name", "") or "")),
646
+ escape_latex_special_chars(hyphenate_long_strings(m.get("status", "") or "")),
647
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(m.get("prescribed_date", "") or ""))),
648
+ escape_latex_special_chars(hyphenate_long_strings(m.get("dosage", "") or ""))
649
+ )
650
+ for m in medications
651
  )
652
+ else:
653
+ medication_rows = "\\multicolumn{5}{l}{No medications available}"
654
+
655
+ encounters = patient.get("encounters", [])
656
+ if encounters:
657
+ encounter_rows = " \\\\\n".join(
658
+ "{} & {} & {} & {} & {}".format(
659
+ escape_latex_special_chars(hyphenate_long_strings(e.get("id", "") or "")),
660
+ escape_latex_special_chars(hyphenate_long_strings(e.get("type", "") or "")),
661
+ escape_latex_special_chars(hyphenate_long_strings(e.get("status", "") or "")),
662
+ escape_latex_special_chars(hyphenate_long_strings(format_timestamp(e.get("period", {}).get("start", "") or ""))),
663
+ escape_latex_special_chars(hyphenate_long_strings(e.get("service_provider", "") or ""))
664
+ )
665
+ for e in encounters
666
  )
667
+ else:
668
+ encounter_rows = "\\multicolumn{5}{l}{No encounters available}"
669
 
670
  # Use Template for safe insertion
671
  latex_template = Template(r"""
 
676
  \geometry{margin=1in}
677
  \usepackage{booktabs,longtable,fancyhdr}
678
  \usepackage{array}
679
+ \usepackage{microtype}
680
+ \microtypesetup{expansion=false} % Disable font expansion to avoid errors
681
  \setlength{\headheight}{14.5pt} % Fix fancyhdr warning
682
  \pagestyle{fancy}
683
  \fancyhf{}
 
748
  \end{document}
749
  """)
750
 
751
+ # Set the generated_on date to 05:14 PM CET, May 16, 2025
752
+ generated_on = datetime.strptime("2025-05-16 17:14:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p")
753
 
754
  latex_filled = latex_template.substitute(
755
  generated_on=generated_on,