#!/usr/bin/env python3 """ Automatically fix field name references in views to match the new core.Patient model. This script updates old field names to FHIR-aligned names. """ import os import re from pathlib import Path # Field name mappings FIELD_MAPPINGS = { # Patient field name changes r'\.first_name\b': '.given_name', r'\.last_name\b': '.family_name', r'\.date_of_birth\b': '.birth_date', r'\.gender\b': '.administrative_gender', # In filter/query contexts r'first_name__': 'given_name__', r'last_name__': 'family_name__', r'date_of_birth__': 'birth_date__', r'gender__': 'administrative_gender__', # In field lists r"'first_name'": "'given_name'", r'"first_name"': '"given_name"', r"'last_name'": "'family_name'", r'"last_name"': '"family_name"', r"'date_of_birth'": "'birth_date'", r'"date_of_birth"': '"birth_date"', r"'gender'": "'administrative_gender'", r'"gender"': '"administrative_gender"', } # Model name changes MODEL_MAPPINGS = { r'from core\.models import AuditLogEntry': 'from core.models import AuditEvent', r'AuditLogEntry\.objects': 'AuditEvent.objects', r'AuditLogEntry\(': 'AuditEvent(', # Import changes for PatientProfile r'from patients\.models import PatientProfile': 'from core.models import Patient', } def fix_file(file_path): """Fix field references in a single file.""" try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() original_content = content changes_made = [] # Apply field mappings for old_pattern, new_value in FIELD_MAPPINGS.items(): if re.search(old_pattern, content): content = re.sub(old_pattern, new_value, content) changes_made.append(f"{old_pattern} → {new_value}") # Apply model mappings for old_pattern, new_value in MODEL_MAPPINGS.items(): if re.search(old_pattern, content): content = re.sub(old_pattern, new_value, content) changes_made.append(f"{old_pattern} → {new_value}") # Only write if changes were made if content != original_content: with open(file_path, 'w', encoding='utf-8') as f: f.write(content) return True, changes_made return False, [] except Exception as e: print(f"Error processing {file_path}: {e}") return False, [] def fix_app(app_path, app_name): """Fix all Python files in an app.""" files_fixed = 0 total_changes = 0 print(f"\nšŸ“ Processing {app_name}...") # Process views.py views_file = app_path / 'views.py' if views_file.exists(): fixed, changes = fix_file(views_file) if fixed: files_fixed += 1 total_changes += len(changes) print(f" āœ“ Fixed {len(changes)} references in views.py") # Process models.py models_file = app_path / 'models.py' if models_file.exists(): fixed, changes = fix_file(models_file) if fixed: files_fixed += 1 total_changes += len(changes) print(f" āœ“ Fixed {len(changes)} references in models.py") # Process forms.py forms_file = app_path / 'forms.py' if forms_file.exists(): fixed, changes = fix_file(forms_file) if fixed: files_fixed += 1 total_changes += len(changes) print(f" āœ“ Fixed {len(changes)} references in forms.py") # Process admin.py admin_file = app_path / 'admin.py' if admin_file.exists(): fixed, changes = fix_file(admin_file) if fixed: files_fixed += 1 total_changes += len(changes) print(f" āœ“ Fixed {len(changes)} references in admin.py") if files_fixed == 0: print(f" ā„¹ļø No changes needed") return files_fixed, total_changes def main(): """Main function to fix all apps.""" project_root = Path(__file__).parent.parent apps = [ 'core', 'accounts', 'patients', 'emr', 'appointments', 'inpatients', 'pharmacy', 'laboratory', 'radiology', 'operating_theatre', 'billing', 'inventory', 'hr', 'analytics', 'communications', 'integration', 'quality', 'facility_management', 'blood_bank', 'insurance_approvals' ] print("=" * 80) print("Fixing Field References - Phase 4 Model Changes") print("=" * 80) total_files = 0 total_changes = 0 for app_name in apps: app_path = project_root / app_name if app_path.exists(): files, changes = fix_app(app_path, app_name) total_files += files total_changes += changes print("\n" + "=" * 80) print("Summary") print("=" * 80) print(f"āœ“ Fixed {total_files} files") print(f"āœ“ Made {total_changes} changes") print("\nAll field references have been updated to match the new core.Patient model!") print("=" * 80) if __name__ == '__main__': main()