Swift-Query / utils /db /db.py
Darshan Kumar R
Deploy to Hugging Face Spaces (Clean)
ed0b977
Raw History Blame Contribute Delete
1.96 kB
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