""" Import ALL historical complaints from all Excel files (2022-2025). Usage: python manage.py import_all_complaints python manage.py import_all_complaints --hospital-code=HH-N python manage.py import_all_complaints --dry-run """ import os from django.core.management.base import BaseCommand, CommandError from django.core.management import call_command COMPLAINT_FILES = [ { "year": 2022, "path": "data/Complaints Report - 2022.xlsx", "sheets": ["AUG 2022 ", "SEP 2022 ", "OCT 2022", "NOV 2022", "DEC 2022"], "importer": "import_historical_complaints", }, { "year": 2023, "path": "data/Complaints Report - 2023.xlsx", "sheets": [ "January 2023 ", "February 2023", "March 2023", "April 2023 ", "May 2023", "June 2023", "July 2023", "August 2023", "September 2023", "October 2023", "November 2023", "December 2023", ], "importer": "import_historical_complaints", }, { "year": 2024, "path": "data/Complaints Report - 2024.xlsx", "sheets": [ "January 2024", "February 2024", "March 2024 ", "April 2024", "May 2024", "June 2024", "July 2024", "August 2024", "September 2024", "October 2024", "November 2024", "December 2024", ], "importer": "import_historical_complaints", }, { "year": 2025, "path": "data/Complaints Report - 2025.xlsx", "sheets": ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"], "importer": "import_2025_complaints_basic", }, ] class Command(BaseCommand): help = "Import all historical complaints from 2022-2025 Excel files" def add_arguments(self, parser): parser.add_argument("--hospital-code", type=str, default="HH-N") parser.add_argument("--dry-run", action="store_true") parser.add_argument("--year", type=int, help="Import only this year") def handle(self, *args, **options): hospital_code = options["hospital_code"] dry_run = options["dry_run"] year_filter = options.get("year") total_success = 0 total_failed = 0 for file_info in COMPLAINT_FILES: if year_filter and file_info["year"] != year_filter: continue path = file_info["path"] if not os.path.exists(path): self.stderr.write(self.style.ERROR(f"File not found: {path}")) continue self.stdout.write(self.style.SUCCESS(f"\n{'='*60}")) self.stdout.write(self.style.SUCCESS(f"YEAR {file_info['year']}: {path}")) self.stdout.write(self.style.SUCCESS(f"{'='*60}")) for sheet in file_info["sheets"]: self.stdout.write(f"\n Sheet: {sheet}") try: call_command( file_info["importer"], path, sheet=sheet, hospital_code=hospital_code, dry_run=dry_run, stdout=self.stdout, stderr=self.stderr, ) except Exception as e: self.stderr.write(self.style.ERROR(f" FAILED: {e}")) total_failed += 1 self.stdout.write(self.style.SUCCESS(f"\n\n{'='*60}")) self.stdout.write(self.style.SUCCESS("ALL IMPORTS COMPLETE")) self.stdout.write(self.style.SUCCESS(f"{'='*60}"))