110 lines
4.1 KiB
Python
110 lines
4.1 KiB
Python
"""
|
|
Diagnostic script to identify why survey links show as invalid.
|
|
"""
|
|
import os
|
|
import django
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
django.setup()
|
|
|
|
from apps.surveys.models import SurveyInstance, SurveyStatus
|
|
from django.utils import timezone
|
|
|
|
print("="*80)
|
|
print("SURVEY LINK ISSUE DIAGNOSTIC")
|
|
print("="*80)
|
|
|
|
# 1. Check SurveyStatus choices
|
|
print("\n1. Available SurveyStatus Choices:")
|
|
for choice in SurveyStatus.choices:
|
|
print(f" - {choice[1]} (value: '{choice[0]}')")
|
|
|
|
# 2. Check recent surveys
|
|
print("\n2. Recent Survey Instances (last 5):")
|
|
recent_surveys = SurveyInstance.objects.order_by('-created_at')[:5]
|
|
for survey in recent_surveys:
|
|
print(f"\n Survey ID: {survey.id}")
|
|
print(f" Status: '{survey.status}'")
|
|
print(f" Token: {survey.access_token[:20]}...")
|
|
print(f" Token Expires: {survey.token_expires_at}")
|
|
print(f" URL: {survey.get_survey_url()}")
|
|
print(f" Template: {survey.survey_template.name}")
|
|
print(f" Patient: {survey.patient.get_full_name()}")
|
|
|
|
# 3. Check if status matches expected values
|
|
print("\n3. Status Validation Check:")
|
|
print("\n survey_form view expects status in: ['pending', 'sent', 'viewed', 'in_progress']")
|
|
print("\n Actual survey statuses in database:")
|
|
for survey in recent_surveys:
|
|
expected_statuses = ['pending', 'sent', 'viewed', 'in_progress']
|
|
is_valid = survey.status in expected_statuses
|
|
status_match = survey.status in [c[0] for c in SurveyStatus.choices]
|
|
|
|
print(f"\n Survey {survey.id}:")
|
|
print(f" - Status value: '{survey.status}'")
|
|
print(f" - In expected list: {is_valid}")
|
|
print(f" - Matches SurveyStatus choices: {status_match}")
|
|
|
|
if not status_match:
|
|
print(f" ⚠️ WARNING: Status '{survey.status}' not in SurveyStatus choices!")
|
|
|
|
if not is_valid:
|
|
print(f" ⚠️ WARNING: Status '{survey.status}' will cause link to be invalid!")
|
|
|
|
# 4. Check token expiration
|
|
print("\n4. Token Expiration Check:")
|
|
for survey in recent_surveys:
|
|
is_expired = survey.token_expires_at and survey.token_expires_at < timezone.now()
|
|
print(f"\n Survey {survey.id}:")
|
|
print(f" - Expires at: {survey.token_expires_at}")
|
|
print(f" - Is expired: {is_expired}")
|
|
if is_expired:
|
|
print(f" ⚠️ WARNING: Token is expired!")
|
|
|
|
# 5. Test access simulation
|
|
print("\n5. Simulating survey_form view check:")
|
|
print("\n Looking for surveys that WOULD match the view's criteria:")
|
|
print(" Criteria: status in ['pending', 'sent', 'viewed', 'in_progress']")
|
|
print(" token_expires_at > now")
|
|
print(" access_token = <token>")
|
|
|
|
matching_surveys = SurveyInstance.objects.filter(
|
|
status__in=['pending', 'sent', 'viewed', 'in_progress'],
|
|
token_expires_at__gt=timezone.now()
|
|
)
|
|
|
|
print(f"\n Found {matching_surveys.count()} surveys that would be accessible")
|
|
|
|
# 6. Show all unique status values in database
|
|
print("\n6. All unique status values in database:")
|
|
unique_statuses = SurveyInstance.objects.values_list('status', flat=True).distinct()
|
|
for status in unique_statuses:
|
|
count = SurveyInstance.objects.filter(status=status).count()
|
|
print(f" - '{status}': {count} surveys")
|
|
|
|
# 7. Recommendation
|
|
print("\n" + "="*80)
|
|
print("DIAGNOSIS SUMMARY")
|
|
print("="*80)
|
|
|
|
has_invalid_status = any(
|
|
s.status not in ['pending', 'sent', 'viewed', 'in_progress']
|
|
for s in recent_surveys
|
|
)
|
|
|
|
if has_invalid_status:
|
|
print("\n❌ PROBLEM IDENTIFIED:")
|
|
print(" Survey status values don't match what the view expects.")
|
|
print("\n Root Cause:")
|
|
print(" - Surveys are created with status='PENDING' (uppercase)")
|
|
print(" - View expects status='pending' (lowercase)")
|
|
print(" - SurveyStatus may not have a PENDING choice")
|
|
print("\n Solution:")
|
|
print(" 1. Add PENDING status to SurveyStatus choices")
|
|
print(" 2. Update survey_form view to use correct status values")
|
|
print(" 3. Update HIS adapter to use correct status")
|
|
else:
|
|
print("\n✅ Status values appear correct")
|
|
print(" Issue may be elsewhere (token expiration, wrong token, etc.)")
|
|
|
|
print("\n" + "="*80) |