|
| 1 | +import datetime |
| 2 | + |
| 3 | +from django.forms import widgets |
| 4 | +from django.forms.widgets import RadioSelect, Textarea |
| 5 | + |
| 6 | +from manage_breast_screening.core.services.auditor import Auditor |
| 7 | +from manage_breast_screening.nhsuk_forms.fields import ( |
| 8 | + CharField, |
| 9 | + ChoiceField, |
| 10 | + IntegerField, |
| 11 | +) |
| 12 | +from manage_breast_screening.nhsuk_forms.fields.choice_fields import ( |
| 13 | + MultipleChoiceField, |
| 14 | +) |
| 15 | +from manage_breast_screening.nhsuk_forms.forms import FormWithConditionalFields |
| 16 | +from manage_breast_screening.participants.models.mastectomy_or_lumpectomy_history_item import ( |
| 17 | + MastectomyOrLumpectomyHistoryItem, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class MastectomyOrLumpectomyHistoryForm(FormWithConditionalFields): |
| 22 | + right_breast_procedure = ChoiceField( |
| 23 | + label="Right breast", |
| 24 | + visually_hidden_label_prefix="What procedure have they had in their ", |
| 25 | + visually_hidden_label_suffix="?", |
| 26 | + label_classes="nhsuk-fieldset__legend--s", |
| 27 | + widget=RadioSelect, |
| 28 | + choices=MastectomyOrLumpectomyHistoryItem.Procedure, |
| 29 | + error_messages={ |
| 30 | + "required": "Select which procedure they have had in the right breast", |
| 31 | + }, |
| 32 | + ) |
| 33 | + left_breast_procedure = ChoiceField( |
| 34 | + label="Left breast", |
| 35 | + visually_hidden_label_prefix="What procedure have they had in their ", |
| 36 | + visually_hidden_label_suffix="?", |
| 37 | + label_classes="nhsuk-fieldset__legend--s", |
| 38 | + widget=RadioSelect, |
| 39 | + choices=MastectomyOrLumpectomyHistoryItem.Procedure, |
| 40 | + error_messages={ |
| 41 | + "required": "Select which procedure they have had in the left breast", |
| 42 | + }, |
| 43 | + ) |
| 44 | + right_breast_other_surgery = MultipleChoiceField( |
| 45 | + label="Right breast", |
| 46 | + visually_hidden_label_prefix="What other surgery have they had in their ", |
| 47 | + visually_hidden_label_suffix="?", |
| 48 | + label_classes="nhsuk-fieldset__legend--s", |
| 49 | + choices=MastectomyOrLumpectomyHistoryItem.Surgery, |
| 50 | + error_messages={ |
| 51 | + "required": "Select any other surgery they have had in the right breast", |
| 52 | + }, |
| 53 | + exclusive_choices={"NO_OTHER_SURGERY"}, |
| 54 | + ) |
| 55 | + left_breast_other_surgery = MultipleChoiceField( |
| 56 | + label="Left breast", |
| 57 | + visually_hidden_label_prefix="What other surgery have they had in their ", |
| 58 | + visually_hidden_label_suffix="?", |
| 59 | + label_classes="nhsuk-fieldset__legend--s", |
| 60 | + choices=MastectomyOrLumpectomyHistoryItem.Surgery, |
| 61 | + error_messages={ |
| 62 | + "required": "Select any other surgery they have had in the left breast", |
| 63 | + }, |
| 64 | + exclusive_choices={"NO_OTHER_SURGERY"}, |
| 65 | + ) |
| 66 | + year_of_surgery = IntegerField( |
| 67 | + hint="Leave blank if unknown", |
| 68 | + required=False, |
| 69 | + label="Year of surgery (optional)", |
| 70 | + label_classes="nhsuk-label--m", |
| 71 | + classes="nhsuk-input--width-4", |
| 72 | + min_value=datetime.date.today().year - 80, |
| 73 | + max_value=datetime.date.today().year, |
| 74 | + error_messages={ |
| 75 | + "min_value": "year_outside_range_error_message", |
| 76 | + "max_value": "year_outside_range_error_message", |
| 77 | + "invalid": "year_invalid_format_error_message", |
| 78 | + }, |
| 79 | + ) |
| 80 | + surgery_reason = ChoiceField( |
| 81 | + choices=MastectomyOrLumpectomyHistoryItem.SurgeryReason, |
| 82 | + label="Why was this surgery done?", |
| 83 | + widget=widgets.RadioSelect, |
| 84 | + error_messages={"required": "Select the reason for surgery"}, |
| 85 | + ) |
| 86 | + surgery_other_reason_details = CharField( |
| 87 | + required=False, |
| 88 | + label="Provide details", |
| 89 | + error_messages={"required": "Provide details of the surgery"}, |
| 90 | + classes="nhsuk-u-width-two-thirds", |
| 91 | + ) |
| 92 | + additional_details = CharField( |
| 93 | + hint="Include any other relevant information about the surgery", |
| 94 | + required=False, |
| 95 | + label="Additional details (optional)", |
| 96 | + label_classes="nhsuk-label--m", |
| 97 | + widget=Textarea(attrs={"rows": 4}), |
| 98 | + max_words=500, |
| 99 | + error_messages={"max_words": "Additional details must be 500 words or less"}, |
| 100 | + ) |
| 101 | + |
| 102 | + def __init__(self, *args, participant, **kwargs): |
| 103 | + super().__init__(*args, **kwargs) |
| 104 | + |
| 105 | + self.given_field_value( |
| 106 | + "surgery_reason", |
| 107 | + MastectomyOrLumpectomyHistoryItem.SurgeryReason.OTHER_REASON, |
| 108 | + ).require_field("surgery_other_reason_details") |
| 109 | + |
| 110 | + def model_values(self): |
| 111 | + return dict( |
| 112 | + left_breast_procedure=self.cleaned_data.get("left_breast_procedure", None), |
| 113 | + right_breast_procedure=self.cleaned_data.get( |
| 114 | + "right_breast_procedure", None |
| 115 | + ), |
| 116 | + left_breast_other_surgery=self.cleaned_data.get( |
| 117 | + "left_breast_other_surgery", [] |
| 118 | + ), |
| 119 | + right_breast_other_surgery=self.cleaned_data.get( |
| 120 | + "right_breast_other_surgery", [] |
| 121 | + ), |
| 122 | + year_of_surgery=self.cleaned_data.get("year_of_surgery", None), |
| 123 | + surgery_reason=self.cleaned_data.get("surgery_reason", None), |
| 124 | + surgery_other_reason_details=self.cleaned_data.get( |
| 125 | + "surgery_other_reason_details", "" |
| 126 | + ), |
| 127 | + additional_details=self.cleaned_data.get("additional_details", ""), |
| 128 | + ) |
| 129 | + |
| 130 | + def create(self, appointment, request): |
| 131 | + auditor = Auditor.from_request(request) |
| 132 | + field_values = self.model_values() |
| 133 | + |
| 134 | + mastectomy_or_lumpectomy_history = ( |
| 135 | + appointment.mastectomy_or_lumpectomy_history_items.create( |
| 136 | + appointment=appointment, |
| 137 | + **field_values, |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + auditor.audit_create(mastectomy_or_lumpectomy_history) |
| 142 | + |
| 143 | + return mastectomy_or_lumpectomy_history |
0 commit comments