178 lines
6.5 KiB
Python
178 lines
6.5 KiB
Python
"""
|
|
Diagnostic script to check why domain dropdown shows no data.
|
|
|
|
This script will:
|
|
1. Check if taxonomy data exists in the database
|
|
2. Test the api_load_categories endpoint
|
|
3. Verify the data structure matches what the frontend expects
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
|
|
# Setup Django
|
|
sys.path.insert(0, '/home/ismail/projects/HH')
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
|
django.setup()
|
|
|
|
from apps.complaints.models import ComplaintCategory
|
|
from apps.organizations.models import Hospital
|
|
|
|
print("=" * 80)
|
|
print("DIAGNOSTIC: Domain Dropdown Issue")
|
|
print("=" * 80)
|
|
|
|
# 1. Check total categories in database
|
|
print("\n1. Checking total categories in database...")
|
|
total_categories = ComplaintCategory.objects.count()
|
|
print(f" Total categories: {total_categories}")
|
|
|
|
if total_categories == 0:
|
|
print(" ❌ ERROR: No categories found in database!")
|
|
print(" SOLUTION: Run: python manage.py load_shct_taxonomy")
|
|
sys.exit(1)
|
|
|
|
# 2. Check categories by level
|
|
print("\n2. Checking categories by level...")
|
|
for level_num, level_name in [
|
|
(1, "DOMAIN"),
|
|
(2, "CATEGORY"),
|
|
(3, "SUBCATEGORY"),
|
|
(4, "CLASSIFICATION")
|
|
]:
|
|
count = ComplaintCategory.objects.filter(level=level_num).count()
|
|
print(f" Level {level_num} ({level_name}): {count}")
|
|
|
|
# 3. Check Level 1 (Domain) categories
|
|
print("\n3. Level 1 Domain categories:")
|
|
domains = ComplaintCategory.objects.filter(level=1)
|
|
print(f" Found {domains.count()} domain(s)")
|
|
for domain in domains:
|
|
print(f" - ID: {domain.id}, Name: {domain.name_en} ({domain.name_ar}), Type: {domain.domain_type}, Active: {domain.is_active}")
|
|
|
|
if domains.count() == 0:
|
|
print(" ❌ ERROR: No Level 1 domains found!")
|
|
print(" SOLUTION: Run: python manage.py load_shct_taxonomy")
|
|
sys.exit(1)
|
|
|
|
# 4. Check hospitals
|
|
print("\n4. Checking hospitals...")
|
|
hospitals = Hospital.objects.filter(status="active")
|
|
print(f" Active hospitals: {hospitals.count()}")
|
|
for hospital in hospitals:
|
|
category_count = hospital.complaint_categories.count()
|
|
print(f" - {hospital.name} (ID: {hospital.id}): {category_count} hospital-specific categories")
|
|
|
|
# 5. Test API endpoint query (simulating what the frontend does)
|
|
print("\n5. Testing API endpoint query...")
|
|
if hospitals.count() > 0:
|
|
test_hospital = hospitals.first()
|
|
print(f" Using hospital: {test_hospital.name} (ID: {test_hospital.id})")
|
|
|
|
# This is what api_load_categories does
|
|
from django.db.models import Q
|
|
categories_queryset = (
|
|
ComplaintCategory.objects.filter(
|
|
Q(hospitals__id=test_hospital.id) | Q(hospitals__isnull=True),
|
|
is_active=True
|
|
)
|
|
.distinct()
|
|
.order_by("level", "order", "name_en")
|
|
)
|
|
|
|
print(f" Query returned {categories_queryset.count()} categories")
|
|
|
|
# Check for Level 1
|
|
level_1_categories = categories_queryset.filter(level=1)
|
|
print(f" Level 1 categories in query: {level_1_categories.count()}")
|
|
|
|
if level_1_categories.count() == 0:
|
|
print(" ❌ ERROR: No Level 1 categories returned by query!")
|
|
print(" This explains why the domain dropdown is empty.")
|
|
else:
|
|
print(" ✓ Level 1 categories found for domain dropdown:")
|
|
for cat in level_1_categories:
|
|
print(f" - {cat.name_en}")
|
|
else:
|
|
print(" ⚠️ No active hospitals found")
|
|
print(" Testing with no hospital (system-wide categories)...")
|
|
|
|
# System-wide only
|
|
categories_queryset = ComplaintCategory.objects.filter(
|
|
hospitals__isnull=True,
|
|
is_active=True
|
|
).order_by("level", "order", "name_en")
|
|
|
|
print(f" Query returned {categories_queryset.count()} system-wide categories")
|
|
level_1_categories = categories_queryset.filter(level=1)
|
|
print(f" Level 1 system-wide categories: {level_1_categories.count()}")
|
|
|
|
# 6. Check is_active flag
|
|
print("\n6. Checking is_active flag for domains...")
|
|
inactive_domains = ComplaintCategory.objects.filter(level=1, is_active=False)
|
|
print(f" Inactive Level 1 domains: {inactive_domains.count()}")
|
|
if inactive_domains.count() > 0:
|
|
print(" ⚠️ Warning: Some domains are inactive:")
|
|
for domain in inactive_domains:
|
|
print(f" - {domain.name_en} (inactive)")
|
|
|
|
# 7. Sample API response format
|
|
print("\n7. Sample API response for Level 1 categories...")
|
|
from django.db.models import Q
|
|
|
|
# Get a sample hospital or use system-wide
|
|
if hospitals.exists():
|
|
hospital_id = hospitals.first().id
|
|
categories_queryset = (
|
|
ComplaintCategory.objects.filter(
|
|
Q(hospitals__id=hospital_id) | Q(hospitals__isnull=True),
|
|
is_active=True
|
|
)
|
|
.distinct()
|
|
.order_by("level", "order", "name_en")
|
|
)
|
|
else:
|
|
categories_queryset = ComplaintCategory.objects.filter(
|
|
hospitals__isnull=True,
|
|
is_active=True
|
|
).order_by("level", "order", "name_en")
|
|
|
|
categories = categories_queryset.values(
|
|
"id", "name_en", "name_ar", "code", "parent_id", "level", "domain_type", "description_en", "description_ar"
|
|
)
|
|
|
|
level_1_data = [cat for cat in categories if cat['level'] == 1]
|
|
|
|
print(f" Level 1 categories that would be sent to frontend: {len(level_1_data)}")
|
|
for cat in level_1_data:
|
|
print(f" - ID: {cat['id']}, Name: {cat['name_en']}, Type: {cat['domain_type']}")
|
|
|
|
# Summary
|
|
print("\n" + "=" * 80)
|
|
print("DIAGNOSTIC SUMMARY")
|
|
print("=" * 80)
|
|
|
|
if ComplaintCategory.objects.filter(level=1, is_active=True).count() > 0:
|
|
print("✓ Database contains active Level 1 (Domain) categories")
|
|
|
|
if hospitals.exists():
|
|
if categories_queryset.filter(level=1).count() > 0:
|
|
print("✓ API query should return Level 1 categories for the selected hospital")
|
|
print("\n💡 POSSIBLE ISSUE: Frontend JavaScript might not be calling the API correctly")
|
|
print(" Check browser console for JavaScript errors")
|
|
print(" Check network tab to see if API call is being made")
|
|
else:
|
|
print("❌ API query returns 0 Level 1 categories")
|
|
print("\n💡 ISSUE: Categories exist but query is not returning them")
|
|
print(" POSSIBLE CAUSES:")
|
|
print(" - All domains are hospital-specific but don't match selected hospital")
|
|
print(" - All domains have is_active=False")
|
|
else:
|
|
print("⚠️ No active hospitals in database")
|
|
print(" Cannot test hospital-specific category loading")
|
|
else:
|
|
print("❌ No active Level 1 (Domain) categories in database")
|
|
print("\n💡 SOLUTION: Run: python manage.py load_shct_taxonomy")
|
|
|
|
print("\n" + "=" * 80) |