Skip to content

Commit 5ba7bfd

Browse files
authored
Merge pull request #764 from NHSDigital/DTOSS-11723-breast-augmentation-year-field
Breast augmentation form uses YearField
2 parents 15515f5 + 941eb13 commit 5ba7bfd

File tree

4 files changed

+30
-80
lines changed

4 files changed

+30
-80
lines changed

manage_breast_screening/mammograms/forms/breast_augmentation_history_form.py

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import datetime
2-
31
from django import forms
42
from django.forms import Form
53
from django.forms.widgets import Textarea
@@ -8,7 +6,7 @@
86
from manage_breast_screening.nhsuk_forms.fields import (
97
BooleanField,
108
CharField,
11-
IntegerField,
9+
YearField,
1210
)
1311
from manage_breast_screening.nhsuk_forms.fields.choice_fields import (
1412
MultipleChoiceField,
@@ -22,6 +20,8 @@ class BreastAugmentationHistoryForm(Form):
2220
right_breast_procedures = MultipleChoiceField(
2321
label="Right breast",
2422
label_classes="nhsuk-fieldset__legend--s",
23+
visually_hidden_label_prefix="What procedure have they had in their ",
24+
visually_hidden_label_suffix="?",
2525
choices=BreastAugmentationHistoryItem.Procedure,
2626
error_messages={
2727
"required": "Select procedures for the right breast",
@@ -31,17 +31,31 @@ class BreastAugmentationHistoryForm(Form):
3131
left_breast_procedures = MultipleChoiceField(
3232
label="Left breast",
3333
label_classes="nhsuk-fieldset__legend--s",
34+
visually_hidden_label_prefix="What procedure have they had in their ",
35+
visually_hidden_label_suffix="?",
3436
choices=BreastAugmentationHistoryItem.Procedure,
3537
error_messages={
3638
"required": "Select procedures for the left breast",
3739
},
3840
exclusive_choices={"NO_PROCEDURES"},
3941
)
42+
procedure_year = YearField(
43+
hint="Leave blank if unknown",
44+
required=False,
45+
label="Year of procedure (optional)",
46+
label_classes="nhsuk-label--m",
47+
classes="nhsuk-input--width-4",
48+
)
4049
implants_have_been_removed = BooleanField(
4150
required=False,
4251
label="Implants have been removed",
4352
classes="app-checkboxes",
4453
)
54+
removal_year = YearField(
55+
required=False,
56+
label="Year removed (if available)",
57+
classes="nhsuk-input--width-4",
58+
)
4559
additional_details = CharField(
4660
hint="Include any other relevant information about the procedure",
4761
required=False,
@@ -52,44 +66,6 @@ class BreastAugmentationHistoryForm(Form):
5266
error_messages={"max_words": "Additional details must be 500 words or less"},
5367
)
5468

55-
def __init__(self, *args, participant, **kwargs):
56-
super().__init__(*args, **kwargs)
57-
58-
# if entered, years should be between 80 years ago and this year
59-
max_year = datetime.date.today().year
60-
min_year = max_year - 80
61-
year_outside_range_error_message = (
62-
f"Year should be between {min_year} and {max_year}."
63-
)
64-
year_invalid_format_error_message = "Enter year as a number."
65-
66-
self.fields["procedure_year"] = IntegerField(
67-
hint="Leave blank if unknown",
68-
required=False,
69-
label="Year of procedure (optional)",
70-
label_classes="nhsuk-label--m",
71-
classes="nhsuk-input--width-4",
72-
min_value=min_year,
73-
max_value=max_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-
self.fields["removal_year"] = IntegerField(
81-
required=False,
82-
label="Year removed (if available)",
83-
classes="nhsuk-input--width-4",
84-
min_value=min_year,
85-
max_value=max_year,
86-
error_messages={
87-
"min_value": year_outside_range_error_message,
88-
"max_value": year_outside_range_error_message,
89-
"invalid": year_invalid_format_error_message,
90-
},
91-
)
92-
9369
def model_values(self):
9470
return dict(
9571
left_breast_procedures=self.cleaned_data.get("left_breast_procedures", []),

manage_breast_screening/mammograms/tests/forms/test_breast_augmentation_history_form.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
@pytest.mark.django_db
1818
class TestBreastAugmentationHistoryForm:
1919
def test_no_data(self, clinical_user):
20-
appointment = AppointmentFactory()
2120
request = RequestFactory().get("/test-form")
2221
request.user = clinical_user
2322

24-
form = BreastAugmentationHistoryForm(
25-
QueryDict(), participant=appointment.participant
26-
)
23+
form = BreastAugmentationHistoryForm(QueryDict())
2724

2825
assert not form.is_valid()
2926
assert form.errors == {
@@ -32,7 +29,6 @@ def test_no_data(self, clinical_user):
3229
}
3330

3431
def test_procedure_year_invalid_format(self, clinical_user):
35-
appointment = AppointmentFactory()
3632
request = RequestFactory().get("/test-form")
3733
request.user = clinical_user
3834

@@ -51,11 +47,10 @@ def test_procedure_year_invalid_format(self, clinical_user):
5147
doseq=True,
5248
)
5349
),
54-
participant=appointment.participant,
5550
)
5651

5752
assert not form.is_valid()
58-
assert form.errors == {"procedure_year": ["Enter year as a number."]}
53+
assert form.errors == {"procedure_year": ["Enter a whole number."]}
5954

6055
@pytest.mark.parametrize(
6156
"selected_breast_procedures",
@@ -78,7 +73,6 @@ def test_procedure_year_invalid_format(self, clinical_user):
7873
def test_no_procedures_and_other_options(
7974
self, clinical_user, selected_breast_procedures
8075
):
81-
appointment = AppointmentFactory()
8276
request = RequestFactory().get("/test-form")
8377
request.user = clinical_user
8478

@@ -94,7 +88,6 @@ def test_no_procedures_and_other_options(
9488
doseq=True,
9589
)
9690
),
97-
participant=appointment.participant,
9891
)
9992

10093
assert not form.is_valid()
@@ -116,7 +109,6 @@ def test_no_procedures_and_other_options(
116109
doseq=True,
117110
)
118111
),
119-
participant=appointment.participant,
120112
)
121113

122114
assert not form.is_valid()
@@ -127,7 +119,6 @@ def test_no_procedures_and_other_options(
127119
}
128120

129121
def test_removal_year_invalid_format(self, clinical_user):
130-
appointment = AppointmentFactory()
131122
request = RequestFactory().get("/test-form")
132123
request.user = clinical_user
133124

@@ -147,13 +138,12 @@ def test_removal_year_invalid_format(self, clinical_user):
147138
doseq=True,
148139
)
149140
),
150-
participant=appointment.participant,
151141
)
152142

153143
assert not form.is_valid()
154144
assert form.errors == {
155145
"removal_year": [
156-
"Enter year as a number.",
146+
"Enter a whole number.",
157147
]
158148
}
159149

@@ -168,14 +158,15 @@ def test_removal_year_invalid_format(self, clinical_user):
168158
],
169159
)
170160
def test_procedure_year_outside_range(self, clinical_user, procedure_year):
171-
appointment = AppointmentFactory()
172161
request = RequestFactory().get("/test-form")
173162
request.user = clinical_user
174163

175164
max_year = datetime.date.today().year
176165
min_year = max_year - 80
177166
year_outside_range_error_message = (
178-
f"Year should be between {min_year} and {max_year}."
167+
(f"Year must be {max_year} or earlier")
168+
if procedure_year > max_year
169+
else (f"Year must be {min_year} or later")
179170
)
180171
form = BreastAugmentationHistoryForm(
181172
QueryDict(
@@ -192,7 +183,6 @@ def test_procedure_year_outside_range(self, clinical_user, procedure_year):
192183
doseq=True,
193184
)
194185
),
195-
participant=appointment.participant,
196186
)
197187

198188
assert not form.is_valid()
@@ -209,14 +199,15 @@ def test_procedure_year_outside_range(self, clinical_user, procedure_year):
209199
],
210200
)
211201
def test_removal_year_outside_range(self, clinical_user, removal_year):
212-
appointment = AppointmentFactory()
213202
request = RequestFactory().get("/test-form")
214203
request.user = clinical_user
215204

216205
max_year = datetime.date.today().year
217206
min_year = max_year - 80
218207
year_outside_range_error_message = (
219-
f"Year should be between {min_year} and {max_year}."
208+
(f"Year must be {max_year} or earlier")
209+
if removal_year > max_year
210+
else (f"Year must be {min_year} or later")
220211
)
221212
form = BreastAugmentationHistoryForm(
222213
QueryDict(
@@ -234,7 +225,6 @@ def test_removal_year_outside_range(self, clinical_user, removal_year):
234225
doseq=True,
235226
)
236227
),
237-
participant=appointment.participant,
238228
)
239229

240230
assert not form.is_valid()
@@ -255,7 +245,6 @@ def test_removal_year_outside_range(self, clinical_user, removal_year):
255245
def test_removal_year_before_procedure_year(
256246
self, clinical_user, procedure_year, removal_year
257247
):
258-
appointment = AppointmentFactory()
259248
request = RequestFactory().get("/test-form")
260249
request.user = clinical_user
261250

@@ -276,7 +265,6 @@ def test_removal_year_before_procedure_year(
276265
doseq=True,
277266
)
278267
),
279-
participant=appointment.participant,
280268
)
281269

282270
assert not form.is_valid()
@@ -306,7 +294,6 @@ def test_removal_year_when_not_removed(self, clinical_user):
306294
doseq=True,
307295
)
308296
),
309-
participant=appointment.participant,
310297
)
311298

312299
# confirm full_clean removes removal_year but keeps procedure_year
@@ -425,7 +412,6 @@ def test_success(self, clinical_user, data):
425412

426413
form = BreastAugmentationHistoryForm(
427414
QueryDict(urlencode(data, doseq=True)),
428-
participant=appointment.participant,
429415
)
430416

431417
assert form.is_valid()

manage_breast_screening/mammograms/views/breast_augmentation_history_view.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from functools import cached_property
2-
31
from django.contrib import messages
42
from django.urls import reverse
53
from django.views.generic import FormView
@@ -55,12 +53,3 @@ def get_context_data(self, **kwargs):
5553
)
5654

5755
return context
58-
59-
@cached_property
60-
def participant(self):
61-
return self.appointment.participant
62-
63-
def get_form_kwargs(self):
64-
kwargs = super().get_form_kwargs()
65-
kwargs["participant"] = self.participant
66-
return kwargs

manage_breast_screening/tests/system/clinical/test_breast_augmentation_history.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_adding_breast_augmentation(self):
1919
self.then_i_see_the_add_breast_augmentation_form()
2020

2121
self.when_i_click_save_without_entering_details()
22-
self.then_i_see_validation_errors_for_missing_benign_lump_details()
22+
self.then_i_see_validation_errors_for_missing_breast_augmentation_details()
2323

2424
self.when_i_select_procedures()
2525
self.and_i_enter_the_procedure_year()
@@ -65,7 +65,7 @@ def then_i_see_the_add_breast_augmentation_form(self):
6565
"Add details of breast implants or augmentation"
6666
)
6767

68-
def then_i_see_validation_errors_for_missing_benign_lump_details(self):
68+
def then_i_see_validation_errors_for_missing_breast_augmentation_details(self):
6969
self.expect_validation_error(
7070
error_text="Select procedures for the right breast",
7171
fieldset_legend="Right breast",
@@ -97,12 +97,11 @@ def and_i_enter_additional_details(self):
9797
"additional details for test of breast augmentation history"
9898
)
9999

100-
def when_i_click_save_without_entering_details(self):
101-
self.and_i_click_save_augmentation()
102-
103100
def and_i_click_save_augmentation(self):
104101
self.page.get_by_text("Save").click()
105102

103+
when_i_click_save_without_entering_details = and_i_click_save_augmentation
104+
106105
def then_i_am_back_on_the_medical_information_page(self):
107106
self.expect_url("mammograms:record_medical_information", pk=self.appointment.pk)
108107

0 commit comments

Comments
 (0)