11from django import forms
22
3-
43class ScreeningAppointmentForm (forms .Form ):
54 decision = forms .ChoiceField (
65 choices = (
@@ -44,4 +43,67 @@ def save(self):
4443
4544
4645class AppointmentCannotGoAheadForm (forms .Form ):
47- pass
46+ STOPPED_REASON_CHOICES = (
47+ ("participant_did_not_attend" , "Participant did not attend" ),
48+ ("failed_identity_check" , "Failed identity check" ),
49+ ("language_difficulties" , "Language difficulties" ),
50+ ("physical_health_issue" , "Physical health issue" ),
51+ ("mental_health_issue" , "Mental health issue" ),
52+ ("last_mammogram_within_6_months" , "Last mammogram within 6 months" ),
53+ ("breast_implant_risks" , "Breast implant risks" ),
54+ ("pain_during_screening" , "Pain during screening" ),
55+ ("technical_issues" , "Technical issues" ),
56+ ("participant_withdrew_consent" , "Participant withdrew consent" ),
57+ ("other" , "Other" ),
58+ )
59+
60+ def __init__ (self , * args , ** kwargs ):
61+ if 'instance' not in kwargs :
62+ raise ValueError ("AppointmentCannotGoAheadForm requires an instance" )
63+ self .instance = kwargs .pop ('instance' )
64+ super ().__init__ (* args , ** kwargs )
65+
66+ # Dynamically add detail fields for each choice
67+ for field_name , _ in self .STOPPED_REASON_CHOICES :
68+ self .fields [f"{ field_name } _details" ] = forms .CharField (required = False )
69+
70+ stopped_reasons = forms .MultipleChoiceField (
71+ choices = STOPPED_REASON_CHOICES ,
72+ required = True ,
73+ error_messages = {
74+ "required" : "A reason for why this appointment cannot continue must be provided"
75+ }
76+ )
77+
78+ decision = forms .ChoiceField (
79+ choices = (
80+ ("True" , "Yes, add participant to reinvite list" ),
81+ ("False" , "No" ),
82+ ),
83+ required = True ,
84+ widget = forms .RadioSelect (),
85+ error_messages = {
86+ "required" : "Select whether the participant needs to be invited for another appointment"
87+ }
88+ )
89+
90+ def clean (self ):
91+ cleaned_data = super ().clean ()
92+
93+ if 'stopped_reasons' in cleaned_data and 'other' in cleaned_data ['stopped_reasons' ]:
94+ if not cleaned_data .get ('other_details' ):
95+ self .add_error ('other_details' , 'Explain why this appointment cannot proceed' )
96+ return cleaned_data
97+
98+ def save (self ):
99+ reasons_json = {}
100+ reasons_json ["stopped_reasons" ] = self .cleaned_data ["stopped_reasons" ]
101+ for field_name , value in self .cleaned_data .items ():
102+ if field_name .endswith ('_details' ) and value :
103+ reasons_json [field_name ] = value
104+ self .instance .stopped_reasons = reasons_json
105+ self .instance .reinvite = self .cleaned_data ["decision" ]
106+ self .instance .status = self .instance .Status .ATTENDED_NOT_SCREENED
107+ self .instance .save ()
108+
109+ return self .instance
0 commit comments