Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +13 -11
api/routes.py
CHANGED
|
@@ -547,24 +547,24 @@ async def add_note(
|
|
| 547 |
|
| 548 |
@router.get("/ehr/patients/{patient_id}/pdf", response_class=Response)
|
| 549 |
async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get_current_user)):
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
if current_user.get('role') not in ['doctor', 'admin']:
|
| 553 |
-
logger.warning(f"Unauthorized PDF generation attempt by {current_user.get('email')}")
|
| 554 |
-
raise HTTPException(status_code=403, detail="Only clinicians can generate patient PDFs")
|
| 555 |
|
|
|
|
| 556 |
try:
|
|
|
|
|
|
|
|
|
|
| 557 |
# Determine if patient_id is ObjectId or fhir_id
|
| 558 |
try:
|
| 559 |
obj_id = ObjectId(patient_id)
|
| 560 |
query = { "$or": [ { "_id": obj_id }, { "fhir_id": patient_id } ] }
|
| 561 |
-
except InvalidId:
|
| 562 |
query = { "fhir_id": patient_id }
|
| 563 |
|
| 564 |
patient = await patients_collection.find_one(query)
|
| 565 |
|
| 566 |
if not patient:
|
| 567 |
-
logger.warning(f"Patient not found: {patient_id}")
|
| 568 |
raise HTTPException(status_code=404, detail="Patient not found")
|
| 569 |
|
| 570 |
# Format LaTeX content using .format() to avoid f-string escape issues
|
|
@@ -664,9 +664,9 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
|
|
| 664 |
for e in patient.get("encounters", [])
|
| 665 |
])
|
| 666 |
|
| 667 |
-
# Update the generated_on date to reflect the current time:
|
| 668 |
latex_filled = latex_template % {
|
| 669 |
-
"generated_on": datetime.strptime("2025-05-16
|
| 670 |
"fhir_id": patient.get("fhir_id", ""),
|
| 671 |
"full_name": patient.get("full_name", ""),
|
| 672 |
"gender": patient.get("gender", ""),
|
|
@@ -706,12 +706,14 @@ async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get
|
|
| 706 |
)
|
| 707 |
|
| 708 |
except subprocess.CalledProcessError as e:
|
| 709 |
-
logger.error(f"LaTeX compilation failed: {e.stderr.decode('utf-8')}")
|
| 710 |
raise HTTPException(status_code=500, detail="LaTeX compilation failed")
|
| 711 |
|
| 712 |
except Exception as e:
|
| 713 |
-
logger.error(f"PDF generation error: {str(e)}")
|
| 714 |
raise HTTPException(status_code=500, detail=f"Failed to generate PDF: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
@router.post("/signup", status_code=status.HTTP_201_CREATED)
|
| 716 |
async def signup(data: SignupForm):
|
| 717 |
logger.info(f"Signup attempt for email: {data.email}")
|
|
|
|
| 547 |
|
| 548 |
@router.get("/ehr/patients/{patient_id}/pdf", response_class=Response)
|
| 549 |
async def generate_patient_pdf(patient_id: str, current_user: dict = Depends(get_current_user)):
|
| 550 |
+
# Disable logging for this route by setting the logger's level to CRITICAL
|
| 551 |
+
logger.setLevel(logging.CRITICAL)
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
+
# Re-enable logging after the function exits to avoid affecting other routes
|
| 554 |
try:
|
| 555 |
+
if current_user.get('role') not in ['doctor', 'admin']:
|
| 556 |
+
raise HTTPException(status_code=403, detail="Only clinicians can generate patient PDFs")
|
| 557 |
+
|
| 558 |
# Determine if patient_id is ObjectId or fhir_id
|
| 559 |
try:
|
| 560 |
obj_id = ObjectId(patient_id)
|
| 561 |
query = { "$or": [ { "_id": obj_id }, { "fhir_id": patient_id } ] }
|
| 562 |
+
except InvalidId:
|
| 563 |
query = { "fhir_id": patient_id }
|
| 564 |
|
| 565 |
patient = await patients_collection.find_one(query)
|
| 566 |
|
| 567 |
if not patient:
|
|
|
|
| 568 |
raise HTTPException(status_code=404, detail="Patient not found")
|
| 569 |
|
| 570 |
# Format LaTeX content using .format() to avoid f-string escape issues
|
|
|
|
| 664 |
for e in patient.get("encounters", [])
|
| 665 |
])
|
| 666 |
|
| 667 |
+
# Update the generated_on date to reflect the current time: 03:10 PM CET, May 16, 2025
|
| 668 |
latex_filled = latex_template % {
|
| 669 |
+
"generated_on": datetime.strptime("2025-05-16 15:10:00+01:00", "%Y-%m-%d %H:%M:%S%z").strftime("%A, %B %d, %Y at %I:%M %p"),
|
| 670 |
"fhir_id": patient.get("fhir_id", ""),
|
| 671 |
"full_name": patient.get("full_name", ""),
|
| 672 |
"gender": patient.get("gender", ""),
|
|
|
|
| 706 |
)
|
| 707 |
|
| 708 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 709 |
raise HTTPException(status_code=500, detail="LaTeX compilation failed")
|
| 710 |
|
| 711 |
except Exception as e:
|
|
|
|
| 712 |
raise HTTPException(status_code=500, detail=f"Failed to generate PDF: {str(e)}")
|
| 713 |
+
finally:
|
| 714 |
+
# Restore the logger level for other routes
|
| 715 |
+
logger.setLevel(logging.INFO)
|
| 716 |
+
|
| 717 |
@router.post("/signup", status_code=status.HTTP_201_CREATED)
|
| 718 |
async def signup(data: SignupForm):
|
| 719 |
logger.info(f"Signup attempt for email: {data.email}")
|