Spaces:
Runtime error
Runtime error
File size: 1,962 Bytes
ed0b977 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from flask_pymongo import PyMongo
mongo = PyMongo()
def init_db(app):
"""
Initialize the MongoDB connection with the Flask app.
Args:
app (Flask): The Flask application instance.
"""
mongo.init_app(app)
def get_all_patients():
"""
Retrieve all patients and generate a textual summary for each.
Returns:
list: A list of strings, each summarizing a patient's data.
"""
try:
patients_collection = mongo.db.patients
patients = list(patients_collection.find())
all_patient_text = []
for p in patients:
summary = f"Patient ID: {p.get('_id')}\n"
summary += f"Name: {p.get('name')}\n"
summary += f"Age: {p.get('age')}\n"
summary += f"Gender: {p.get('gender')}\n"
if p.get('symptoms'):
summary += f"Symptoms: {p.get('symptoms')}\n"
if p.get('diagnosis'):
summary += f"Diagnosis: {p.get('diagnosis')}\n"
if p.get('treatment'):
summary += f"Treatment: {p.get('treatment')}\n"
if p.get('medical_history'):
summary += f"Medical History: {p.get('medical_history')}\n"
all_patient_text.append(summary)
return all_patient_text
except Exception as e:
print(f"Error fetching patients: {e}")
return []
def get_next_id(collection_name):
"""
Generate the next sequential integer ID for a given collection.
This is used to maintain backward compatibility with legacy checks requiring integer IDs.
Args:
collection_name (str): The name of the MongoDB collection.
Returns:
int: The next available integer ID.
"""
try:
last_item = mongo.db[collection_name].find_one(sort=[("id", -1)])
if last_item and 'id' in last_item:
return int(last_item['id']) + 1
return 1
except Exception:
return 1
|