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