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

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +25 -33
api/routes.py CHANGED
@@ -608,23 +608,25 @@ 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
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 "")),
@@ -633,13 +635,14 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
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 "")),
@@ -648,13 +651,14 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
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 "")),
@@ -663,9 +667,9 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
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"""
@@ -707,49 +711,37 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
707
 
708
  \section*{Clinical Notes}
709
  \begin{longtable}{>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{6.5cm}}
710
- \toprule
711
  \textbf{Date} & \textbf{Type} & \textbf{Text} \\
712
- \midrule
713
  \endhead
714
  $notes
715
- \bottomrule
716
  \end{longtable}
717
 
718
  \section*{Conditions}
719
  \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{3cm}}
720
- \toprule
721
  \textbf{ID} & \textbf{Code} & \textbf{Status} & \textbf{Onset} & \textbf{Verification} \\
722
- \midrule
723
  \endhead
724
  $conditions
725
- \bottomrule
726
  \end{longtable}
727
 
728
  \section*{Medications}
729
- \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{2cm}}
730
- \toprule
731
  \textbf{ID} & \textbf{Name} & \textbf{Status} & \textbf{Date} & \textbf{Dosage} \\
732
- \midrule
733
  \endhead
734
  $medications
735
- \bottomrule
736
  \end{longtable}
737
 
738
  \section*{Encounters}
739
- \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4.5cm}>{\raggedright\arraybackslash}p{3cm}}
740
- \toprule
741
  \textbf{ID} & \textbf{Type} & \textbf{Status} & \textbf{Start} & \textbf{Provider} \\
742
- \midrule
743
  \endhead
744
  $encounters
745
- \bottomrule
746
  \end{longtable}
747
 
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,
@@ -767,10 +759,10 @@ $encounters
767
  ]))),
768
  marital_status=escape_latex_special_chars(patient.get("marital_status", "") or ""),
769
  language=escape_latex_special_chars(patient.get("language", "") or ""),
770
- notes=note_rows,
771
- conditions=condition_rows,
772
- medications=medication_rows,
773
- encounters=encounter_rows
774
  )
775
 
776
  # Compile LaTeX in a temporary directory
 
608
  if not patient:
609
  raise HTTPException(status_code=404, detail="Patient not found")
610
 
611
+ # Prepare table content
612
  notes = patient.get("notes", [])
613
+ notes_content = ""
614
  if notes:
615
+ notes_content = "\\toprule\n" + " \\\\\n".join(
616
  "{} & {} & {}".format(
617
  escape_latex_special_chars(hyphenate_long_strings(format_timestamp(n.get("date", "") or ""))),
618
  escape_latex_special_chars(hyphenate_long_strings(n.get("type", "") or "")),
619
  escape_latex_special_chars(hyphenate_long_strings(n.get("text", "") or ""))
620
  )
621
  for n in notes
622
+ ) + "\n\\bottomrule"
623
  else:
624
+ notes_content = "\\multicolumn{3}{l}{No notes available}"
625
 
626
  conditions = patient.get("conditions", [])
627
+ conditions_content = ""
628
  if conditions:
629
+ conditions_content = "\\toprule\n" + " \\\\\n".join(
630
  "{} & {} & {} & {} & {}".format(
631
  escape_latex_special_chars(hyphenate_long_strings(c.get("id", "") or "")),
632
  escape_latex_special_chars(hyphenate_long_strings(c.get("code", "") or "")),
 
635
  escape_latex_special_chars(hyphenate_long_strings(c.get("verification_status", "") or ""))
636
  )
637
  for c in conditions
638
+ ) + "\n\\bottomrule"
639
  else:
640
+ conditions_content = "\\multicolumn{5}{l}{No conditions available}"
641
 
642
  medications = patient.get("medications", [])
643
+ medications_content = ""
644
  if medications:
645
+ medications_content = "\\toprule\n" + " \\\\\n".join(
646
  "{} & {} & {} & {} & {}".format(
647
  escape_latex_special_chars(hyphenate_long_strings(m.get("id", "") or "")),
648
  escape_latex_special_chars(hyphenate_long_strings(m.get("name", "") or "")),
 
651
  escape_latex_special_chars(hyphenate_long_strings(m.get("dosage", "") or ""))
652
  )
653
  for m in medications
654
+ ) + "\n\\bottomrule"
655
  else:
656
+ medications_content = "\\multicolumn{5}{l}{No medications available}"
657
 
658
  encounters = patient.get("encounters", [])
659
+ encounters_content = ""
660
  if encounters:
661
+ encounters_content = "\\toprule\n" + " \\\\\n".join(
662
  "{} & {} & {} & {} & {}".format(
663
  escape_latex_special_chars(hyphenate_long_strings(e.get("id", "") or "")),
664
  escape_latex_special_chars(hyphenate_long_strings(e.get("type", "") or "")),
 
667
  escape_latex_special_chars(hyphenate_long_strings(e.get("service_provider", "") or ""))
668
  )
669
  for e in encounters
670
+ ) + "\n\\bottomrule"
671
  else:
672
+ encounters_content = "\\multicolumn{5}{l}{No encounters available}"
673
 
674
  # Use Template for safe insertion
675
  latex_template = Template(r"""
 
711
 
712
  \section*{Clinical Notes}
713
  \begin{longtable}{>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{6.5cm}}
 
714
  \textbf{Date} & \textbf{Type} & \textbf{Text} \\
 
715
  \endhead
716
  $notes
 
717
  \end{longtable}
718
 
719
  \section*{Conditions}
720
  \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{3cm}}
 
721
  \textbf{ID} & \textbf{Code} & \textbf{Status} & \textbf{Onset} & \textbf{Verification} \\
 
722
  \endhead
723
  $conditions
 
724
  \end{longtable}
725
 
726
  \section*{Medications}
727
+ \begin{longtable}{>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{4cm}>{\raggedright\arraybackslash}p{2cm}>{\raggedright\arraybackslash}p{3.5cm}>{\raggedright\arraybackslash}p{3cm}}
 
728
  \textbf{ID} & \textbf{Name} & \textbf{Status} & \textbf{Date} & \textbf{Dosage} \\
 
729
  \endhead
730
  $medications
 
731
  \end{longtable}
732
 
733
  \section*{Encounters}
734
+ \begin{longtable}{>{\raggedright\arraybackslash}p{2.5cm}>{\raggedright\arraybackslash}p{4.5cm}>{\raggedright\arraybackslash}p{2.5cm}>{\raggedright\arraybackslash}p{4.5cm}>{\raggedright\arraybackslash}p{3.5cm}}
 
735
  \textbf{ID} & \textbf{Type} & \textbf{Status} & \textbf{Start} & \textbf{Provider} \\
 
736
  \endhead
737
  $encounters
 
738
  \end{longtable}
739
 
740
  \end{document}
741
  """)
742
 
743
+ # Set the generated_on date to 05:26 PM CET, May 16, 2025
744
+ generated_on = datetime.strptime("2025-05-16 17:26:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p")
745
 
746
  latex_filled = latex_template.substitute(
747
  generated_on=generated_on,
 
759
  ]))),
760
  marital_status=escape_latex_special_chars(patient.get("marital_status", "") or ""),
761
  language=escape_latex_special_chars(patient.get("language", "") or ""),
762
+ notes=notes_content,
763
+ conditions=conditions_content,
764
+ medications=medications_content,
765
+ encounters=encounters_content
766
  )
767
 
768
  # Compile LaTeX in a temporary directory