Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- 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
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
)
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
)
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
)
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
)
|
| 651 |
-
|
| 652 |
-
|
| 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}
|
|
|
|
| 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:
|
| 735 |
-
generated_on = datetime.strptime("2025-05-16 17:
|
| 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,
|