65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import os
|
|
import django
|
|
|
|
# Set up Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hospital_management.settings')
|
|
django.setup()
|
|
|
|
from patients.models import PatientProfile
|
|
|
|
def remove_first_three_chars_from_mrn():
|
|
patient_id = None
|
|
mrn_value = None
|
|
try:
|
|
updated_count = 0
|
|
updated_patients = []
|
|
|
|
patients = PatientProfile.objects.all()
|
|
|
|
# Process each patient
|
|
for patient in patients:
|
|
original_mrn = patient.mrn
|
|
|
|
# Check if MRN has at least 3 characters
|
|
if len(original_mrn) > 3:
|
|
# Remove first 3 characters
|
|
new_mrn = original_mrn[3:]
|
|
|
|
# Update the patient's MRN
|
|
patient.mrn = new_mrn
|
|
patient.save()
|
|
|
|
updated_count += 1
|
|
updated_patients.append({
|
|
'patient_id': str(patient.patient_id),
|
|
'patient_name': patient.get_full_name(),
|
|
'original_mrn': original_mrn,
|
|
'new_mrn': new_mrn
|
|
})
|
|
else:
|
|
# Skip patients with MRN length <= 3
|
|
continue
|
|
|
|
return {
|
|
'success': True,
|
|
'message': f'Successfully updated {updated_count} patient(s)',
|
|
'updated_count': updated_count,
|
|
'updated_patients': updated_patients
|
|
}
|
|
|
|
except PatientProfile.DoesNotExist:
|
|
return {
|
|
'success': False,
|
|
'message': 'Patient not found',
|
|
'updated_count': 0,
|
|
'updated_patients': []
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'success': False,
|
|
'message': f'Error updating MRN data: {str(e)}',
|
|
'updated_count': 0,
|
|
'updated_patients': []
|
|
}
|
|
|
|
print(remove_first_three_chars_from_mrn()) |