3.6 KiB
Survey Multiple Access Fix
Problem
Previously, survey links could only be viewed once. When a patient refreshed the page or revisited the survey link, they would see an error message:
"We're sorry, but this survey link is no longer valid or has expired."
This was caused by the survey status being updated to 'viewed' on first access, which was not included in the list of allowed statuses for subsequent accesses.
Root Cause
In apps/surveys/public_views.py, the survey_form view was filtering surveys by status:
survey = SurveyInstance.objects.get(
access_token=token,
status__in=['pending', 'sent', 'in_progress'], # Missing 'viewed'!
token_expires_at__gt=timezone.now()
)
Flow of the bug:
- Patient opens survey link → Status changes from
'sent'to'viewed' - Patient refreshes page or opens link again
- Status is now
'viewed'→ Not in allowed list - SurveyInstance.DoesNotExist → Error page shown
Solution
Added 'viewed' to the list of allowed statuses:
survey = SurveyInstance.objects.get(
access_token=token,
status__in=['pending', 'sent', 'viewed', 'in_progress'],
token_expires_at__gt=timezone.now()
)
Behavior After Fix
Survey Access Rules
✅ Can Access Multiple Times:
- Status:
pending,sent,viewed,in_progress - Token: Not expired (
token_expires_at > now)
❌ Cannot Access:
- Status:
completed,expired,cancelled - Token: Expired (
token_expires_at <= now)
Survey Lifecycle
- Created → Status:
pending, Token: Valid for 2 days - Sent to patient → Status:
sent - First open → Status:
viewed,open_count= 1,opened_at= now - Patient refreshes → Status:
viewed,open_count= 2,last_opened_at= now - Patient answers questions → Status:
in_progress(auto-detected) - Patient refreshes again → Status:
in_progress,open_count= 3 - Patient submits → Status:
completed,completed_at= now - Patient tries to access again → Error: Survey already completed
- After 2 days → Token expires → Error: Link expired
Token Expiry
The survey link is valid for 2 days from creation by default:
token_expires_at = timezone.now() + timedelta(days=2)
After this time, the link shows an error message regardless of survey status.
Files Modified
apps/surveys/public_views.py- Line 31: Added'viewed'to allowed statuses
Testing
A test script has been created to verify the fix:
python test_survey_multiple_access.py
Tests:
- ✅ Survey can be opened multiple times
- ✅ Survey cannot be accessed after completion
- ✅ Survey cannot be accessed after token expiry
Benefits
- Patient Experience: Patients can now take their time to complete surveys
- Data Integrity:
open_countaccurately tracks multiple visits - Tracking: Each visit is logged with timestamp and device info
- Flexibility: Patients can refresh the page without losing access
- Security: Still prevents access after completion or token expiry
Backward Compatibility
This fix is fully backward compatible. Existing surveys will continue to work as expected, and the change only affects the behavior when patients refresh or revisit survey links.
Related Features
This fix works in conjunction with:
- Survey Tracking System: Tracks opens, time spent, and completion
- Automatic Status Detection:
viewed→in_progresson first interaction - Abandoned Survey Detection: Auto-marks surveys as abandoned after 24 hours
- 2-Day Token Expiry: Link automatically expires after 48 hours