#!/usr/bin/env python """ Test script to verify admin evaluation charts fix. Tests that the page loads without t.put errors. """ import os import sys import django # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings') sys.path.insert(0, '/home/ismail/projects/HH') django.setup() from django.test import Client, TestCase from django.contrib.auth import get_user_model from apps.organizations.models import Hospital, Department from apps.complaints.models import Complaint from apps.complaints.models import Inquiry User = get_user_model() def test_admin_evaluation_page(): """Test that admin evaluation page loads correctly""" print("=" * 60) print("Testing Admin Evaluation Charts Fix") print("=" * 60) # Create test user user, created = User.objects.get_or_create( email='test_admin@test.com', defaults={ 'first_name': 'Test', 'last_name': 'Admin', 'is_staff': True, 'is_hospital_admin': True } ) # Ensure hospital hospital, _ = Hospital.objects.get_or_create( name_en='Test Hospital', defaults={'status': 'active'} ) if created: user.hospital = hospital user.save() # Create department department, _ = Department.objects.get_or_create( name_en='Test Department', defaults={'status': 'active', 'hospital': hospital} ) if created: user.department = department user.save() # Create test complaints assigned to user complaint_count = Complaint.objects.filter(assigned_to=user).count() if complaint_count == 0: print("\n✓ Creating test complaints...") for i in range(5): Complaint.objects.create( title=f'Test Complaint {i+1}', description=f'Test complaint {i+1} description', severity='medium', source_id=1, hospital=hospital, department=department, assigned_to=user, status='resolved' ) print(f"✓ Created 5 test complaints") else: print(f"✓ Found {complaint_count} existing complaints") # Create test inquiries assigned to user inquiry_count = Inquiry.objects.filter(assigned_to=user).count() if inquiry_count == 0: print("\n✓ Creating test inquiries...") for i in range(3): Inquiry.objects.create( subject=f'Test Inquiry {i+1}', message=f'Test inquiry {i+1} message', hospital=hospital, department=department, assigned_to=user, status='resolved', responded_at=user.date_joined ) print(f"✓ Created 3 test inquiries") else: print(f"✓ Found {inquiry_count} existing inquiries") # Test page load client = Client() client.force_login(user) print("\n" + "=" * 60) print("Testing Admin Evaluation Page Load") print("=" * 60) try: response = client.get('/dashboard/admin-evaluation/') if response.status_code == 200: print("✓ Page loaded successfully (200 OK)") # Check for template content content = response.content.decode('utf-8') checks = [ ('Performance data present', 'performanceData' in content), ('Chart containers present', 'complaintSourceChart' in content), ('Complaints tab present', 'complaints-tab' in content), ('Inquiries tab present', 'inquiries-tab' in content), ('RenderChart function present', 'renderChart' in content), ('Chart instances tracking present', 'chartInstances' in content), ('Visibility check present', 'offsetParent' in content), ('Tab event listener present', 'shown.bs.tab' in content), ] print("\nTemplate Checks:") all_passed = True for check_name, passed in checks: status = "✓" if passed else "✗" print(f" {status} {check_name}") if not passed: all_passed = False if all_passed: print("\n" + "=" * 60) print("SUCCESS: All template checks passed!") print("=" * 60) print("\nThe fix includes:") print(" • Chart instances tracking to prevent duplicates") print(" • Visibility checks before rendering") print(" • Proper cleanup of existing chart instances") print(" • Tab-based rendering for inquiry charts") print(" • Comprehensive error handling and logging") return True else: print("\n" + "=" * 60) print("WARNING: Some checks failed") print("=" * 60) return False else: print(f"✗ Page load failed with status code: {response.status_code}") return False except Exception as e: print(f"✗ Error loading page: {str(e)}") import traceback traceback.print_exc() return False if __name__ == '__main__': success = test_admin_evaluation_page() sys.exit(0 if success else 1)