Skip to content

Commit 6030339

Browse files
committed
use MastectomyOrLumpectomyHistoryForm for both add and update
1 parent bb9547b commit 6030339

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

manage_breast_screening/mammograms/forms/mastectomy_or_lumpectomy_history_form.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818

19-
class MastectomyOrLumpectomyHistoryBaseForm(FormWithConditionalFields):
19+
class MastectomyOrLumpectomyHistoryForm(FormWithConditionalFields):
2020
right_breast_procedure = ChoiceField(
2121
label="Right breast",
2222
visually_hidden_label_prefix="What procedure have they had in their ",
@@ -91,6 +91,20 @@ class MastectomyOrLumpectomyHistoryBaseForm(FormWithConditionalFields):
9191
)
9292

9393
def __init__(self, *args, **kwargs):
94+
self.instance = kwargs.pop("instance", None)
95+
96+
if self.instance:
97+
kwargs["initial"] = {
98+
"left_breast_procedure": self.instance.left_breast_procedure,
99+
"right_breast_procedure": self.instance.right_breast_procedure,
100+
"left_breast_other_surgery": self.instance.left_breast_other_surgery,
101+
"right_breast_other_surgery": self.instance.right_breast_other_surgery,
102+
"year_of_surgery": self.instance.year_of_surgery,
103+
"surgery_reason": self.instance.surgery_reason,
104+
"surgery_other_reason_details": self.instance.surgery_other_reason_details,
105+
"additional_details": self.instance.additional_details,
106+
}
107+
94108
super().__init__(*args, **kwargs)
95109

96110
self.given_field_value(
@@ -118,8 +132,6 @@ def model_values(self):
118132
additional_details=self.cleaned_data.get("additional_details", ""),
119133
)
120134

121-
122-
class MastectomyOrLumpectomyHistoryForm(MastectomyOrLumpectomyHistoryBaseForm):
123135
def create(self, appointment, request):
124136
auditor = Auditor.from_request(request)
125137
field_values = self.model_values()
@@ -135,24 +147,6 @@ def create(self, appointment, request):
135147

136148
return mastectomy_or_lumpectomy_history
137149

138-
139-
class MastectomyOrLumpectomyHistoryUpdateForm(MastectomyOrLumpectomyHistoryBaseForm):
140-
def __init__(self, instance, *args, **kwargs):
141-
self.instance = instance
142-
143-
kwargs["initial"] = {
144-
"left_breast_procedure": instance.left_breast_procedure,
145-
"right_breast_procedure": instance.right_breast_procedure,
146-
"left_breast_other_surgery": instance.left_breast_other_surgery,
147-
"right_breast_other_surgery": instance.right_breast_other_surgery,
148-
"year_of_surgery": instance.year_of_surgery,
149-
"surgery_reason": instance.surgery_reason,
150-
"surgery_other_reason_details": instance.surgery_other_reason_details,
151-
"additional_details": instance.additional_details,
152-
}
153-
154-
super().__init__(*args, **kwargs)
155-
156150
def update(self, request):
157151
self.instance.left_breast_procedure = self.cleaned_data["left_breast_procedure"]
158152
self.instance.right_breast_procedure = self.cleaned_data[

manage_breast_screening/mammograms/tests/forms/test_mastectomy_or_lumpectomy_history_form.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from ...forms.mastectomy_or_lumpectomy_history_form import (
1818
MastectomyOrLumpectomyHistoryForm,
19-
MastectomyOrLumpectomyHistoryUpdateForm,
2019
)
2120

2221

@@ -331,7 +330,7 @@ def create_year_outside_range_error_messsage(self, request_year):
331330

332331

333332
@pytest.mark.django_db
334-
class TestImplantedMedicalDeviceHistoryUpdateForm:
333+
class TestUpdateMastectomyOrLumpectomyHistoryForm:
335334
@pytest.fixture
336335
def instance(self):
337336
return MastectomyOrLumpectomyHistoryItemFactory(
@@ -349,7 +348,7 @@ def instance(self):
349348
)
350349

351350
def test_no_data(self, instance):
352-
form = MastectomyOrLumpectomyHistoryUpdateForm(instance, QueryDict())
351+
form = MastectomyOrLumpectomyHistoryForm(QueryDict(), instance=instance)
353352

354353
assert not form.is_valid()
355354
assert form.errors == {
@@ -388,9 +387,9 @@ def test_no_data(self, instance):
388387
],
389388
)
390389
def test_success(self, instance, data, dummy_request):
391-
form = MastectomyOrLumpectomyHistoryUpdateForm(
392-
instance,
390+
form = MastectomyOrLumpectomyHistoryForm(
393391
QueryDict(urlencode(data, doseq=True)),
392+
instance=instance,
394393
)
395394

396395
assert form.is_valid()

manage_breast_screening/mammograms/views/mastectomy_or_lumpectomy_history_view.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from ..forms.mastectomy_or_lumpectomy_history_form import (
1818
MastectomyOrLumpectomyHistoryForm,
19-
MastectomyOrLumpectomyHistoryUpdateForm,
2019
)
2120
from .mixins import InProgressAppointmentMixin
2221

@@ -89,7 +88,7 @@ def get_context_data(self, **kwargs):
8988

9089

9190
class UpdateMastectomyOrLumpectomyHistoryView(BaseMastectomyOrLumpectomyHistoryView):
92-
form_class = MastectomyOrLumpectomyHistoryUpdateForm
91+
form_class = MastectomyOrLumpectomyHistoryForm
9392

9493
def get_instance(self):
9594
try:

0 commit comments

Comments
 (0)