86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
"""
|
|
Script to fix survey status values from uppercase to lowercase.
|
|
|
|
This script updates all surveys with uppercase status values (e.g., 'SENT')
|
|
to use the correct lowercase SurveyStatus values (e.g., 'sent').
|
|
"""
|
|
import os
|
|
import django
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
django.setup()
|
|
|
|
from apps.surveys.models import SurveyInstance, SurveyStatus
|
|
|
|
print("="*80)
|
|
print("FIXING SURVEY STATUS VALUES")
|
|
print("="*80)
|
|
|
|
# Get all surveys with uppercase status values
|
|
uppercase_surveys = SurveyInstance.objects.exclude(status__in=[c[0] for c in SurveyStatus.choices])
|
|
|
|
print(f"\nFound {uppercase_surveys.count()} surveys with incorrect status values")
|
|
|
|
# Group by status value
|
|
status_counts = {}
|
|
for survey in uppercase_surveys:
|
|
status_counts[survey.status] = status_counts.get(survey.status, 0) + 1
|
|
|
|
print("\nStatus values to fix:")
|
|
for status, count in sorted(status_counts.items()):
|
|
print(f" - '{status}': {count} surveys")
|
|
|
|
# Fix surveys
|
|
fixed_count = 0
|
|
for survey in uppercase_surveys:
|
|
old_status = survey.status
|
|
|
|
# Map uppercase values to correct SurveyStatus values
|
|
if old_status.upper() == 'SENT':
|
|
new_status = SurveyStatus.SENT
|
|
elif old_status.upper() == 'PENDING':
|
|
# Use SENT as PENDING doesn't exist in SurveyStatus
|
|
new_status = SurveyStatus.SENT
|
|
elif old_status.upper() == 'DELIVERED':
|
|
# Use SENT as DELIVERED doesn't exist in SurveyStatus
|
|
new_status = SurveyStatus.SENT
|
|
elif old_status.upper() == 'VIEWED':
|
|
new_status = SurveyStatus.VIEWED
|
|
elif old_status.upper() == 'IN_PROGRESS':
|
|
new_status = SurveyStatus.IN_PROGRESS
|
|
elif old_status.upper() == 'COMPLETED':
|
|
new_status = SurveyStatus.COMPLETED
|
|
elif old_status.upper() == 'ABANDONED':
|
|
new_status = SurveyStatus.ABANDONED
|
|
elif old_status.upper() == 'EXPIRED':
|
|
new_status = SurveyStatus.EXPIRED
|
|
elif old_status.upper() == 'CANCELLED':
|
|
new_status = SurveyStatus.CANCELLED
|
|
else:
|
|
# Default to SENT for unknown status values
|
|
new_status = SurveyStatus.SENT
|
|
|
|
survey.status = new_status
|
|
survey.save(update_fields=['status'])
|
|
fixed_count += 1
|
|
|
|
print(f" Survey {survey.id}: '{old_status}' → '{new_status}'")
|
|
|
|
print(f"\n" + "="*80)
|
|
print(f"FIXED {fixed_count} SURVEYS")
|
|
print("="*80)
|
|
|
|
# Verify all surveys now have correct status values
|
|
incorrect_surveys = SurveyInstance.objects.exclude(status__in=[c[0] for c in SurveyStatus.choices])
|
|
if incorrect_surveys.exists():
|
|
print(f"\n⚠️ WARNING: Still have {incorrect_surveys.count()} surveys with incorrect status")
|
|
else:
|
|
print(f"\n✅ All surveys now have correct status values!")
|
|
|
|
# Show distribution of correct status values
|
|
print("\nCurrent status distribution:")
|
|
for choice in SurveyStatus.choices:
|
|
count = SurveyInstance.objects.filter(status=choice[0]).count()
|
|
print(f" - {choice[1]} ({choice[0]}): {count} surveys")
|
|
|
|
print("\n" + "="*80) |