|
| 1 | +from django.forms.widgets import Textarea |
| 2 | + |
| 3 | +from manage_breast_screening.manual_images.services import StudyService |
| 4 | +from manage_breast_screening.nhsuk_forms.fields import CharField, IntegerField |
| 5 | +from manage_breast_screening.nhsuk_forms.forms import FormWithConditionalFields |
| 6 | +from manage_breast_screening.participants.models.appointment import ( |
| 7 | + AppointmentWorkflowStepCompletion, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class AddImageDetailsForm(FormWithConditionalFields): |
| 12 | + rmlo_count = IntegerField( |
| 13 | + label="RMLO", |
| 14 | + classes="nhsuk-input--width-4", |
| 15 | + required=True, |
| 16 | + min_value=0, |
| 17 | + max_value=20, |
| 18 | + error_messages={ |
| 19 | + "min_value": "Number of RMLO images must be at least 0.", |
| 20 | + "max_value": "Number of RMLO images must be at most 20.", |
| 21 | + "invalid": "Enter a valid number of RMLO images.", |
| 22 | + "required": "Enter the number of RMLO images.", |
| 23 | + }, |
| 24 | + ) |
| 25 | + rcc_count = IntegerField( |
| 26 | + label="RCC", |
| 27 | + classes="nhsuk-input--width-4", |
| 28 | + required=True, |
| 29 | + min_value=0, |
| 30 | + max_value=20, |
| 31 | + error_messages={ |
| 32 | + "min_value": "Number of RCC images must be at least 0.", |
| 33 | + "max_value": "Number of RCC images must be at most 20.", |
| 34 | + "invalid": "Enter a valid number of RCC images.", |
| 35 | + "required": "Enter the number of RCC images.", |
| 36 | + }, |
| 37 | + ) |
| 38 | + right_eklund_count = IntegerField( |
| 39 | + label="Right Eklund", |
| 40 | + hint="Used with breast implants", |
| 41 | + classes="nhsuk-input--width-4", |
| 42 | + required=True, |
| 43 | + min_value=0, |
| 44 | + max_value=20, |
| 45 | + error_messages={ |
| 46 | + "min_value": "Number of Right Eklund images must be at least 0.", |
| 47 | + "max_value": "Number of Right Eklund images must be at most 20.", |
| 48 | + "invalid": "Enter a valid number of Right Eklund images.", |
| 49 | + "required": "Enter the number of Right Eklund images.", |
| 50 | + }, |
| 51 | + ) |
| 52 | + lmlo_count = IntegerField( |
| 53 | + label="LMLO", |
| 54 | + classes="nhsuk-input--width-4", |
| 55 | + required=True, |
| 56 | + min_value=0, |
| 57 | + max_value=20, |
| 58 | + error_messages={ |
| 59 | + "min_value": "Number of LMLO images must be at least 0.", |
| 60 | + "max_value": "Number of LMLO images must be at most 20.", |
| 61 | + "invalid": "Enter a valid number of LMLO images.", |
| 62 | + "required": "Enter the number of LMLO images.", |
| 63 | + }, |
| 64 | + ) |
| 65 | + lcc_count = IntegerField( |
| 66 | + label="LCC", |
| 67 | + classes="nhsuk-input--width-4", |
| 68 | + required=True, |
| 69 | + min_value=0, |
| 70 | + max_value=20, |
| 71 | + error_messages={ |
| 72 | + "min_value": "Number of LCC images must be at least 0.", |
| 73 | + "max_value": "Number of LCC images must be at most 20.", |
| 74 | + "invalid": "Enter a valid number of LCC images.", |
| 75 | + "required": "Enter the number of LCC images.", |
| 76 | + }, |
| 77 | + ) |
| 78 | + left_eklund_count = IntegerField( |
| 79 | + label="Left Eklund", |
| 80 | + hint="Used with breast implants", |
| 81 | + classes="nhsuk-input--width-4", |
| 82 | + required=True, |
| 83 | + min_value=0, |
| 84 | + max_value=20, |
| 85 | + error_messages={ |
| 86 | + "min_value": "Number of Left Eklund images must be at least 0.", |
| 87 | + "max_value": "Number of Left Eklund images must be at most 20.", |
| 88 | + "invalid": "Enter a valid number of Left Eklund images.", |
| 89 | + "required": "Enter the number of Left Eklund images.", |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + additional_details = CharField( |
| 94 | + hint="Provide information for image readers when reviewing", |
| 95 | + required=False, |
| 96 | + label="Notes for reader (optional)", |
| 97 | + label_classes="nhsuk-label--s", |
| 98 | + widget=Textarea(attrs={"rows": 2}), |
| 99 | + max_words=500, |
| 100 | + error_messages={"max_words": "Notes for reader must be 500 words or less"}, |
| 101 | + ) |
| 102 | + |
| 103 | + def __init__(self, *args, **kwargs): |
| 104 | + super().__init__(*args, **kwargs) |
| 105 | + |
| 106 | + def clean(self): |
| 107 | + cleaned_data = super().clean() |
| 108 | + |
| 109 | + if ( |
| 110 | + self.cleaned_data.get("rmlo_count") == 0 |
| 111 | + and self.cleaned_data.get("rcc_count") == 0 |
| 112 | + and self.cleaned_data.get("right_eklund_count") == 0 |
| 113 | + and self.cleaned_data.get("lmlo_count") == 0 |
| 114 | + and self.cleaned_data.get("lcc_count") == 0 |
| 115 | + and self.cleaned_data.get("left_eklund_count") == 0 |
| 116 | + ): |
| 117 | + self.add_error(None, "Enter at least one image count") |
| 118 | + |
| 119 | + return cleaned_data |
| 120 | + |
| 121 | + def save(self, study_service: StudyService): |
| 122 | + return study_service.create( |
| 123 | + additional_details=self.cleaned_data.get("additional_details", ""), |
| 124 | + series_data=[ |
| 125 | + self.build_series_dict("MLO", "R", "rmlo_count"), |
| 126 | + self.build_series_dict("CC", "R", "rcc_count"), |
| 127 | + self.build_series_dict("EKLUND", "R", "right_eklund_count"), |
| 128 | + self.build_series_dict("MLO", "L", "lmlo_count"), |
| 129 | + self.build_series_dict("CC", "L", "lcc_count"), |
| 130 | + self.build_series_dict("EKLUND", "L", "left_eklund_count"), |
| 131 | + ], |
| 132 | + ) |
| 133 | + |
| 134 | + def build_series_dict(self, view_position, laterality, count_field_name): |
| 135 | + return { |
| 136 | + "view_position": view_position, |
| 137 | + "laterality": laterality, |
| 138 | + "count": self.cleaned_data.get(count_field_name), |
| 139 | + } |
| 140 | + |
| 141 | + def mark_workflow_step_complete(self): |
| 142 | + self.appointment.completed_workflow_steps.get_or_create( |
| 143 | + step_name=AppointmentWorkflowStepCompletion.StepNames.TAKE_IMAGES, |
| 144 | + defaults={"created_by": self.request.user}, |
| 145 | + ) |
0 commit comments